Skip to content

Commit e5e0f69

Browse files
authored
feat: refactor prompt folder handling and update its configuration key (#228)
- Add detailed comments to the `initConfig` function to describe its steps and purpose - Replace existing logic for handling the prompt folder with a switch-case structure - Add `prompt.folder` configuration key to the list of available keys - Add `prompt.folder` flag to `configSetCmd` - Update the configuration key `prompt_folder` to `prompt.folder` in `helper.go` - Enhance `prompt.go` to display the prompt folder path and improve confirmation prompt message Signed-off-by: appleboy <[email protected]>
1 parent fb64e0c commit e5e0f69

File tree

5 files changed

+70
-27
lines changed

5 files changed

+70
-27
lines changed

cmd/cmd.go

Lines changed: 62 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,25 @@ func init() {
4848
rootCmd.CompletionOptions.HiddenDefaultCmd = true
4949
}
5050

51+
// initConfig initializes the configuration for the application.
52+
// It sets up the configuration file, environment variables, and prompt folder.
53+
//
54+
// If a configuration file is specified by the cfgFile variable, it uses that file.
55+
// If the file does not exist, it creates a new one.
56+
// If no configuration file is specified, it searches for a configuration file
57+
// named ".codegpt.yaml" in the user's home directory under ".config/codegpt".
58+
//
59+
// The function also sets up environment variable handling to support multiple
60+
// CI/CD platforms, such as GitHub Actions and Drone CI, by setting the appropriate
61+
// environment variable prefixes.
62+
//
63+
// Additionally, it ensures that the prompt folder is correctly set up. If a prompt
64+
// folder is specified by the promptFolder variable, it verifies that it is a directory
65+
// and creates it if it does not exist. If no prompt folder is specified, it defaults
66+
// to a "prompt" directory under the ".config/codegpt" directory in the user's home.
67+
//
68+
// The function uses the Viper library for configuration management and the Cobra
69+
// library for error handling.
5170
func initConfig() {
5271
if cfgFile != "" {
5372
// Use config file from the flag.
@@ -78,30 +97,6 @@ func initConfig() {
7897
}
7998
}
8099

81-
if promptFolder != "" {
82-
viper.Set("prompt_folder", promptFolder)
83-
if file.IsFile(promptFolder) {
84-
log.Fatalf("prompt folder %s is a file", promptFolder)
85-
}
86-
// create the prompt folder if it doesn't exist
87-
if !file.IsDir(promptFolder) {
88-
if err := os.MkdirAll(promptFolder, os.ModePerm); err != nil {
89-
log.Fatal(err)
90-
}
91-
}
92-
} else {
93-
// Find home directory.
94-
home, err := os.UserHomeDir()
95-
cobra.CheckErr(err)
96-
targetFolder := path.Join(home, ".config", "codegpt", "prompt")
97-
if !file.IsDir(targetFolder) {
98-
if err := os.MkdirAll(targetFolder, os.ModePerm); err != nil {
99-
log.Fatal(err)
100-
}
101-
}
102-
viper.Set("prompt_folder", targetFolder)
103-
}
104-
105100
viper.AutomaticEnv()
106101
viper.SetEnvKeyReplacer(replacer)
107102

@@ -127,6 +122,49 @@ func initConfig() {
127122
fmt.Fprintln(os.Stderr, err)
128123
}
129124
}
125+
126+
switch {
127+
case promptFolder != "":
128+
// If a prompt folder is specified by the promptFolder variable,
129+
// check if it is a file. If it is, log a fatal error.
130+
if file.IsFile(promptFolder) {
131+
log.Fatalf("prompt folder %s is a file", promptFolder)
132+
}
133+
// If the prompt folder does not exist, create it.
134+
if !file.IsDir(promptFolder) {
135+
if err := os.MkdirAll(promptFolder, os.ModePerm); err != nil {
136+
log.Fatal(err)
137+
}
138+
}
139+
// Set the prompt folder in the configuration.
140+
viper.Set("prompt.folder", promptFolder)
141+
case viper.GetString("prompt.folder") != "":
142+
// If the prompt folder is specified in the configuration,
143+
// retrieve it and check if it is a file. If it is, log a fatal error.
144+
promptFolder = viper.GetString("prompt.folder")
145+
if file.IsFile(promptFolder) {
146+
log.Fatalf("prompt folder %s is a file", promptFolder)
147+
}
148+
// If the prompt folder does not exist, create it.
149+
if !file.IsDir(promptFolder) {
150+
if err := os.MkdirAll(promptFolder, os.ModePerm); err != nil {
151+
log.Fatal(err)
152+
}
153+
}
154+
default:
155+
// If no prompt folder is specified, use the default prompt folder
156+
// under the ".config/codegpt" directory in the user's home directory.
157+
home, err := os.UserHomeDir()
158+
cobra.CheckErr(err)
159+
targetFolder := path.Join(home, ".config", "codegpt", "prompt")
160+
if !file.IsDir(targetFolder) {
161+
if err := os.MkdirAll(targetFolder, os.ModePerm); err != nil {
162+
log.Fatal(err)
163+
}
164+
}
165+
// Set the prompt folder in the configuration.
166+
viper.Set("prompt.folder", targetFolder)
167+
}
130168
}
131169

132170
func Execute(ctx context.Context) {

cmd/config_list.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ var availableKeys = map[string]string{
3636
"openai.top_p": "An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered.",
3737
"openai.frequency_penalty": "Number between 0.0 and 1.0 that penalizes new tokens based on their existing frequency in the text so far. Decreases the model's likelihood to repeat the same line verbatim.",
3838
"openai.presence_penalty": "Number between 0.0 and 1.0 that penalizes new tokens based on whether they appear in the text so far. Increases the model's likelihood to talk about new topics.",
39+
"prompt.folder": "prompt template folder",
3940
}
4041

4142
// configListCmd represents the command to list the configuration values.

cmd/config_set.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ func init() {
3232
configSetCmd.Flags().BoolP("skip_verify", "", false, availableKeys["openai.skip_verify"])
3333
configSetCmd.Flags().StringP("headers", "", "", availableKeys["openai.headers"])
3434
configSetCmd.Flags().StringP("api_version", "", "", availableKeys["openai.api_version"])
35+
configSetCmd.Flags().StringP("prompt_folder", "", "", availableKeys["prompt.folder"])
3536
_ = viper.BindPFlag("openai.base_url", configSetCmd.Flags().Lookup("base_url"))
3637
_ = viper.BindPFlag("openai.org_id", configSetCmd.Flags().Lookup("org_id"))
3738
_ = viper.BindPFlag("openai.api_key", configSetCmd.Flags().Lookup("api_key"))
@@ -50,6 +51,7 @@ func init() {
5051
_ = viper.BindPFlag("openai.skip_verify", configSetCmd.Flags().Lookup("skip_verify"))
5152
_ = viper.BindPFlag("openai.headers", configSetCmd.Flags().Lookup("headers"))
5253
_ = viper.BindPFlag("openai.api_version", configSetCmd.Flags().Lookup("api_version"))
54+
_ = viper.BindPFlag("prompt.folder", configSetCmd.Flags().Lookup("prompt_folder"))
5355
}
5456

5557
// configSetCmd updates the config value.

cmd/hepler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func check() error {
6666
}
6767

6868
// load custom prompt
69-
promptFolder := viper.GetString("prompt_folder")
69+
promptFolder := viper.GetString("prompt.folder")
7070
if promptFolder != "" {
7171
if err := util.LoadTemplatesFromDir(promptFolder); err != nil {
7272
return fmt.Errorf("failed to load custom prompt templates: %s", err)

cmd/prompt.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,14 @@ var promptCmd = &cobra.Command{
3939
return nil
4040
}
4141

42-
confirm, err := confirmation.New("Do you want to load default prompt data, will overwrite your data", confirmation.No).RunPrompt()
42+
folder := viper.GetString("prompt.folder")
43+
44+
color.Yellow("Prompt folder: %s", folder)
45+
confirm, err := confirmation.New("Do you want to load the default prompt data? This will overwrite your existing data.", confirmation.No).RunPrompt()
4346
if err != nil || !confirm {
4447
return err
4548
}
4649

47-
folder := viper.GetString("prompt_folder")
4850
for _, key := range defaultPromptDataKeys {
4951
if err := savePromptData(folder, key); err != nil {
5052
return err

0 commit comments

Comments
 (0)