Skip to content

Commit

Permalink
🔧 individual config key set/get impl.
Browse files Browse the repository at this point in the history
  • Loading branch information
yusufcanb committed Mar 1, 2024
1 parent 44faf51 commit dafacde
Showing 1 changed file with 63 additions and 8 deletions.
71 changes: 63 additions & 8 deletions config/cli.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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 <key>",
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
},
}
}

0 comments on commit dafacde

Please sign in to comment.