Skip to content

Commit

Permalink
feat: improve template variables handling and testing (#80)
Browse files Browse the repository at this point in the history
 - Add "github.com/joho/godotenv" to go.mod
 - Introduce templateVarsFile for loading variables from a file
 - Add error checking for template variables file in check() function
 - Modify commitCmd to support loading variables from a file using godotenv
 - Add new test files: commit_message.tpl and commit_vars.env

ref #72
  • Loading branch information
appleboy authored May 20, 2023
1 parent 61b2777 commit 4b2d053
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 4 deletions.
26 changes: 22 additions & 4 deletions cmd/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/appleboy/CodeGPT/util"

"github.com/fatih/color"
"github.com/joho/godotenv"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
Expand All @@ -29,9 +30,11 @@ var (
socksProxy string
templateFile string
templateString string
templateVars []string
commitAmend bool
timeout time.Duration

templateVars []string
templateVarsFile string
)

func init() {
Expand All @@ -46,6 +49,7 @@ func init() {
commitCmd.PersistentFlags().StringVar(&templateFile, "template_file", "", "git commit message file")
commitCmd.PersistentFlags().StringVar(&templateString, "template_string", "", "git commit message string")
commitCmd.PersistentFlags().StringSliceVar(&templateVars, "template_vars", []string{}, "template variables")
commitCmd.PersistentFlags().StringVar(&templateVarsFile, "template_vars_file", "", "template variables file")
commitCmd.PersistentFlags().BoolVar(&commitAmend, "amend", false, "replace the tip of the current branch by creating a new commit.")
commitCmd.PersistentFlags().DurationVarP(&timeout, "timeout", "t", 10*time.Second, "http timeout")
_ = viper.BindPFlag("output.file", commitCmd.PersistentFlags().Lookup("file"))
Expand Down Expand Up @@ -171,9 +175,23 @@ var commitCmd = &cobra.Command{
"summarize_title": strings.TrimSpace(summarizeTitle),
"summarize_message": strings.TrimSpace(summarizeMessage),
}
vars := util.ConvertToMap(templateVars)
for k, v := range vars {
data[k] = v

// add template vars
if vars := util.ConvertToMap(templateVars); len(vars) > 0 {
for k, v := range vars {
data[k] = v
}
}

// add template vars from file
if templateVarsFile != "" {
allENV, err := godotenv.Read(templateVarsFile)
if err != nil {
return err
}
for k, v := range allENV {
data[k] = v
}
}

if viper.GetString("git.template_file") != "" {
Expand Down
4 changes: 4 additions & 0 deletions cmd/hepler.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,9 @@ func check() error {
return fmt.Errorf("template file not found: %s", templateFile)
}

if templateVarsFile != "" && !file.IsFile(templateVarsFile) {
return fmt.Errorf("template variables file not found: %s", templateVarsFile)
}

return nil
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.20
require (
github.com/appleboy/com v0.1.7
github.com/fatih/color v1.15.0
github.com/joho/godotenv v1.5.1
github.com/sashabaranov/go-openai v1.9.4
github.com/spf13/cobra v1.7.0
github.com/spf13/viper v1.15.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
Expand Down
5 changes: 5 additions & 0 deletions tests/commit_message.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{{ .summarize_prefix }}: {{ .summarize_title }}

{{ .summarize_message }}

{{ if .JIRA_URL }}{{ .JIRA_URL }}{{ end }}
1 change: 1 addition & 0 deletions tests/commit_vars.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
JIRA_URL=http://example.com/

0 comments on commit 4b2d053

Please sign in to comment.