go.mondoo.com/cnquery@v0.0.0-20231005093811-59568235f6ea/providers-sdk/v1/lr/cli/cmd/root.go (about) 1 // copyright: 2019, Dominik Richter and Christoph Hartmann 2 // author: Dominik Richter 3 // author: Christoph Hartmann 4 5 package cmd 6 7 import ( 8 "fmt" 9 "os" 10 11 homedir "github.com/mitchellh/go-homedir" 12 "github.com/spf13/cobra" 13 "github.com/spf13/viper" 14 ) 15 16 var cfgFile string 17 18 // rootCmd represents the base command when called without any subcommands 19 var rootCmd = &cobra.Command{ 20 Use: "cli", 21 Short: "lr", 22 Long: `MQL Resources`, 23 // Uncomment the following line if your bare application 24 // has an action associated with it: 25 // Run: func(cmd *cobra.Command, args []string) { }, 26 } 27 28 // Execute adds all child commands to the root command and sets flags appropriately. 29 // This is called by main.main(). It only needs to happen once to the rootCmd. 30 func Execute() { 31 if err := rootCmd.Execute(); err != nil { 32 fmt.Println(err) 33 os.Exit(1) 34 } 35 } 36 37 func init() { 38 cobra.OnInitialize(initConfig) 39 40 // Here you will define your flags and configuration settings. 41 // Cobra supports persistent flags, which, if defined here, 42 // will be global for your application. 43 rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "Set config file path (default $HOME/.lr.yaml)") 44 } 45 46 // initConfig reads in config file and ENV variables if set. 47 func initConfig() { 48 if cfgFile != "" { 49 // Use config file from the flag. 50 viper.SetConfigFile(cfgFile) 51 } else { 52 // Find home directory. 53 home, err := homedir.Dir() 54 if err != nil { 55 fmt.Println(err) 56 os.Exit(1) 57 } 58 59 // Search config in home directory with name ".cli" (without extension). 60 viper.AddConfigPath(home) 61 viper.SetConfigName(".lr") 62 } 63 64 viper.AutomaticEnv() // read in environment variables that match 65 66 // If a config file is found, read it in. 67 if err := viper.ReadInConfig(); err == nil { 68 fmt.Println("Using config file:", viper.ConfigFileUsed()) 69 } 70 }