Skip to content

Commit 15727d0

Browse files
committed
refactor: Use RunE for error handling in rootCmd
This commit changes the `Run` function of `rootCmd` to `RunE`. This allows for returning errors directly instead of using `cobra.CheckErr`, which improves error handling and makes the code more idiomatic for Cobra applications. Additionally, specific error messages are now returned using `errors.New` or `fmt.Errorf` for better clarity. --- {"auto-commit-msg":{"language":"rust","version":"0.4.0-dev","model":"gemini-2.5-flash-lite","response_time":1.67,"execution_time":1.71}}
1 parent 1c0c5df commit 15727d0

1 file changed

Lines changed: 17 additions & 14 deletions

File tree

cmd/root.go

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cmd
22

33
import (
44
"encoding/json"
5+
"errors"
56
"fmt"
67
"log"
78
"math"
@@ -51,7 +52,7 @@ var config Config
5152
var rootCmd = &cobra.Command{
5253
Use: "auto-commit-msg COMMIT_MSG_FILE",
5354
Short: "Generates a commit message from a git diff using AI",
54-
Run: func(cmd *cobra.Command, args []string) {
55+
RunE: func(cmd *cobra.Command, args []string) error {
5556
var executionTime time.Time
5657
if config.Trace {
5758
executionTime = time.Now()
@@ -73,21 +74,21 @@ var rootCmd = &cobra.Command{
7374
}
7475
if commitSource != "" {
7576
log.Printf("Commit source '%s' is not empty, skipping commit message generation\n", commitSource)
76-
return
77+
return nil
7778
}
7879

7980
cachedGitDiff, err := git.DiffCached()
8081
if err != nil {
81-
cobra.CheckErr(err)
82+
return err
8283
}
8384

8485
if cachedGitDiff == "" {
85-
cobra.CheckErr("git diff is empty")
86+
return errors.New("git diff is empty")
8687
}
8788

8889
stats, err := git.DiffCachedStats()
8990
if err != nil {
90-
cobra.CheckErr(err)
91+
return err
9192
}
9293

9394
totalChanges := stats.Insertions + stats.Deletions
@@ -96,26 +97,26 @@ var rootCmd = &cobra.Command{
9697
if totalChanges < totalChangesThreshold {
9798
model = config.Diff.ShortModel
9899
if model == "" {
99-
cobra.CheckErr("short_model cannot be empty")
100+
return errors.New("short_model cannot be empty")
100101
}
101102
log.Printf("git diff total changes %d under %d threshold, using model for short diffs: %s\n", totalChanges, totalChangesThreshold, model)
102103
} else {
103104
model = config.Diff.LongModel
104105
if model == "" {
105-
cobra.CheckErr("long_model cannot be empty")
106+
return errors.New("long_model cannot be empty")
106107
}
107108
log.Printf("git diff total changes %d over %d threshold, using model for long diffs: %s\n", totalChanges, totalChangesThreshold, model)
108109
}
109110
if config.Provider.ApiKey == "" {
110-
cobra.CheckErr("api_key environment variable name cannot be empty")
111+
return errors.New("api_key environment variable name cannot be empty")
111112
}
112113

113114
apiKey := os.Getenv(config.Provider.ApiKey)
114115
if apiKey == "" {
115-
cobra.CheckErr(fmt.Sprintf("environment variable %s is required", config.Provider.ApiKey))
116+
return fmt.Errorf("environment variable %s is required", config.Provider.ApiKey)
116117
}
117118
if config.Provider.BaseUrl == "" {
118-
cobra.CheckErr("base_url cannot be empty")
119+
return errors.New("base_url cannot be empty")
119120
}
120121

121122
client := openai.NewClient(config.Provider.BaseUrl, apiKey)
@@ -138,10 +139,10 @@ var rootCmd = &cobra.Command{
138139
responseDuration = time.Since(responseTime)
139140
}
140141
if err != nil {
141-
cobra.CheckErr(err)
142+
return err
142143
}
143144
if len(res.Choices) == 0 {
144-
cobra.CheckErr(fmt.Sprintf("expects response to include at least one choice: %+v", res))
145+
return fmt.Errorf("expects response to include at least one choice: %+v", res)
145146
}
146147

147148
commitMsg := res.Choices[0].Message.Content
@@ -158,7 +159,7 @@ var rootCmd = &cobra.Command{
158159

159160
traceJSON, err := json.Marshal(traceWrapper)
160161
if err != nil {
161-
cobra.CheckErr(err)
162+
return err
162163
}
163164

164165
commitMsg = fmt.Sprintf("%s\n---\n%s", commitMsg, traceJSON)
@@ -169,9 +170,11 @@ var rootCmd = &cobra.Command{
169170
} else {
170171
err = os.WriteFile(commitMsgFile, []byte(commitMsg), 0644)
171172
if err != nil {
172-
cobra.CheckErr(err)
173+
return err
173174
}
174175
}
176+
177+
return nil
175178
},
176179
}
177180

0 commit comments

Comments
 (0)