From f3490c65d7d7ee9d1eb7360a5ffdbca8a569c193 Mon Sep 17 00:00:00 2001 From: lgdd Date: Wed, 31 Jul 2024 20:22:49 +0200 Subject: [PATCH] feat: config supports setting using = --- internal/cmd/config/config.go | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/internal/cmd/config/config.go b/internal/cmd/config/config.go index 4f059be..e65ca5c 100644 --- a/internal/cmd/config/config.go +++ b/internal/cmd/config/config.go @@ -20,7 +20,13 @@ var ( if err := cobra.MaximumNArgs(2)(cmd, args); err != nil { return err } - if len(args) > 0 && !slices.Contains(viper.AllKeys(), args[0]) { + if len(args) == 1 && strings.Contains(args[0], "=") { + key := strings.Split(args[0], "=")[0] + if len(key) > 0 && !slices.Contains(viper.AllKeys(), key) { + return fmt.Errorf("invalid config key") + } + } + if len(args) > 1 && !slices.Contains(viper.AllKeys(), args[0]) { return fmt.Errorf("invalid config key") } return nil @@ -43,6 +49,9 @@ func run(cmd *cobra.Command, args []string) { } if len(args) == 1 { + if strings.Contains(args[0], "=") { + setKeyValue(args[0]) + } logger.Println(viper.GetString(args[0])) os.Exit(0) } @@ -65,3 +74,13 @@ func printConfKeyValues() { os.Exit(0) } } + +func setKeyValue(arg string) { + keyValue := strings.Split(arg, "=") + key := keyValue[0] + value := keyValue[1] + if len(key) > 0 && len(value) > 0 { + viper.Set(key, value) + viper.WriteConfig() + } +}