2022-01-19 18:56:31 +00:00
|
|
|
/*
|
2022-01-19 21:30:30 +00:00
|
|
|
Copyright © 2022 brian buller <brian@bullercodeworks.com>
|
2022-01-19 18:56:31 +00:00
|
|
|
|
|
|
|
*/
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-01-20 14:51:39 +00:00
|
|
|
"sort"
|
2022-01-21 14:40:44 +00:00
|
|
|
"strings"
|
2022-01-19 18:56:31 +00:00
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
2022-01-20 14:51:39 +00:00
|
|
|
"github.com/spf13/viper"
|
2022-01-19 18:56:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// configCmd represents the config command
|
|
|
|
var configCmd = &cobra.Command{
|
|
|
|
Use: "config",
|
2022-01-21 14:40:44 +00:00
|
|
|
Short: "Show or update configuration values",
|
|
|
|
Long: `To set values just list them in key=value format.
|
|
|
|
For example:
|
|
|
|
gime config copytags=true roundto=30m`,
|
|
|
|
RunE: opConfig,
|
2022-01-19 18:56:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rootCmd.AddCommand(configCmd)
|
2022-01-19 21:30:30 +00:00
|
|
|
}
|
2022-01-19 18:56:31 +00:00
|
|
|
|
2022-01-19 21:30:30 +00:00
|
|
|
func opConfig(cmd *cobra.Command, args []string) error {
|
2022-01-21 14:40:44 +00:00
|
|
|
updConfig := make(map[string]string)
|
|
|
|
if len(args) > 0 {
|
|
|
|
// We're setting arguments
|
|
|
|
for _, a := range args {
|
|
|
|
pts := strings.Split(a, "=")
|
|
|
|
if len(pts) == 2 {
|
|
|
|
updConfig[pts[0]] = pts[1]
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("Unable to parse config values.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-01-20 14:51:39 +00:00
|
|
|
var settings []string
|
|
|
|
for k, v := range viper.AllSettings() {
|
2022-01-21 14:40:44 +00:00
|
|
|
switch v.(type) {
|
|
|
|
case bool:
|
|
|
|
if nv, ok := updConfig[k]; ok {
|
|
|
|
v = nv == "true"
|
|
|
|
viper.Set(k, v)
|
|
|
|
viper.WriteConfig()
|
|
|
|
}
|
|
|
|
settings = append(settings, fmt.Sprintf("%s: %t", k, v))
|
|
|
|
default:
|
|
|
|
if nv, ok := updConfig[k]; ok {
|
|
|
|
v = nv
|
|
|
|
viper.Set(k, v)
|
|
|
|
viper.WriteConfig()
|
|
|
|
}
|
|
|
|
settings = append(settings, fmt.Sprintf("%s: %s", k, v))
|
|
|
|
}
|
2022-01-20 14:51:39 +00:00
|
|
|
}
|
|
|
|
sort.Strings(settings)
|
|
|
|
fmt.Println("Configuration File:", viper.ConfigFileUsed())
|
|
|
|
for _, v := range settings {
|
|
|
|
fmt.Println(v)
|
|
|
|
}
|
2022-01-19 21:30:30 +00:00
|
|
|
return nil
|
2022-01-19 18:56:31 +00:00
|
|
|
}
|