From dafacdef5bd0af29836aa231d73f2d99de3561b2 Mon Sep 17 00:00:00 2001 From: Yusuf Can Bayrak Date: Fri, 1 Mar 2024 23:39:41 +0100 Subject: [PATCH] :wrench: individual config key set/get impl. --- config/cli.go | 71 +++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 63 insertions(+), 8 deletions(-) diff --git a/config/cli.go b/config/cli.go index d62c0a2..5f2781b 100644 --- a/config/cli.go +++ b/config/cli.go @@ -1,10 +1,12 @@ package config import ( + "errors" "fmt" "github.com/spf13/viper" "github.com/urfave/cli/v2" "github.com/yusufcanb/tlm/shell" + "net/url" ) func (c *Config) Action(_ *cli.Context) error { @@ -39,16 +41,69 @@ func (c *Config) Command() *cli.Command { return &cli.Command{ Name: "config", Aliases: []string{"c"}, - Usage: "configure preferences.", + Usage: "Configures tlm preferences.", Action: c.Action, Subcommands: []*cli.Command{ - { - Name: "set", - Usage: "set configuration", - Action: func(context *cli.Context) error { - return nil - }, - }, + c.subCommandGet(), + c.subCommandSet(), + }, + } +} + +func (c *Config) subCommandGet() *cli.Command { + return &cli.Command{ + Name: "get", + Usage: "get configuration by key", + UsageText: "tlm config get ", + Action: func(c *cli.Context) error { + key := c.Args().Get(0) + value := viper.GetString(key) + + if value == "" { + fmt.Println(fmt.Sprintf("%s <%s> is not a tlm parameter", shell.Err(), key)) + return nil + } + + fmt.Println(fmt.Sprintf("%s = %s", key, value)) + return nil + }, + } +} + +func (c *Config) subCommandSet() *cli.Command { + return &cli.Command{ + Name: "set", + Usage: "set configuration", + Action: func(c *cli.Context) error { + key := c.Args().Get(0) + + switch key { + case "llm.host": + u, err := url.ParseRequestURI(c.Args().Get(1)) + if err != nil { + return errors.New("Invalid url: " + c.Args().Get(1)) + } + viper.Set(key, u.String()) + + case "llm.suggest", "llm.explain": + mode := c.Args().Get(1) + if mode != "stable" && mode != "balanced" && mode != "creative" { + return errors.New("Invalid mode: " + mode) + } + viper.Set(key, mode) + default: + fmt.Println(fmt.Sprintf("%s <%s> is not a tlm parameter", shell.Err(), key)) + return nil + } + + viper.Set(key, c.Args().Get(1)) + err := viper.WriteConfig() + if err != nil { + return err + } + + fmt.Println(fmt.Sprintf("%s = %s %s", key, c.Args().Get(1), shell.Ok())) + return nil }, } }