Split config setting and getting into separate functions

This commit is contained in:
Juan Carlos Mejías Rodríguez 2019-07-23 21:59:08 -04:00
parent 143215b39f
commit 53d26a8ac3

View File

@ -29,9 +29,24 @@ var configCmd = &cobra.Command{
log.Fatalf("Unkonwn configuration key \"%s\"", args[0]) log.Fatalf("Unkonwn configuration key \"%s\"", args[0])
} }
// Create new viper if len(args) == 1 {
commandViper := viper.New() // Get config
value, configGettingErr := getConfig(args[0])
common.CheckError(configGettingErr)
fmt.Println(value)
} else {
// Set config
configSettingErr := setConfig(args[0], args[1])
common.CheckError(configSettingErr)
}
},
}
func init() {
rootCmd.AddCommand(configCmd)
}
func loadCofig () (*viper.Viper, error) {
// Set config file name // Set config file name
var configFile string var configFile string
if viper.ConfigFileUsed() != "" { if viper.ConfigFileUsed() != "" {
@ -41,42 +56,51 @@ var configCmd = &cobra.Command{
// Find home directory // Find home directory
home, err := homedir.Dir() home, err := homedir.Dir()
if err != nil { if err != nil {
fmt.Println(err) return &viper.Viper{}, err
os.Exit(1)
} }
// Use $HOME/.psu.yaml // Use $HOME/.psu.yaml
configFile = fmt.Sprintf("%s%s.psu.yaml", home, string(os.PathSeparator)) configFile = fmt.Sprintf("%s%s.psu.yaml", home, string(os.PathSeparator))
} }
commandViper.SetConfigFile(configFile) newViper := viper.New()
newViper.SetConfigFile(configFile)
// Read config from file // Read config from file
if configReadingErr := commandViper.ReadInConfig(); configReadingErr != nil { if configReadingErr := newViper.ReadInConfig(); configReadingErr != nil {
common.PrintVerbose(fmt.Sprintf("Could not read configuration from \"%s\". Expect all configuration values to be unset.", configFile)) common.PrintVerbose(fmt.Sprintf("Could not read configuration from \"%s\". Expect all configuration values to be unset.", configFile))
} }
if len(args) == 1 { return newViper, nil
// Get config }
fmt.Println(commandViper.Get(args[0]))
} else { func getConfig(key string) (interface{}, error) {
// Set config newViper, configLoadingErr := loadCofig ()
commandViper.Set(args[0], args[1]) if configLoadingErr != nil {
return nil, configLoadingErr
}
return newViper.Get(key), nil
}
func setConfig(key string, value string) error {
newViper, configLoadingErr := loadCofig ()
if configLoadingErr != nil {
return configLoadingErr
}
newViper.Set(key, value)
// Make sure the config file exists // Make sure the config file exists
_, fileCreationErr := os.Create(configFile) _, fileCreationErr := os.Create(newViper.ConfigFileUsed())
if fileCreationErr != nil { if fileCreationErr != nil {
common.CheckError(fileCreationErr) return fileCreationErr
} }
// Write te config file // Write te config file
configWritingErr := commandViper.WriteConfig() configWritingErr := newViper.WriteConfig()
if configWritingErr != nil { if configWritingErr != nil {
common.CheckError(configWritingErr) return configWritingErr
}
}
},
} }
func init() { return nil
rootCmd.AddCommand(configCmd)
} }