Skip to content

Commit

Permalink
feat: config supports setting using =
Browse files Browse the repository at this point in the history
  • Loading branch information
lgdd committed Jul 31, 2024
1 parent fc8d1c4 commit f3490c6
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion internal/cmd/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
Expand All @@ -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()
}
}

0 comments on commit f3490c6

Please sign in to comment.