// Execute adds all child commands to the root command sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
funcExecute(){
iferr:=RootCmd.Execute();err!=nil{
fmt.Println(err)
os.Exit(1)
}
}
funcinit(){
cobra.OnInitialize(initConfig,initLogger)
// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.
RootCmd.PersistentFlags().StringVar(&cfgFile,"config","","config file (default is $HOME/.php-fpm_exporter.yaml)")
RootCmd.PersistentFlags().StringVar(&logLevel,"log.level","error","Only log messages with the given severity or above. Valid levels: [debug, info, warn, error, fatal]")
// Cobra also supports local flags, which will only run
// when this action is called directly.
RootCmd.Flags().BoolP("toggle","t",false,"Help message for toggle")
}
// initConfig reads in config file and ENV variables if set.
funcinitConfig(){
ifcfgFile!=""{
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
}else{
// Find home directory.
home,err:=homedir.Dir()
iferr!=nil{
fmt.Println(err)
os.Exit(1)
}
// Search config in home directory with name ".php-fpm_exporter" (without extension).
viper.AddConfigPath(home)
viper.SetConfigName(".php-fpm_exporter")
}
viper.AutomaticEnv()// read in environment variables that match