github.com/alibaba/sealer@v0.8.6-0.20220430115802-37a2bdaa8173/seautil/cmd/root.go (about) 1 // Copyright © 2021 Alibaba Group Holding Ltd. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package cmd 16 17 import ( 18 "fmt" 19 "os" 20 21 homedir "github.com/mitchellh/go-homedir" 22 "github.com/spf13/cobra" 23 "github.com/spf13/viper" 24 ) 25 26 var cfgFile string 27 28 // rootCmd represents the base command when called without any subcommands 29 var rootCmd = &cobra.Command{ 30 Use: "seautil", 31 Short: "A brief description of your application", 32 Long: `A longer description that spans multiple lines and likely contains 33 examples and usage of using your application. For example: 34 35 Cobra is a CLI library for Go that empowers applications. 36 This application is a tool to generate the needed files 37 to quickly create a Cobra application.`, 38 // Uncomment the following line if your bare application 39 // has an action associated with it: 40 // Run: func(cmd *cobra.Command, args []string) { }, 41 } 42 43 // Execute adds all child commands to the root command and sets flags appropriately. 44 // This is called by main.main(). It only needs to happen once to the rootCmd. 45 func Execute() { 46 if err := rootCmd.Execute(); err != nil { 47 fmt.Println(err) 48 os.Exit(1) 49 } 50 } 51 52 func init() { 53 cobra.OnInitialize(initConfig) 54 55 // Here you will define your flags and configuration settings. 56 // Cobra supports persistent flags, which, if defined here, 57 // will be global for your application. 58 59 rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.seautil.yaml)") 60 61 // Cobra also supports local flags, which will only run 62 // when this action is called directly. 63 rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") 64 } 65 66 // initConfig reads in config file and ENV variables if set. 67 func initConfig() { 68 if cfgFile != "" { 69 // Use config file from the flag. 70 viper.SetConfigFile(cfgFile) 71 } else { 72 // Find home directory. 73 home, err := homedir.Dir() 74 if err != nil { 75 fmt.Println(err) 76 os.Exit(1) 77 } 78 79 // Search config in home directory with name ".seautil" (without extension). 80 viper.AddConfigPath(home) 81 viper.SetConfigName(".seautil") 82 } 83 84 viper.AutomaticEnv() // read in environment variables that match 85 86 // If a config file is found, read it in. 87 if err := viper.ReadInConfig(); err == nil { 88 fmt.Println("Using config file:", viper.ConfigFileUsed()) 89 } 90 }