Skip to content

Commit ca86109

Browse files
committed
Update config command with show and edit subcommands
1 parent 5cb3488 commit ca86109

3 files changed

Lines changed: 51 additions & 45 deletions

File tree

cmd/config.go

Lines changed: 47 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,94 +4,96 @@ import (
44
"fmt"
55
"os"
66
"os/exec"
7+
"strings"
78

89
"smartcommit/config"
910

1011
"github.com/spf13/cobra"
1112
)
1213

13-
var ConfigCmd = &cobra.Command{
14+
var configCmd = &cobra.Command{
1415
Use: "config",
15-
Short: "View or change smartcommit configuration",
16-
Long: `Manage smartcommit configuration
16+
Short: "View or manage SmartCommit configuration",
17+
}
1718

18-
Examples:
19-
smartcommit config edit # Edit the system prompt (tone/style)
20-
smartcommit config show # View current configuration`,
19+
var showCmd = &cobra.Command{
20+
Use: "show",
21+
Short: "Show current configuration",
22+
Run: func(cmd *cobra.Command, args []string) {
23+
cfg := config.LoadOrDefault()
24+
fmt.Println("📁 Current Config:")
25+
fmt.Println(" Provider: ", cfg.Provider)
26+
fmt.Println(" Model: ", cfg.Model)
27+
fmt.Println(" Base URL: ", cfg.BaseURL)
28+
if cfg.APIKey != "" {
29+
fmt.Println(" API Key: ", mask(cfg.APIKey))
30+
}
31+
fmt.Println(" SystemPrompt: ", cfg.SystemPrompt)
32+
},
2133
}
2234

23-
var EditConfigCmd = &cobra.Command{
35+
var editCmd = &cobra.Command{
2436
Use: "edit",
25-
Short: "Edit system prompt in your preferred editor (e.g., vim, nano, code)",
37+
Short: "Edit the system prompt used for generation (in Vim)",
2638
Run: func(cmd *cobra.Command, args []string) {
2739
cfg := config.LoadOrDefault()
2840

2941
tmpFile, err := os.CreateTemp("", "smartcommit-prompt-*.txt")
3042
if err != nil {
31-
fmt.Println("❌ Failed to create temp file:", err)
43+
fmt.Println("❌ Could not create temp file:", err)
3244
return
3345
}
3446
defer os.Remove(tmpFile.Name())
3547

36-
// Write current prompt to temp file
48+
// Write current prompt
3749
_, _ = tmpFile.WriteString(cfg.SystemPrompt)
3850
tmpFile.Close()
3951

40-
// Open in default editor
4152
editor := os.Getenv("EDITOR")
4253
if editor == "" {
4354
editor = "vim"
4455
}
45-
cmdEdit := exec.Command(editor, tmpFile.Name())
46-
cmdEdit.Stdin = os.Stdin
47-
cmdEdit.Stdout = os.Stdout
48-
cmdEdit.Stderr = os.Stderr
4956

50-
if err := cmdEdit.Run(); err != nil {
51-
fmt.Println("❌ Failed to open editor:", err)
57+
editCmd := exec.Command(editor, tmpFile.Name())
58+
editCmd.Stdin = os.Stdin
59+
editCmd.Stdout = os.Stdout
60+
editCmd.Stderr = os.Stderr
61+
if err := editCmd.Run(); err != nil {
62+
fmt.Println("❌ Editor closed with error:", err)
5263
return
5364
}
5465

55-
// Read edited prompt
56-
editedBytes, err := os.ReadFile(tmpFile.Name())
66+
// Read new prompt
67+
newPromptBytes, err := os.ReadFile(tmpFile.Name())
5768
if err != nil {
58-
fmt.Println("❌ Failed to read edited prompt:", err)
69+
fmt.Println("❌ Could not read edited file:", err)
5970
return
6071
}
61-
62-
edited := string(editedBytes)
63-
if edited == "" {
64-
fmt.Println("⚠️ Prompt left empty. Aborting.")
72+
newPrompt := strings.TrimSpace(string(newPromptBytes))
73+
if newPrompt == "" {
74+
fmt.Println("⚠️ Prompt is empty — edit cancelled.")
6575
return
6676
}
6777

68-
cfg.SystemPrompt = edited
78+
cfg.SystemPrompt = newPrompt
6979
if err := config.Save(cfg); err != nil {
7080
fmt.Println("❌ Failed to save config:", err)
71-
} else {
72-
fmt.Println("✅ System prompt updated successfully.")
81+
return
7382
}
83+
fmt.Println("✅ System prompt updated.")
7484
},
7585
}
7686

7787

78-
var ShowConfigCmd = &cobra.Command{
79-
Use: "show",
80-
Short: "Show current config",
81-
Run: func(cmd *cobra.Command, args []string) {
82-
cfg := config.LoadOrDefault()
83-
fmt.Printf("\nSystem Prompt: %s\n", cfg.SystemPrompt)
84-
fmt.Printf("Provider: %s\n", cfg.Provider)
85-
fmt.Printf("Model: %s\n", cfg.Model)
86-
if cfg.APIKey != "" {
87-
fmt.Println("API Key: [set]")
88-
} else {
89-
fmt.Println("API Key: [not set]")
90-
}
91-
},
88+
func init() {
89+
configCmd.AddCommand(showCmd)
90+
configCmd.AddCommand(editCmd)
91+
rootCmd.AddCommand(configCmd)
9292
}
9393

94-
func init() {
95-
ConfigCmd.AddCommand(EditConfigCmd)
96-
ConfigCmd.AddCommand(ShowConfigCmd)
97-
}
94+
func mask(s string) string {
95+
if len(s) <= 6 {
96+
return "******"
97+
}
98+
return s[:3] + "..." + s[len(s)-3:]
99+
}

config/config.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,7 @@ func Save(cfg *Config) error {
5656
}
5757
return os.WriteFile(path, out, 0644)
5858
}
59+
func ConfigPath() string {
60+
dir, _ := os.UserConfigDir()
61+
return filepath.Join(dir, "smartcommit", "config.yaml")
62+
}

smartcommit

-512 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)