2020-11-22 05:25:24 +00:00
|
|
|
package registry
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2020-12-29 03:21:41 +00:00
|
|
|
"strings"
|
2020-11-22 05:25:24 +00:00
|
|
|
"text/template"
|
|
|
|
|
2022-06-14 19:59:53 +00:00
|
|
|
"go.jolheiser.com/tmpl/config"
|
|
|
|
|
2020-11-22 05:25:24 +00:00
|
|
|
"github.com/AlecAivazis/survey/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
func prompt(dir string, defaults bool) (templatePrompts, error) {
|
2022-06-14 19:59:53 +00:00
|
|
|
templatePath := filepath.Join(dir, "tmpl.yaml")
|
|
|
|
fi, err := os.Open(templatePath)
|
2020-11-23 05:02:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-06-14 19:59:53 +00:00
|
|
|
defer fi.Close()
|
2020-11-23 05:02:16 +00:00
|
|
|
|
2022-06-14 19:59:53 +00:00
|
|
|
cfg, err := config.Load(fi)
|
2020-11-22 05:25:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-06-14 19:59:53 +00:00
|
|
|
prompts := make(templatePrompts, 0, len(cfg.Prompts))
|
|
|
|
for _, prompt := range cfg.Prompts {
|
|
|
|
tp := templatePrompt{
|
|
|
|
Prompt: prompt,
|
2020-11-22 05:25:24 +00:00
|
|
|
}
|
2022-06-14 19:59:53 +00:00
|
|
|
if tp.Label == "" {
|
|
|
|
tp.Label = tp.ID
|
2020-11-22 05:25:24 +00:00
|
|
|
}
|
2022-06-14 19:59:53 +00:00
|
|
|
prompts = append(prompts, tp)
|
2020-11-22 05:25:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for idx, prompt := range prompts {
|
2020-12-29 03:21:41 +00:00
|
|
|
// Check for env variable
|
2022-06-14 19:59:53 +00:00
|
|
|
envKey := strings.ToUpper(prompt.ID)
|
|
|
|
if e, ok := os.LookupEnv(fmt.Sprintf("TMPL_VAR_%s", envKey)); ok {
|
2020-12-29 03:21:41 +00:00
|
|
|
prompts[idx].Value = e
|
2022-06-14 19:59:53 +00:00
|
|
|
os.Setenv(fmt.Sprintf("TMPL_PROMPT_%s", envKey), e)
|
2020-12-29 03:21:41 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if we are using defaults
|
|
|
|
if defaults {
|
2022-06-15 14:53:04 +00:00
|
|
|
val := os.ExpandEnv(prompt.Default)
|
|
|
|
prompts[idx].Value = val
|
|
|
|
os.Setenv(fmt.Sprintf("TMPL_PROMPT_%s", envKey), val)
|
2020-12-29 03:21:41 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-06-15 14:53:04 +00:00
|
|
|
// Otherwise, prompt
|
2020-11-22 05:25:24 +00:00
|
|
|
var p survey.Prompt
|
2022-06-15 14:53:04 +00:00
|
|
|
if len(prompt.Options) > 0 {
|
|
|
|
opts := make([]string, 0, len(prompt.Options))
|
|
|
|
for idy, opt := range prompt.Options {
|
|
|
|
opts[idy] = os.ExpandEnv(opt)
|
2022-06-14 19:59:53 +00:00
|
|
|
}
|
2020-11-22 05:25:24 +00:00
|
|
|
p = &survey.Select{
|
2022-06-14 19:59:53 +00:00
|
|
|
Message: prompt.Label,
|
2022-06-15 14:53:04 +00:00
|
|
|
Options: opts,
|
2020-11-24 06:10:46 +00:00
|
|
|
Help: prompt.Help,
|
|
|
|
}
|
2022-06-15 14:53:04 +00:00
|
|
|
} else {
|
2020-11-24 06:10:46 +00:00
|
|
|
p = &survey.Input{
|
2022-06-14 19:59:53 +00:00
|
|
|
Message: prompt.Label,
|
2022-06-15 14:53:04 +00:00
|
|
|
Default: os.ExpandEnv(prompt.Default),
|
2020-11-22 05:25:24 +00:00
|
|
|
Help: prompt.Help,
|
|
|
|
}
|
|
|
|
}
|
2022-06-15 14:53:04 +00:00
|
|
|
|
2020-11-22 05:25:24 +00:00
|
|
|
var a string
|
|
|
|
if err := survey.AskOne(p, &a); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
prompts[idx].Value = a
|
2022-06-14 19:59:53 +00:00
|
|
|
os.Setenv(fmt.Sprintf("TMPL_PROMPT_%s", envKey), a)
|
2020-11-22 05:25:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return prompts, nil
|
|
|
|
}
|
|
|
|
|
2022-06-14 19:59:53 +00:00
|
|
|
type templatePrompt struct {
|
|
|
|
config.Prompt
|
|
|
|
Value string
|
|
|
|
}
|
|
|
|
|
2020-11-22 05:25:24 +00:00
|
|
|
type templatePrompts []templatePrompt
|
|
|
|
|
2020-11-24 06:10:46 +00:00
|
|
|
// ToMap converts a slice to templatePrompt into a suitable template context
|
2022-06-14 19:59:53 +00:00
|
|
|
func (t templatePrompts) ToMap() map[string]any {
|
|
|
|
m := make(map[string]any)
|
2020-11-22 05:25:24 +00:00
|
|
|
for _, p := range t {
|
2022-06-14 19:59:53 +00:00
|
|
|
m[p.ID] = p.Value
|
2020-11-22 05:25:24 +00:00
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2020-11-24 06:10:46 +00:00
|
|
|
// ToFuncMap converts a slice of templatePrompt into a suitable template.FuncMap
|
2020-11-22 05:25:24 +00:00
|
|
|
func (t templatePrompts) ToFuncMap() template.FuncMap {
|
2022-06-14 19:59:53 +00:00
|
|
|
m := make(map[string]any)
|
2020-11-22 05:25:24 +00:00
|
|
|
for k, v := range t.ToMap() {
|
|
|
|
vv := v // Enclosure
|
2022-06-14 19:59:53 +00:00
|
|
|
m[k] = func() any {
|
2020-11-24 06:10:46 +00:00
|
|
|
return vv
|
2020-11-22 05:25:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|