package config import ( "github.com/spf13/cobra" "github.com/spf13/viper" ) type Config struct { API APIConfig Web WebConfig } type APIConfig struct { Port int } type WebConfig struct { Port int } func GetConfig() (cfg Config, err error) { return cfg, viper.Unmarshal(&cfg) } // RegisterFlags adds persistent flags necessary to the application to the specified *cobra.Command func RegisterFlags(cmd *cobra.Command) error { // api.port ; --api-port cmd.PersistentFlags().Int("api-port", 3182, "API server port") if err := viper.BindPFlag("api.port", cmd.PersistentFlags().Lookup("api-port")); err != nil { return err } // web.port ; --web-port cmd.PersistentFlags().Int("web-port", 3183, "Webserver port") if err := viper.BindPFlag("web.port", cmd.PersistentFlags().Lookup("web-port")); err != nil { return err } return nil }