mirror of
https://gitlab.com/psuapp/psu.git
synced 2024-08-30 18:12:34 +00:00
Split config setting and getting in two new commands config set and config get
This commit is contained in:
parent
648cd6651a
commit
49367f2a11
101
cmd/config.go
101
cmd/config.go
@ -1,112 +1,15 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/greenled/portainer-stack-utils/common"
|
||||
"github.com/mitchellh/go-homedir"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
// configCmd represents the config command
|
||||
var configCmd = &cobra.Command{
|
||||
Use: "config KEY [VALUE]",
|
||||
Short: "Get and set configuration options",
|
||||
Example: "psu config user admin",
|
||||
Args: cobra.MinimumNArgs(1),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
// Check if it's a valid key
|
||||
var keyExists bool
|
||||
for _, key := range viper.AllKeys() {
|
||||
if key == args[0] {
|
||||
keyExists = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !keyExists {
|
||||
logrus.WithFields(logrus.Fields{
|
||||
"key": args[0],
|
||||
"suggestions": "Try looking up the available configuration keys: psu config ls",
|
||||
}).Fatal("Unknown configuration key")
|
||||
}
|
||||
|
||||
if len(args) == 1 {
|
||||
// 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)
|
||||
}
|
||||
},
|
||||
Use: "config",
|
||||
Short: "Manage configs",
|
||||
}
|
||||
|
||||
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 {
|
||||
logrus.WithFields(logrus.Fields{
|
||||
"file": configFile,
|
||||
}).Warn("Could not read configuration from file. Expect all configuration values to be unset.")
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
44
cmd/configGet.go
Normal file
44
cmd/configGet.go
Normal file
@ -0,0 +1,44 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/greenled/portainer-stack-utils/common"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// configGetCmd represents the config get command
|
||||
var configGetCmd = &cobra.Command{
|
||||
Use: "get KEY",
|
||||
Short: "Get config",
|
||||
Args: cobra.ExactArgs(1),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
keyExists := common.CheckConfigKeyExists(args[0])
|
||||
if !keyExists {
|
||||
logrus.WithFields(logrus.Fields{
|
||||
"key": args[0],
|
||||
"suggestions": "Try looking up the available configuration keys: psu config ls",
|
||||
}).Fatal("Unknown configuration key")
|
||||
}
|
||||
|
||||
// Get config
|
||||
value, configGettingErr := getConfig(args[0])
|
||||
common.CheckError(configGettingErr)
|
||||
fmt.Println(value)
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
configCmd.AddCommand(configGetCmd)
|
||||
}
|
||||
|
||||
func getConfig(key string) (value interface{}, err error) {
|
||||
newViper, err := common.LoadCofig()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
value = newViper.Get(key)
|
||||
|
||||
return
|
||||
}
|
55
cmd/configSet.go
Normal file
55
cmd/configSet.go
Normal file
@ -0,0 +1,55 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
|
||||
"github.com/greenled/portainer-stack-utils/common"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// configSetCmd represents the config set command
|
||||
var configSetCmd = &cobra.Command{
|
||||
Use: "set KEY VALUE",
|
||||
Short: "Set config",
|
||||
Args: cobra.ExactArgs(2),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
keyExists := common.CheckConfigKeyExists(args[0])
|
||||
if !keyExists {
|
||||
logrus.WithFields(logrus.Fields{
|
||||
"key": args[0],
|
||||
"suggestions": "Try looking up the available configuration keys: psu config ls",
|
||||
}).Fatal("Unknown configuration key")
|
||||
}
|
||||
|
||||
// Set config
|
||||
configSettingErr := setConfig(args[0], args[1])
|
||||
common.CheckError(configSettingErr)
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
configCmd.AddCommand(configSetCmd)
|
||||
}
|
||||
|
||||
func setConfig(key string, value string) (err error) {
|
||||
newViper, err := common.LoadCofig()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
newViper.Set(key, value)
|
||||
|
||||
// Make sure the config file exists
|
||||
_, err = os.Create(newViper.ConfigFileUsed())
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// Write te config file
|
||||
err = newViper.WriteConfig()
|
||||
|
||||
return
|
||||
}
|
45
common/configs.go
Normal file
45
common/configs.go
Normal file
@ -0,0 +1,45 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/mitchellh/go-homedir"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
func LoadCofig() (v *viper.Viper, err error) {
|
||||
// Set config file name
|
||||
var configFile string
|
||||
if viper.ConfigFileUsed() != "" {
|
||||
// Use config file from viper
|
||||
configFile = viper.ConfigFileUsed()
|
||||
} else {
|
||||
// Find home directory
|
||||
var home string
|
||||
home, err = homedir.Dir()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// Use $HOME/.psu.yaml
|
||||
configFile = fmt.Sprintf("%s%s.psu.yaml", home, string(os.PathSeparator))
|
||||
}
|
||||
v = viper.New()
|
||||
v.SetConfigFile(configFile)
|
||||
|
||||
// Read config from file
|
||||
err = v.ReadInConfig()
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func CheckConfigKeyExists(key string) (keyExists bool) {
|
||||
for _, k := range viper.AllKeys() {
|
||||
if k == key {
|
||||
keyExists = true
|
||||
break
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
Loading…
Reference in New Issue
Block a user