github.com/quantosnetwork/Quantos@v0.0.0-20220306172517-e20b28c5a29a/cmd/root.go (about) 1 /* 2 Copyright © 2022 Quantos developers 3 4 Permission is hereby granted, free of charge, to any person obtaining a copy 5 of this software and associated documentation files (the "Software"), to deal 6 in the Software without restriction, including without limitation the rights 7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 copies of the Software, and to permit persons to whom the Software is 9 furnished to do so, subject to the following conditions: 10 11 The above copyright notice and this permission notice shall be included in 12 all copies or substantial portions of the Software. 13 14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 THE SOFTWARE. 21 */ 22 package cmd 23 24 import ( 25 "fmt" 26 "github.com/spf13/cobra" 27 "os" 28 29 "github.com/spf13/viper" 30 ) 31 32 var cfgFile string 33 34 // rootCmd represents the base command when called without any subcommands 35 var rootCmd = &cobra.Command{ 36 Use: "quantos-cli", 37 Short: "A brief description of your application", 38 Long: `A longer description that spans multiple lines and likely contains 39 examples and usage of using your application. For example: 40 41 Cobra is a CLI library for Go that empowers applications. 42 This application is a tool to generate the needed files 43 to quickly create a Cobra application.`, 44 // Uncomment the following line if your bare application 45 // has an action associated with it: 46 // Run: func(cmd *cobra.Command, args []string) { }, 47 } 48 49 // Execute adds all child commands to the root command and sets flags appropriately. 50 // This is called by main.main(). It only needs to happen once to the rootCmd. 51 func Execute() { 52 cobra.CheckErr(rootCmd.Execute()) 53 } 54 55 func init() { 56 cobra.OnInitialize(initConfig) 57 58 // Here you will define your flags and configuration settings. 59 // Cobra supports persistent flags, which, if defined here, 60 // will be global for your application. 61 62 rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.quantos-cli.yaml)") 63 64 // Cobra also supports local flags, which will only run 65 // when this action is called directly. 66 rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") 67 } 68 69 // initConfig reads in config file and ENV variables if set. 70 func initConfig() { 71 if cfgFile != "" { 72 // Use config file from the flag. 73 viper.SetConfigFile(cfgFile) 74 } else { 75 // Find home directory. 76 home, err := os.UserHomeDir() 77 cobra.CheckErr(err) 78 79 // Search config in home directory with name ".quantos-cli" (without extension). 80 viper.AddConfigPath(home) 81 viper.SetConfigType("yaml") 82 viper.SetConfigName(".quantos-cli") 83 } 84 85 viper.AutomaticEnv() // read in environment variables that match 86 87 // If a config file is found, read it in. 88 if err := viper.ReadInConfig(); err == nil { 89 fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed()) 90 } 91 }