2020-11-22 05:25:24 +00:00
|
|
|
package registry
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2023-08-20 20:01:58 +00:00
|
|
|
"strconv"
|
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"
|
|
|
|
|
2023-12-20 04:07:47 +00:00
|
|
|
"github.com/charmbracelet/huh"
|
2020-11-22 05:25:24 +00:00
|
|
|
)
|
|
|
|
|
2023-12-20 04:07:47 +00:00
|
|
|
func prompt(dir string, defaults, accessible 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:59:21 +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:59:21 +00:00
|
|
|
// Otherwise, prompt
|
2023-12-20 04:07:47 +00:00
|
|
|
var f huh.Field
|
2023-08-20 20:01:58 +00:00
|
|
|
switch prompt.Type {
|
|
|
|
case config.PromptTypeSelect:
|
2023-12-20 04:07:47 +00:00
|
|
|
opts := make([]huh.Option[string], 0, len(prompt.Options))
|
2022-06-15 14:59:21 +00:00
|
|
|
for idy, opt := range prompt.Options {
|
2023-12-20 04:07:47 +00:00
|
|
|
o := os.ExpandEnv(opt)
|
|
|
|
opts[idy] = huh.NewOption(o, o)
|
2020-11-24 06:10:46 +00:00
|
|
|
}
|
2023-12-20 04:07:47 +00:00
|
|
|
def := prompt.Default
|
|
|
|
f = huh.NewSelect[string]().
|
|
|
|
Title(prompt.Label).
|
|
|
|
Description(prompt.Help).
|
|
|
|
Options(opts...).
|
|
|
|
Key(prompt.ID).
|
|
|
|
Value(&def)
|
2023-08-20 20:01:58 +00:00
|
|
|
case config.PromptTypeConfirm:
|
|
|
|
def, _ := strconv.ParseBool(os.ExpandEnv(prompt.Default))
|
2023-12-20 04:07:47 +00:00
|
|
|
f = huh.NewConfirm().
|
|
|
|
Title(prompt.Label).
|
|
|
|
Description(prompt.Help).
|
|
|
|
Key(prompt.ID).
|
|
|
|
Value(&def)
|
|
|
|
case config.PromptTypeMultiline, config.PromptTypeEditor:
|
|
|
|
def := os.ExpandEnv(prompt.Default)
|
|
|
|
f = huh.NewText().
|
|
|
|
Title(prompt.Label).
|
|
|
|
Description(prompt.Help).
|
|
|
|
Key(prompt.ID).
|
|
|
|
Value(&def)
|
2023-08-20 20:01:58 +00:00
|
|
|
default:
|
2023-12-20 04:07:47 +00:00
|
|
|
def := os.ExpandEnv(prompt.Default)
|
|
|
|
f = huh.NewInput().
|
|
|
|
Title(prompt.Label).
|
|
|
|
Description(prompt.Help).
|
|
|
|
Key(prompt.ID).
|
|
|
|
Value(&def)
|
2020-11-22 05:25:24 +00:00
|
|
|
}
|
2023-12-20 04:07:47 +00:00
|
|
|
if err := huh.NewForm(huh.NewGroup(f)).WithAccessible(accessible).WithTheme(huh.ThemeCatppuccin()).Run(); err != nil {
|
|
|
|
return nil, fmt.Errorf("could not run field: %w", err)
|
2020-11-22 05:25:24 +00:00
|
|
|
}
|
2023-12-20 04:07:47 +00:00
|
|
|
prompts[idx].Value = f.GetValue()
|
|
|
|
os.Setenv(fmt.Sprintf("TMPL_PROMPT_%s", envKey), fmt.Sprint(f.GetValue()))
|
2020-11-22 05:25:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return prompts, nil
|
|
|
|
}
|
|
|
|
|
2022-06-14 19:59:53 +00:00
|
|
|
type templatePrompt struct {
|
|
|
|
config.Prompt
|
2023-08-20 20:01:58 +00:00
|
|
|
Value any
|
2022-06-14 19:59:53 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|