Last active
August 29, 2015 14:26
-
-
Save jmsdnns/e3a3ddee75045c0095c4 to your computer and use it in GitHub Desktop.
Trying out Cobra and Viper for Go
This file contains 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 ( | |
"encoding/json" | |
"fmt" | |
"io/ioutil" | |
"os" | |
"reflect" | |
"github.com/spf13/cobra" | |
"github.com/spf13/viper" | |
) | |
var configFile string | |
var someFloat64 float64 | |
var someNumber int | |
var someString string | |
// Main command | |
var TestCmd = &cobra.Command{ | |
Use: "testviper", | |
Short: "explore how viper & cobra works", | |
Long: `HACK THE PLANET HACK THE PLANET HACK THE PLANET`, | |
Run: func(cmd *cobra.Command, args []string) { | |
InitializeConfig() | |
}, | |
} | |
// Variable used to get around initialization loop | |
var testCmdV *cobra.Command | |
func InitializeDefaultSettings() { | |
fmt.Println("InitializeDefaultSettings") | |
// Command line config should inform Viper defaults when possible | |
float64Flag, _ := testCmdV.PersistentFlags().GetFloat64("float") | |
numberFlag, _ := testCmdV.PersistentFlags().GetInt("number") | |
stringFlag, _ := testCmdV.PersistentFlags().GetString("string") | |
fmt.Println("SomeFloat", reflect.TypeOf(float64Flag)) | |
fmt.Println("SomeNumber", reflect.TypeOf(numberFlag)) | |
fmt.Println("SomeString", reflect.TypeOf(stringFlag)) | |
viper.SetDefault("SomeFloat", float64Flag) | |
viper.SetDefault("SomeNumber", numberFlag) | |
viper.SetDefault("SomeString", stringFlag) | |
} | |
// Interesting that Viper has facilities for reading but none for writing | |
func CreateConfig() error { | |
j, jerr := json.MarshalIndent(viper.AllSettings(), "", " ") | |
if jerr != nil { | |
fmt.Println("jerr:", jerr.Error()) | |
return jerr | |
} | |
err := ioutil.WriteFile(configFile, j, 0644) | |
if err != nil { | |
fmt.Println("ERROR: ", err) | |
return err | |
} | |
return nil | |
} | |
func InitializeConfig() { | |
// Initialize defaults | |
InitializeDefaultSettings() | |
// Read default from config | |
viper.SetConfigFile(configFile) | |
err := viper.ReadInConfig() | |
if err != nil { | |
// No config. Create default | |
cErr := CreateConfig() | |
if cErr != nil { | |
// Couldn't write config. Bail. | |
os.Exit(-1) | |
} | |
} | |
// Check for command line overrides of config | |
if testCmdV.PersistentFlags().Lookup("float").Changed { | |
viper.Set("SomeFloat", someFloat64) | |
} | |
if testCmdV.PersistentFlags().Lookup("number").Changed { | |
viper.Set("SomeNumber", someNumber) | |
} | |
if testCmdV.PersistentFlags().Lookup("string").Changed { | |
viper.Set("SomeString", someString) | |
} | |
} | |
func init() { | |
TestCmd.PersistentFlags().Float64VarP(&someFloat64, "float", "f", 42, "just some float64") | |
TestCmd.PersistentFlags().IntVarP(&someNumber, "number", "n", 42, "just some number") | |
TestCmd.PersistentFlags().StringVarP(&someString, "string", "s", "weeee", "just some string") | |
TestCmd.PersistentFlags().StringVarP(&configFile, "config", "c", "config.json", "filename for storing config as json") | |
testCmdV = TestCmd | |
} | |
func main() { | |
TestCmd.Execute() | |
fmt.Println("SomeFloat", reflect.TypeOf(viper.Get("SomeFloat"))) | |
fmt.Println("SomeNumber", reflect.TypeOf(viper.GetInt("SomeNumber"))) | |
fmt.Println("SomeString", reflect.TypeOf(viper.Get("SomeString"))) | |
fmt.Printf("Float64: %f\n", viper.Get("SomeFloat")) | |
fmt.Printf("Number: %d\n", viper.GetInt("SomeNumber")) | |
fmt.Printf("String: %s\n", viper.Get("SomeString")) | |
fmt.Printf("Config: %s\n", configFile) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment