mirror of
https://gitlab.com/psuapp/psu.git
synced 2024-08-30 18:12:34 +00:00
Split config setting and getting into separate functions
This commit is contained in:
parent
143215b39f
commit
53d26a8ac3
104
cmd/config.go
104
cmd/config.go
@ -29,50 +29,15 @@ var configCmd = &cobra.Command{
|
||||
log.Fatalf("Unkonwn configuration key \"%s\"", args[0])
|
||||
}
|
||||
|
||||
// Create new viper
|
||||
commandViper := viper.New()
|
||||
|
||||
// Set config file name
|
||||
var configFile string
|
||||
if viper.ConfigFileUsed() != "" {
|
||||
// Use config file from viper
|
||||
configFile = viper.ConfigFileUsed()
|
||||
} else {
|
||||
// Find home directory
|
||||
home, err := homedir.Dir()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Use $HOME/.psu.yaml
|
||||
configFile = fmt.Sprintf("%s%s.psu.yaml", home, string(os.PathSeparator))
|
||||
}
|
||||
commandViper.SetConfigFile(configFile)
|
||||
|
||||
// Read config from file
|
||||
if configReadingErr := commandViper.ReadInConfig(); configReadingErr != nil {
|
||||
common.PrintVerbose(fmt.Sprintf("Could not read configuration from \"%s\". Expect all configuration values to be unset.", configFile))
|
||||
}
|
||||
|
||||
if len(args) == 1 {
|
||||
// Get config
|
||||
fmt.Println(commandViper.Get(args[0]))
|
||||
value, configGettingErr := getConfig(args[0])
|
||||
common.CheckError(configGettingErr)
|
||||
fmt.Println(value)
|
||||
} else {
|
||||
// Set config
|
||||
commandViper.Set(args[0], args[1])
|
||||
|
||||
// Make sure the config file exists
|
||||
_, fileCreationErr := os.Create(configFile)
|
||||
if fileCreationErr != nil {
|
||||
common.CheckError(fileCreationErr)
|
||||
}
|
||||
|
||||
// Write te config file
|
||||
configWritingErr := commandViper.WriteConfig()
|
||||
if configWritingErr != nil {
|
||||
common.CheckError(configWritingErr)
|
||||
}
|
||||
configSettingErr := setConfig(args[0], args[1])
|
||||
common.CheckError(configSettingErr)
|
||||
}
|
||||
},
|
||||
}
|
||||
@ -80,3 +45,62 @@ var configCmd = &cobra.Command{
|
||||
func init() {
|
||||
rootCmd.AddCommand(configCmd)
|
||||
}
|
||||
|
||||
func loadCofig () (*viper.Viper, error) {
|
||||
// Set config file name
|
||||
var configFile string
|
||||
if viper.ConfigFileUsed() != "" {
|
||||
// Use config file from viper
|
||||
configFile = viper.ConfigFileUsed()
|
||||
} else {
|
||||
// Find home directory
|
||||
home, err := homedir.Dir()
|
||||
if err != nil {
|
||||
return &viper.Viper{}, err
|
||||
}
|
||||
|
||||
// Use $HOME/.psu.yaml
|
||||
configFile = fmt.Sprintf("%s%s.psu.yaml", home, string(os.PathSeparator))
|
||||
}
|
||||
newViper := viper.New()
|
||||
newViper.SetConfigFile(configFile)
|
||||
|
||||
// Read config from file
|
||||
if configReadingErr := newViper.ReadInConfig(); configReadingErr != nil {
|
||||
common.PrintVerbose(fmt.Sprintf("Could not read configuration from \"%s\". Expect all configuration values to be unset.", configFile))
|
||||
}
|
||||
|
||||
return newViper, nil
|
||||
}
|
||||
|
||||
func getConfig(key string) (interface{}, error) {
|
||||
newViper, configLoadingErr := loadCofig ()
|
||||
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
|
||||
_, fileCreationErr := os.Create(newViper.ConfigFileUsed())
|
||||
if fileCreationErr != nil {
|
||||
return fileCreationErr
|
||||
}
|
||||
|
||||
// Write te config file
|
||||
configWritingErr := newViper.WriteConfig()
|
||||
if configWritingErr != nil {
|
||||
return configWritingErr
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user