-
Notifications
You must be signed in to change notification settings - Fork 3
/
prompt.go
107 lines (100 loc) · 2.71 KB
/
prompt.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package prompt
import (
"bytes"
"github.com/leslieleung/ptpt/internal/interract"
"github.com/leslieleung/ptpt/internal/prompt"
"github.com/leslieleung/ptpt/internal/ui"
"github.com/leslieleung/ptpt/static"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"io/fs"
"os"
"path/filepath"
)
var PromptCmd = &cobra.Command{
Use: "prompt",
}
var loadPromptCmd = &cobra.Command{
Use: "load",
PreRun: ui.ToggleDebug,
Run: func(cmd *cobra.Command, args []string) {
LoadPrompt()
},
}
func LoadPrompt() {
vp := viper.New()
vp.SetConfigType("yaml")
prompts := make(map[string]prompt.Prompt)
// bundled prompts
bundledPrompts, err := static.BundledPromptsStorage.ReadDir(".")
if err != nil {
ui.ErrorfExit("Error reading config file, %s", err)
}
for _, bundledPrompt := range bundledPrompts {
data, _ := static.BundledPromptsStorage.ReadFile(filepath.Join(bundledPrompt.Name()))
vpp := viper.New()
vpp.SetConfigType("yaml")
err = vpp.ReadConfig(bytes.NewReader(data))
if err != nil {
ui.ErrorfExit("Error reading config file, %s", err)
}
bundle := prompt.Bundle{}
err = vpp.Unmarshal(&bundle)
if err != nil {
ui.ErrorfExit("Error unmarshalling config file, %s", err)
}
for _, p := range bundle.Prompts {
prompts[p.Name] = p
}
}
var dirPrompts []os.DirEntry
if _, err := os.Stat(interract.GetPromptDir()); err != nil {
goto SumPrompts
}
dirPrompts, err = os.ReadDir(interract.GetPromptDir())
if err != nil {
ui.ErrorfExit("Error reading external config file, %s", err)
}
log.Debugf("dir: %s", interract.GetPromptDir())
for _, dirPrompt := range dirPrompts {
err := iterateDir(filepath.Join(interract.GetPromptDir(), dirPrompt.Name()), dirPrompt, vp, prompts)
if err != nil {
ui.ErrorfExit("Error reading external config file, %s", err)
}
}
SumPrompts:
log.Debugf("[total %d] prompts", len(prompts))
prompt.Lib = prompts
log.Debugf("Loaded %d prompts", len(prompts))
}
type promptForm struct {
Prompt string `survey:"prompt"`
Description string `survey:"description"`
System string `survey:"system"`
More bool `survey:"more"`
}
func iterateDir(fName string, fInfo fs.DirEntry, vp *viper.Viper, prompts map[string]prompt.Prompt) error {
if fInfo.IsDir() {
return nil
}
vp.SetConfigFile(fName)
err := vp.ReadInConfig()
if err != nil {
ui.ErrorfExit("Error reading config file, %s", err)
}
bundle := prompt.Bundle{}
err = vp.Unmarshal(&bundle)
if err != nil {
ui.ErrorfExit("Error unmarshalling config file, %s", err)
}
for _, p := range bundle.Prompts {
prompts[p.Name] = p
}
return nil
}
func init() {
PromptCmd.AddCommand(subscribeCmd)
PromptCmd.AddCommand(createPromptCmd)
PromptCmd.AddCommand(loadPromptCmd)
}