Skip to content

Commit 359a48a

Browse files
committed
chore(cmd): Refactor commit message language and model handling
- Add `commitLang` and `commitModel` variables - Replace `commitCmd.PersistentFlags().StringP` with `commitCmd.PersistentFlags().StringVar` - Remove `commit.lang` and `commit.model` bindings - Use `prompt.GetLanguage` to check for default language - Use `openai.GetModel` to check for default model - Remove `color.Green("Summarize the commit message use " + viper.GetString("openai.model") + " model")` from line 49 - Set `viper.Set("output.lang", commitLang)` if `prompt.GetLanguage(commitLang) != prompt.DefaultLanguage` - Add `color.Green("Summarize the commit message use " + viper.GetString("openai.model") + " model")` to line 81 - Add error handling for translation in the `if` statement in lines 87-96
1 parent 0b255fe commit 359a48a

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

cmd/commit.go

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,15 @@ import (
1515
"github.com/spf13/viper"
1616
)
1717

18+
var (
19+
commitLang string
20+
commitModel string
21+
)
22+
1823
func init() {
1924
commitCmd.PersistentFlags().StringP("file", "f", ".git/COMMIT_EDITMSG", "commit message file")
20-
commitCmd.PersistentFlags().StringP("model", "m", "gpt-3.5-turbo", "openai model")
21-
commitCmd.PersistentFlags().StringP("lang", "l", "en", "summarizing language uses English by default")
22-
_ = viper.BindPFlag("commit.model", commitCmd.PersistentFlags().Lookup("model"))
23-
_ = viper.BindPFlag("commit.lang", commitCmd.PersistentFlags().Lookup("lang"))
25+
commitCmd.PersistentFlags().StringVar(&commitModel, "model", "gpt-3.5-turbo", "select openai model")
26+
commitCmd.PersistentFlags().StringVar(&commitLang, "lang", "en", "summarizing language uses English by default")
2427
_ = viper.BindPFlag("output.file", commitCmd.PersistentFlags().Lookup("file"))
2528
}
2629

@@ -40,12 +43,17 @@ var commitCmd = &cobra.Command{
4043
return err
4144
}
4245

43-
color.Green("Summarize the commit message use " + viper.GetString("openai.model") + " model")
46+
// check default language
47+
if prompt.GetLanguage(commitLang) != prompt.DefaultLanguage {
48+
viper.Set("output.lang", commitLang)
49+
}
4450

45-
if prompt.GetLanguage(viper.GetString("commit.lang")) != prompt.DefaultLanguage {
46-
viper.Set("output.lang", viper.GetString("commit.lang"))
51+
// check default model
52+
if openai.GetModel(commitModel) != openai.DefaultModel {
53+
viper.Set("openai.model", commitModel)
4754
}
4855

56+
color.Green("Summarize the commit message use " + viper.GetString("openai.model") + " model")
4957
client, err := openai.New(
5058
viper.GetString("openai.api_key"),
5159
viper.GetString("openai.model"),
@@ -104,7 +112,7 @@ var commitCmd = &cobra.Command{
104112
}
105113

106114
// translate a git commit message
107-
color.Cyan("We are trying to translate a git commit message to " + prompt.GetLanguage(viper.GetString("output.lang")) + "language")
115+
color.Cyan("We are trying to translate a git commit message to " + prompt.GetLanguage(viper.GetString("output.lang")) + " language")
108116
summarize, err := client.Completion(cmd.Context(), out)
109117
if err != nil {
110118
return err

0 commit comments

Comments
 (0)