/* Copyright © 2021 Sniptt Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package cmd import ( "context" "fmt" "os" "github.com/sniptt-official/ots/build" "github.com/spf13/cobra" "github.com/spf13/viper" ) var ( cfgFile string // rootCmd represents the base command when called without any subcommands rootCmd = &cobra.Command{ Use: "ots", Short: "Easily create and share end-to-end encrypted secrets with others", Version: build.Version, } ) // Execute adds all child commands to the root command and sets flags appropriately. // This is called by main.main(). It only needs to happen once to the rootCmd. func Execute(ctx context.Context) { if err := rootCmd.ExecuteContext(ctx); err != nil { fmt.Fprintf(os.Stderr, "Error: %v\n", err) os.Exit(1) } } func init() { cobra.OnInitialize(initConfig) rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.ots.yaml)") initDefaults() } func initDefaults() { // Set default API URL for each region viper.SetDefault("apiUrl.us-east-1", "https://ots.us-east-1.api.sniptt.com/secrets") viper.SetDefault("apiUrl.eu-central-1", "https://ots.eu-central-1.api.sniptt.com/secrets") // Set default API keys for each region viper.SetDefault("apiKey.us-east-1", "V3aFLdJneB21Cyd5m6peY7TEMy9H13kH3KQUHGnC") viper.SetDefault("apiKey.eu-central-1", "xLNkEyYKlS4Fu3ieSZtjY34rG9DeQXeY3a3QvGwA") } // initConfig reads in config file and ENV variables if set. func initConfig() { if cfgFile != "" { viper.SetConfigFile(cfgFile) } else { home, err := os.UserHomeDir() if err != nil { fmt.Fprintf(os.Stderr, "Error getting home directory: %v\n", err) os.Exit(1) } viper.AddConfigPath(home) viper.SetConfigType("yaml") viper.SetConfigName(".ots") } viper.SetEnvPrefix("OTS") viper.AutomaticEnv() if err := viper.ReadInConfig(); err == nil { fmt.Fprintf(os.Stderr, "Using config file: %s\n", viper.ConfigFileUsed()) } }