Skip to content

Commit bf89c73

Browse files
committed
refactor: refactor prompt handling logic and improve code clarity
- Add detailed comments explaining the `promptCmd` command functionality - Refactor the conditional check for `loadPromptData` to return early if not set - Replace variable `change` with `confirm` for clarity - Extract the prompt data saving logic into a new function `savePromptData` Signed-off-by: Bo-Yi Wu <[email protected]>
1 parent e51b002 commit bf89c73

File tree

1 file changed

+34
-21
lines changed

1 file changed

+34
-21
lines changed

cmd/prompt.go

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,34 +25,47 @@ var defaultPromptDataKeys = []string{
2525
prompt.ConventionalCommitTemplate,
2626
}
2727

28+
// promptCmd represents the command to load default prompt data.
29+
// It uses the "prompt" keyword and provides a short description: "load default prompt data".
30+
// The command executes the RunE function which checks if the loadPromptData flag is set.
31+
// If set, it prompts the user for confirmation to load the default prompt data, which will overwrite existing data.
32+
// Upon confirmation, it retrieves the prompt folder path from the configuration and saves the default prompt data keys to the specified folder.
33+
// If any error occurs during the process, it returns the error.
2834
var promptCmd = &cobra.Command{
2935
Use: "prompt",
3036
Short: "load default prompt data",
3137
RunE: func(cmd *cobra.Command, args []string) error {
32-
if loadPromptData {
33-
change, err := confirmation.New("Do you want to load default prompt data, will overwrite your data", confirmation.No).RunPrompt()
34-
if err != nil || !change {
35-
return err
36-
}
38+
if !loadPromptData {
39+
return nil
40+
}
3741

38-
folder := viper.GetString("prompt_folder")
39-
for _, key := range defaultPromptDataKeys {
40-
// load default prompt data
41-
out, err := prompt.GetRawData(key)
42-
if err != nil {
43-
return err
44-
}
45-
46-
// save out to file
47-
target := path.Join(folder, key)
48-
if err := os.WriteFile(target, out, 0o600); err != nil {
49-
return err
50-
}
51-
color.Cyan("save %s to %s", key, target)
52-
}
42+
confirm, err := confirmation.New("Do you want to load default prompt data, will overwrite your data", confirmation.No).RunPrompt()
43+
if err != nil || !confirm {
44+
return err
45+
}
5346

54-
return nil
47+
folder := viper.GetString("prompt_folder")
48+
for _, key := range defaultPromptDataKeys {
49+
if err := savePromptData(folder, key); err != nil {
50+
return err
51+
}
5552
}
5653
return nil
5754
},
5855
}
56+
57+
func savePromptData(folder, key string) error {
58+
// load default prompt data
59+
out, err := prompt.GetRawData(key)
60+
if err != nil {
61+
return err
62+
}
63+
64+
// save out to file
65+
target := path.Join(folder, key)
66+
if err := os.WriteFile(target, out, 0o600); err != nil {
67+
return err
68+
}
69+
color.Cyan("save %s to %s", key, target)
70+
return nil
71+
}

0 commit comments

Comments
 (0)