|
| 1 | +// File: cmd/config.go |
| 2 | +package cmd |
| 3 | + |
| 4 | +import ( |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "os/exec" |
| 8 | + "path/filepath" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "smartcommit/config" |
| 12 | + |
| 13 | + "github.com/spf13/cobra" |
| 14 | +) |
| 15 | + |
| 16 | +var ConfigCmd = &cobra.Command{ |
| 17 | + Use: "config", |
| 18 | + Short: "View or change smartcommit configuration", |
| 19 | +} |
| 20 | + |
| 21 | +var setCmd = &cobra.Command{ |
| 22 | + Use: "set <key> <value>", |
| 23 | + Short: "Set a configuration value", |
| 24 | + Args: cobra.ExactArgs(2), |
| 25 | + Run: func(cmd *cobra.Command, args []string) { |
| 26 | + key, value := args[0], args[1] |
| 27 | + cfg := config.LoadOrDefault() |
| 28 | + cfg.Set(key, value) |
| 29 | + if err := config.Save(cfg); err != nil { |
| 30 | + fmt.Println("❌ Failed to save config:", err) |
| 31 | + return |
| 32 | + } |
| 33 | + fmt.Println("✅ Config updated") |
| 34 | + }, |
| 35 | +} |
| 36 | + |
| 37 | +var showCmd = &cobra.Command{ |
| 38 | + Use: "show", |
| 39 | + Short: "Display current configuration", |
| 40 | + Run: func(cmd *cobra.Command, args []string) { |
| 41 | + cfg := config.LoadOrDefault() |
| 42 | + cfg.PrettyPrint() |
| 43 | + }, |
| 44 | +} |
| 45 | + |
| 46 | +var editCmd = &cobra.Command{ |
| 47 | + Use: "edit system_prompt", |
| 48 | + Short: "Edit the system prompt using your default editor", |
| 49 | + Args: cobra.ExactArgs(1), |
| 50 | + Run: func(cmd *cobra.Command, args []string) { |
| 51 | + if args[0] != "system_prompt" { |
| 52 | + fmt.Println("❌ Only 'system_prompt' can be edited via editor for now.") |
| 53 | + return |
| 54 | + } |
| 55 | + |
| 56 | + cfg := config.LoadOrDefault() |
| 57 | + tmpfile := filepath.Join(os.TempDir(), "smartcommit_system_prompt.txt") |
| 58 | + os.WriteFile(tmpfile, []byte(cfg.SystemPrompt), 0644) |
| 59 | + |
| 60 | + editor := os.Getenv("EDITOR") |
| 61 | + if editor == "" { |
| 62 | + editor = "vim" |
| 63 | + } |
| 64 | + |
| 65 | + execCmd := exec.Command(editor, tmpfile) |
| 66 | + execCmd.Stdin = os.Stdin |
| 67 | + execCmd.Stdout = os.Stdout |
| 68 | + execCmd.Stderr = os.Stderr |
| 69 | + execCmd.Run() |
| 70 | + |
| 71 | + updated, _ := os.ReadFile(tmpfile) |
| 72 | + cfg.SystemPrompt = strings.TrimSpace(string(updated)) |
| 73 | + if err := config.Save(cfg); err != nil { |
| 74 | + fmt.Println("❌ Failed to save:", err) |
| 75 | + return |
| 76 | + } |
| 77 | + fmt.Println("✅ Updated system prompt") |
| 78 | + }, |
| 79 | +} |
| 80 | + |
| 81 | +func init() { |
| 82 | + ConfigCmd.AddCommand(setCmd, showCmd, editCmd) |
| 83 | +} |
0 commit comments