package cmd import ( "fmt" "os" "github.com/spf13/cobra" "github.com/spf13/viper" ) var cfgFile string //GlobalCfg is a struct with all the global configuration available var GlobalCfg objects.GlobalConfig // rootCmd represents the base command when called without any subcommands var rootCmd = &cobra.Command{ Use: "feeder", Short: "Generate iTunes-compatible RSS feed generator", Long: `Generate iTunes-compatible RSS feed generator.`, } // 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() { if err := rootCmd.Execute(); err != nil { fmt.Println(err) os.Exit(1) } } func init() { cobra.OnInitialize(initConfig) } // initConfig reads in config file and ENV variables if set. func initConfig() { if cfgFile != "" { // enable ability to specify config file via flag viper.SetConfigFile(cfgFile) } viper.SetConfigName("feeder") // name of config file (without extension) viper.AddConfigPath("$HOME") // adding home directory as first search path viper.AddConfigPath("/usr/local/etc/feeder") // Finally, look in /etc as second search path viper.SetConfigType("yaml") // or viper.SetConfigType("yaml") viper.AutomaticEnv() // read in environment variables that match // If a config file is found, read it in. if err := viper.ReadInConfig(); err == nil { GlobalCfg.SourceHost = viper.GetString("podcast_name") GlobalCfg.SourceHost = viper.GetString("podcast_author") GlobalCfg.SourceHost = viper.GetString("podcast_link") GlobalCfg.SourceHost = viper.GetString("podcast_language") GlobalCfg.SourceHost = viper.GetString("podcast_is_explicit") GlobalCfg.SourceHost = viper.GetString("podcast_owner") GlobalCfg.SourceHost = viper.GetString("podcast_owner_email") GlobalCfg.SourceHost = viper.GetString("podcast_categories") GlobalCfg.SourceHost = viper.GetString("podcast_copyright") GlobalCfg.SourceHost = viper.GetString("podcast_type") GlobalCfg.SourceHost = viper.GetString("podcast_image") GlobalCfg.SourceHost = viper.GetString("podcast_description") GlobalCfg.SourceHost = viper.GetString("podcast_items") } else { fmt.Printf("Error parsing config! - %v", err) os.Exit(1) } }