Skip to content

Commit

Permalink
refactor: refactor configuration handling and helper functions
Browse files Browse the repository at this point in the history
- Add available keys to the `config.go` file
- Replace a comment about a Git command with a more general one in the `helper.go` file
- Update Viper configuration values based on CLI flags in the `helper.go` file
- Check if the template file specified in the configuration exists in the `helper.go` file

Signed-off-by: Bo-Yi.Wu <appleboy.tw@gmail.com>
  • Loading branch information
appleboy committed Apr 15, 2023
1 parent 92b3564 commit 182cea6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
8 changes: 7 additions & 1 deletion cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ var availableKeys = []string{
"openai.timeout",
"openai.max_tokens",
"openai.temperature",

"openai.provider",
"openai.model_name",
}
Expand Down Expand Up @@ -74,18 +73,25 @@ var configCmd = &cobra.Command{
Short: "Add openai config (openai.api_key, openai.model ...)",
Args: cobra.MinimumNArgs(3),
RunE: func(cmd *cobra.Command, args []string) error {
// Check if command is 'set'
if args[0] != "set" {
return errors.New("config set key value. ex: config set openai.api_key sk-...")
}

// Check if key is available
if !array.InSlice(args[1], availableKeys) {
return errors.New("available key list: " + strings.Join(availableKeys, ", "))
}

// Set config value in viper
viper.Set(args[1], args[2])

// Write config to file
if err := viper.WriteConfig(); err != nil {
return err
}

// Print success message with config file location
color.Green("you can see the config file: %s", viper.ConfigFileUsed())
return nil
},
Expand Down
14 changes: 7 additions & 7 deletions cmd/hepler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"errors"
"fmt"

"github.com/appleboy/CodeGPT/openai"
"github.com/appleboy/CodeGPT/prompt"
Expand All @@ -12,11 +13,12 @@ import (
)

func check() error {
// check git command exist
// Check if the Git command is available on the system's PATH
if !util.IsCommandAvailable("git") {
return errors.New("Git command not found on your system's PATH. Please install Git and try again.")
}

// Update Viper configuration values based on the CLI flags
if diffUnified != 3 {
viper.Set("git.diff_unified", diffUnified)
}
Expand All @@ -25,12 +27,10 @@ func check() error {
viper.Set("git.exclude_list", excludeList)
}

// check default language
if prompt.GetLanguage(commitLang) != prompt.DefaultLanguage {
viper.Set("output.lang", commitLang)
}

// check default model
if openai.GetModel(commitModel) != openai.DefaultModel {
viper.Set("openai.model", commitModel)
}
Expand All @@ -55,10 +55,10 @@ func check() error {
viper.Set("git.template_string", templateString)
}

if viper.GetString("git.template_file") != "" {
if !file.IsFile(viper.GetString("git.template_file")) {
return errors.New("template file not found: " + viper.GetString("git.template_file"))
}
// Check if the template file specified in the configuration exists
templateFile := viper.GetString("git.template_file")
if templateFile != "" && !file.IsFile(templateFile) {
return fmt.Errorf("template file not found: %s", templateFile)
}

return nil
Expand Down

0 comments on commit 182cea6

Please sign in to comment.