refactor: add error handling and refactor git utilities
- Enhance error checking in all commands using git.Run - Refactor git helper to return errors properly - Fix README.md filename typo - Update .gitignore with additional ignores - Add fallback for config home dir - Improve request handling in grok client
This commit is contained in:
parent
f1dc9b457d
commit
9bd3e1d00e
5
.gitignore
vendored
5
.gitignore
vendored
@ -3,4 +3,7 @@ build/
|
|||||||
grokkit
|
grokkit
|
||||||
*.bak
|
*.bak
|
||||||
*.log
|
*.log
|
||||||
*.tmp
|
*.tmp
|
||||||
|
.env
|
||||||
|
coverage.out
|
||||||
|
coverage.html
|
||||||
@ -15,7 +15,11 @@ var commitCmd = &cobra.Command{
|
|||||||
Use: "commit",
|
Use: "commit",
|
||||||
Short: "Generate message and commit staged changes",
|
Short: "Generate message and commit staged changes",
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
diff := git.Run([]string{"diff", "--cached", "--no-color"})
|
diff, err := git.Run([]string{"diff", "--cached", "--no-color"})
|
||||||
|
if err != nil {
|
||||||
|
color.Red("Failed to get staged changes: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
if diff == "" {
|
if diff == "" {
|
||||||
color.Yellow("No staged changes!")
|
color.Yellow("No staged changes!")
|
||||||
return
|
return
|
||||||
|
|||||||
@ -14,7 +14,11 @@ var commitMsgCmd = &cobra.Command{
|
|||||||
Use: "commit-msg",
|
Use: "commit-msg",
|
||||||
Short: "Generate conventional commit message from staged changes",
|
Short: "Generate conventional commit message from staged changes",
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
diff := git.Run([]string{"diff", "--cached", "--no-color"})
|
diff, err := git.Run([]string{"diff", "--cached", "--no-color"})
|
||||||
|
if err != nil {
|
||||||
|
color.Red("Failed to get staged changes: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
if diff == "" {
|
if diff == "" {
|
||||||
color.Yellow("No staged changes!")
|
color.Yellow("No staged changes!")
|
||||||
return
|
return
|
||||||
|
|||||||
16
cmd/edit.go
16
cmd/edit.go
@ -28,11 +28,18 @@ var editCmd = &cobra.Command{
|
|||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
original, _ := os.ReadFile(filePath)
|
original, err := os.ReadFile(filePath)
|
||||||
|
if err != nil {
|
||||||
|
color.Red("Failed to read file: %v", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
cleanedOriginal := removeLastModifiedComments(string(original))
|
cleanedOriginal := removeLastModifiedComments(string(original))
|
||||||
|
|
||||||
backupPath := filePath + ".bak"
|
backupPath := filePath + ".bak"
|
||||||
_ = os.WriteFile(backupPath, original, 0644)
|
if err := os.WriteFile(backupPath, original, 0644); err != nil {
|
||||||
|
color.Red("Failed to create backup: %v", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
client := grok.NewClient()
|
client := grok.NewClient()
|
||||||
messages := []map[string]string{
|
messages := []map[string]string{
|
||||||
@ -57,7 +64,10 @@ var editCmd = &cobra.Command{
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
_ = os.WriteFile(filePath, []byte(newContent), 0644)
|
if err := os.WriteFile(filePath, []byte(newContent), 0644); err != nil {
|
||||||
|
color.Red("Failed to write file: %v", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
color.Green("✅ Applied successfully! Backup: %s", backupPath)
|
color.Green("✅ Applied successfully! Backup: %s", backupPath)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|||||||
@ -12,7 +12,11 @@ var historyCmd = &cobra.Command{
|
|||||||
Use: "history",
|
Use: "history",
|
||||||
Short: "Summarize recent git history",
|
Short: "Summarize recent git history",
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
log := git.Run([]string{"log", "--oneline", "-10"})
|
log, err := git.Run([]string{"log", "--oneline", "-10"})
|
||||||
|
if err != nil {
|
||||||
|
color.Red("Failed to get git log: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
if log == "" {
|
if log == "" {
|
||||||
color.Yellow("No commits found.")
|
color.Yellow("No commits found.")
|
||||||
return
|
return
|
||||||
|
|||||||
@ -14,9 +14,13 @@ var prDescribeCmd = &cobra.Command{
|
|||||||
Use: "pr-describe",
|
Use: "pr-describe",
|
||||||
Short: "Generate full PR description from current branch",
|
Short: "Generate full PR description from current branch",
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
diff := git.Run([]string{"diff", "main..HEAD", "--no-color"})
|
diff, err := git.Run([]string{"diff", "main..HEAD", "--no-color"})
|
||||||
if diff == "" {
|
if err != nil || diff == "" {
|
||||||
diff = git.Run([]string{"diff", "origin/main..HEAD", "--no-color"})
|
diff, err = git.Run([]string{"diff", "origin/main..HEAD", "--no-color"})
|
||||||
|
if err != nil {
|
||||||
|
color.Red("Failed to get branch diff: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if diff == "" {
|
if diff == "" {
|
||||||
color.Yellow("No changes on this branch compared to main/origin/main.")
|
color.Yellow("No changes on this branch compared to main/origin/main.")
|
||||||
|
|||||||
@ -18,8 +18,16 @@ var reviewCmd = &cobra.Command{
|
|||||||
model := config.GetModel(modelFlag)
|
model := config.GetModel(modelFlag)
|
||||||
|
|
||||||
client := grok.NewClient()
|
client := grok.NewClient()
|
||||||
diff := git.Run([]string{"diff", "--no-color"})
|
diff, err := git.Run([]string{"diff", "--no-color"})
|
||||||
status := git.Run([]string{"status", "--short"})
|
if err != nil {
|
||||||
|
color.Red("Failed to get git diff: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
status, err := git.Run([]string{"status", "--short"})
|
||||||
|
if err != nil {
|
||||||
|
color.Red("Failed to get git status: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
messages := []map[string]string{
|
messages := []map[string]string{
|
||||||
{"role": "system", "content": "You are an expert code reviewer. Give a concise summary + 3-5 actionable improvements."},
|
{"role": "system", "content": "You are an expert code reviewer. Give a concise summary + 3-5 actionable improvements."},
|
||||||
|
|||||||
@ -8,7 +8,11 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func Load() {
|
func Load() {
|
||||||
home, _ := os.UserHomeDir()
|
home, err := os.UserHomeDir()
|
||||||
|
if err != nil {
|
||||||
|
// Fall back to current directory if home not found
|
||||||
|
home = "."
|
||||||
|
}
|
||||||
configPath := filepath.Join(home, ".config", "grokkit")
|
configPath := filepath.Join(home, ".config", "grokkit")
|
||||||
|
|
||||||
viper.SetConfigName("config")
|
viper.SetConfigName("config")
|
||||||
@ -20,6 +24,7 @@ func Load() {
|
|||||||
viper.SetDefault("default_model", "grok-4")
|
viper.SetDefault("default_model", "grok-4")
|
||||||
viper.SetDefault("temperature", 0.7)
|
viper.SetDefault("temperature", 0.7)
|
||||||
|
|
||||||
|
// Config file is optional, so we ignore read errors
|
||||||
_ = viper.ReadInConfig()
|
_ = viper.ReadInConfig()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
19
internal/git/git.go
Normal file
19
internal/git/git.go
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
package git
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os/exec"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Run(args []string) (string, error) {
|
||||||
|
out, err := exec.Command("git", args...).Output()
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("git command failed: %w", err)
|
||||||
|
}
|
||||||
|
return string(out), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func IsRepo() bool {
|
||||||
|
_, err := exec.Command("git", "rev-parse", "--is-inside-work-tree").Output()
|
||||||
|
return err == nil
|
||||||
|
}
|
||||||
@ -1,13 +0,0 @@
|
|||||||
package git
|
|
||||||
|
|
||||||
import "os/exec"
|
|
||||||
|
|
||||||
func Run(args []string) string {
|
|
||||||
out, _ := exec.Command("git", args...).Output()
|
|
||||||
return string(out)
|
|
||||||
}
|
|
||||||
|
|
||||||
func IsRepo() bool {
|
|
||||||
_, err := exec.Command("git", "rev-parse", "--is-inside-work-tree").Output()
|
|
||||||
return err == nil
|
|
||||||
}
|
|
||||||
@ -49,8 +49,16 @@ func (c *Client) streamInternal(messages []map[string]string, model string, prin
|
|||||||
"stream": true,
|
"stream": true,
|
||||||
}
|
}
|
||||||
|
|
||||||
body, _ := json.Marshal(payload)
|
body, err := json.Marshal(payload)
|
||||||
req, _ := http.NewRequest("POST", url, bytes.NewReader(body))
|
if err != nil {
|
||||||
|
color.Red("Failed to marshal request: %v", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
req, err := http.NewRequest("POST", url, bytes.NewReader(body))
|
||||||
|
if err != nil {
|
||||||
|
color.Red("Failed to create request: %v", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
req.Header.Set("Authorization", "Bearer "+c.APIKey)
|
req.Header.Set("Authorization", "Bearer "+c.APIKey)
|
||||||
req.Header.Set("Content-Type", "application/json")
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user