Skip to content

Commit ebbd68c

Browse files
committed
refactor: improve config validation
This commit refactors the configuration validation logic in the `initConfig` function. Previously, the validation for `short_model` and `long_model` was done within the `rootCmd.Run` function, which meant that errors would only be surfaced when the diff command was actually executed. This change moves the validation to the `initConfig` function, ensuring that configuration errors related to empty `short_model` or `long_model`, or a negative `threshold`, are caught earlier during the initialization phase. This provides a better user experience by surfacing configuration issues immediately upon command execution, rather than during the processing of diffs. Additionally, it simplifies the logic by removing redundant checks within the `rootCmd.Run` function. The validation for `short_model` and `long_model` is now handled solely in `initConfig`, and the `threshold` validation is also added there. --- {"auto-commit-msg":{"language":"rust","version":"0.4.0-dev","model":"gemini-2.5-flash-lite","response_time":2.11,"execution_time":2.15}}
1 parent 3e1c2bb commit ebbd68c

1 file changed

Lines changed: 15 additions & 6 deletions

File tree

cmd/root.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,15 +97,9 @@ var rootCmd = &cobra.Command{
9797
var model string
9898
if totalChanges < totalChangesThreshold {
9999
model = config.Diff.ShortModel
100-
if model == "" {
101-
return errors.New("short_model cannot be empty")
102-
}
103100
log.Printf("git diff total changes %d under %d threshold, using model for short diffs: %s\n", totalChanges, totalChangesThreshold, model)
104101
} else {
105102
model = config.Diff.LongModel
106-
if model == "" {
107-
return errors.New("long_model cannot be empty")
108-
}
109103
log.Printf("git diff total changes %d over %d threshold, using model for long diffs: %s\n", totalChanges, totalChangesThreshold, model)
110104
}
111105
if config.Provider.ApiKey == "" {
@@ -229,4 +223,19 @@ func initConfig() {
229223
if err := viper.Unmarshal(&config); err != nil {
230224
cobra.CheckErr(err)
231225
}
226+
227+
shortModel := config.Diff.ShortModel
228+
if shortModel == "" {
229+
cobra.CheckErr("diff.short_model cannot be empty")
230+
}
231+
232+
longModel := config.Diff.LongModel
233+
if longModel == "" {
234+
cobra.CheckErr("diff.long_model cannot be empty")
235+
}
236+
237+
threshold := config.Diff.Threshold
238+
if threshold < 0 {
239+
cobra.CheckErr("diff.threshold should be greater than 0")
240+
}
232241
}

0 commit comments

Comments
 (0)