/* Copyright © Brian Buller */ package cmd import ( "fmt" "strings" "github.com/spf13/cobra" "github.com/spf13/viper" ) // configCmd represents the config command var configCmd = &cobra.Command{ Use: "config", Short: "View/Edit Config", RunE: func(cmd *cobra.Command, args []string) error { setVal := func(k, v string) error { viper.Set(k, v) return viper.WriteConfig() } switch len(args) { case 0: // List all config keys all := viper.AllKeys() for i := range all { fmt.Printf("%s: %s\n", all[i], viper.Get(all[i])) } return nil case 1: k := args[0] if strings.Contains(k, "=") { // This is actually a set pts := strings.Split(k, "=") if viper.IsSet(pts[0]) { return setVal(pts[0], pts[1]) } else { return fmt.Errorf("no config value found for %s", pts[0]) } return nil } // Show a config key/value if viper.IsSet(k) { fmt.Printf("%s: %s\n", k, viper.Get(k)) } else { return fmt.Errorf("no config value found for %s", k) } return nil case 2: // Setting a config value if !viper.IsSet(args[0]) { return fmt.Errorf("no config value found for %s", args[0]) } return setVal(args[0], args[1]) } return nil }, } func init() { rootCmd.AddCommand(configCmd) }