Last active
December 3, 2024 07:02
-
-
Save zoetrope/d14ee9bf37ba494c4280ba1d66b45c65 to your computer and use it in GitHub Desktop.
cobra sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"os" | |
"strings" | |
"time" | |
homedir "github.com/mitchellh/go-homedir" | |
"github.com/spf13/cobra" | |
"github.com/spf13/viper" | |
) | |
func main() { | |
Execute() | |
} | |
const ( | |
cfgAddress = "address" | |
cfgInterval = "interval" | |
cfgCount = "count" | |
cfgPort = "nested.port" | |
) | |
var cfgFile string | |
var config struct { | |
Address string | |
Interval time.Duration | |
Count int | |
Nested struct { | |
Port int | |
} | |
} | |
var rootCmd = &cobra.Command{ | |
Use: "cobra-123", | |
Run: func(cmd *cobra.Command, args []string) { | |
fmt.Printf("%#v\n", config) | |
}, | |
} | |
// 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) | |
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.cobra-123.yaml)") | |
rootCmd.PersistentFlags().String(cfgAddress, "", "The address") | |
rootCmd.PersistentFlags().Duration(cfgInterval, 1*time.Second, "The interval") | |
rootCmd.PersistentFlags().Int(cfgCount, 1000, "The count") | |
rootCmd.PersistentFlags().Int(cfgPort, 200, "The port") | |
//rootCmd.MarkPersistentFlagRequired(cfgAddress) | |
viper.BindPFlag(cfgAddress, rootCmd.PersistentFlags().Lookup(cfgAddress)) | |
viper.BindPFlag(cfgInterval, rootCmd.PersistentFlags().Lookup(cfgInterval)) | |
viper.BindPFlag(cfgCount, rootCmd.PersistentFlags().Lookup(cfgCount)) | |
viper.BindPFlag(cfgPort, rootCmd.PersistentFlags().Lookup(cfgPort)) | |
} | |
// initConfig reads in config file and ENV variables if set. | |
func initConfig() { | |
if cfgFile != "" { | |
// Use config file from the flag. | |
viper.SetConfigFile(cfgFile) | |
} else { | |
// Find home directory. | |
home, err := homedir.Dir() | |
if err != nil { | |
fmt.Println(err) | |
os.Exit(1) | |
} | |
// Search config in home directory with name ".zdb" (without extension). | |
viper.AddConfigPath(home) | |
viper.AddConfigPath(".") | |
viper.SetConfigName("sample") | |
viper.SetConfigType("yaml") | |
} | |
viper.AutomaticEnv() // read in environment variables that match | |
replacer := strings.NewReplacer("-", "_") | |
viper.SetEnvKeyReplacer(replacer) | |
// If a config file is found, read it in. | |
if err := viper.ReadInConfig(); err == nil { | |
fmt.Println("Using config file:", viper.ConfigFileUsed()) | |
} | |
if err := viper.Unmarshal(&config); err != nil { | |
fmt.Println(err) | |
os.Exit(1) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment