gime/cmd/config.go

69 lines
1.4 KiB
Go

/*
Copyright © 2022 brian buller <brian@bullercodeworks.com>
*/
package cmd
import (
"fmt"
"sort"
"strings"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
// configCmd represents the config command
var configCmd = &cobra.Command{
Use: "config",
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,
}
func init() {
rootCmd.AddCommand(configCmd)
}
func opConfig(cmd *cobra.Command, args []string) error {
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.")
}
}
}
var settings []string
for k, v := range viper.AllSettings() {
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))
}
}
sort.Strings(settings)
fmt.Println("Configuration File:", viper.ConfigFileUsed())
for _, v := range settings {
fmt.Println(v)
}
return nil
}