76 lines
1.7 KiB
Go
76 lines
1.7 KiB
Go
package configcmd
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"git.yoresee.cc/nightstar/ry/internal/config"
|
|
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
func Cmd(cfg *config.Config, cfgPath string) *cobra.Command {
|
|
configCmd := &cobra.Command{
|
|
Use: "config",
|
|
Short: "Manage ry configuration",
|
|
}
|
|
|
|
configShowCmd := &cobra.Command{
|
|
Use: "show",
|
|
Short: "Show current configuration",
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
fmt.Printf("Config file: %s\n", cfgPath)
|
|
fmt.Printf("Current provider: %s\n", cfg.CurrentProvider)
|
|
fmt.Printf("Output format: %s\n", cfg.Output)
|
|
fmt.Println("Providers:")
|
|
for name, pc := range cfg.Providers {
|
|
masked := pc.APIKey
|
|
if len(masked) > 4 {
|
|
masked = masked[:2] + strings.Repeat("*", len(masked)-4) + masked[len(masked)-2:]
|
|
}
|
|
marker := " "
|
|
if name == cfg.CurrentProvider {
|
|
marker = "*"
|
|
}
|
|
fmt.Printf(" %s %s: api_key=%s\n", marker, name, masked)
|
|
}
|
|
return nil
|
|
},
|
|
}
|
|
|
|
configSetCmd := &cobra.Command{
|
|
Use: "set <key> <value>",
|
|
Short: "Set a config value",
|
|
Args: cobra.ExactArgs(2),
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
key, value := args[0], args[1]
|
|
switch key {
|
|
case "output":
|
|
cfg.Output = value
|
|
default:
|
|
return fmt.Errorf("unknown config key: %s", key)
|
|
}
|
|
if err := config.Save(cfgPath, cfg); err != nil {
|
|
return fmt.Errorf("save config: %w", err)
|
|
}
|
|
fmt.Printf("Set %s = %s\n", key, value)
|
|
return nil
|
|
},
|
|
}
|
|
|
|
configPathCmd := &cobra.Command{
|
|
Use: "path",
|
|
Short: "Print config file path",
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
fmt.Println(cfgPath)
|
|
return nil
|
|
},
|
|
}
|
|
|
|
configCmd.AddCommand(configShowCmd)
|
|
configCmd.AddCommand(configSetCmd)
|
|
configCmd.AddCommand(configPathCmd)
|
|
|
|
return configCmd
|
|
}
|