2020-11-22 05:25:24 +00:00
|
|
|
package registry
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-11-23 05:02:16 +00:00
|
|
|
"io/ioutil"
|
2020-11-22 05:25:24 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"sort"
|
2020-12-29 03:21:41 +00:00
|
|
|
"strings"
|
2020-11-22 05:25:24 +00:00
|
|
|
"text/template"
|
|
|
|
|
|
|
|
"github.com/AlecAivazis/survey/v2"
|
|
|
|
"github.com/pelletier/go-toml"
|
|
|
|
)
|
|
|
|
|
|
|
|
type templatePrompt struct {
|
|
|
|
Key string `toml:"-"`
|
|
|
|
Value interface{} `toml:"-"`
|
|
|
|
Message string `toml:"prompt"`
|
|
|
|
Help string `toml:"help"`
|
|
|
|
Default interface{} `toml:"default"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func prompt(dir string, defaults bool) (templatePrompts, error) {
|
|
|
|
templatePath := filepath.Join(dir, "template.toml")
|
|
|
|
if _, err := os.Lstat(templatePath); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-11-23 05:02:16 +00:00
|
|
|
templateBytes, err := ioutil.ReadFile(templatePath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Expand the template with environment variables
|
|
|
|
templateContents := os.ExpandEnv(string(templateBytes))
|
|
|
|
|
|
|
|
tree, err := toml.Load(templateContents)
|
2020-11-22 05:25:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
prompts := make(templatePrompts, len(tree.Keys()))
|
|
|
|
for idx, k := range tree.Keys() {
|
|
|
|
v := tree.Get(k)
|
|
|
|
|
|
|
|
obj, ok := v.(*toml.Tree)
|
|
|
|
if !ok {
|
|
|
|
prompts[idx] = templatePrompt{
|
|
|
|
Key: k,
|
|
|
|
Message: k,
|
|
|
|
Default: v,
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
var p templatePrompt
|
|
|
|
if err := obj.Unmarshal(&p); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
p.Key = k
|
|
|
|
if p.Message == "" {
|
|
|
|
p.Message = p.Key
|
|
|
|
}
|
|
|
|
if p.Default == nil {
|
|
|
|
p.Default = ""
|
|
|
|
}
|
|
|
|
prompts[idx] = p
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sort the prompts so they are consistent
|
|
|
|
sort.Sort(prompts)
|
|
|
|
|
|
|
|
for idx, prompt := range prompts {
|
2020-12-29 03:21:41 +00:00
|
|
|
// Check for env variable
|
|
|
|
if e, ok := os.LookupEnv(fmt.Sprintf("TMPL_VAR_%s", strings.ToUpper(prompt.Key))); ok {
|
|
|
|
prompts[idx].Value = e
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if we are using defaults
|
|
|
|
if defaults {
|
|
|
|
prompts[idx].Value = prompt.Default
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-11-22 05:25:24 +00:00
|
|
|
var p survey.Prompt
|
|
|
|
switch t := prompt.Default.(type) {
|
|
|
|
case []string:
|
|
|
|
p = &survey.Select{
|
|
|
|
Message: prompt.Message,
|
|
|
|
Options: t,
|
|
|
|
Help: prompt.Help,
|
|
|
|
}
|
2020-11-24 06:10:46 +00:00
|
|
|
case bool:
|
|
|
|
p = &survey.Confirm{
|
|
|
|
Message: prompt.Message,
|
|
|
|
Default: t,
|
|
|
|
Help: prompt.Help,
|
|
|
|
}
|
|
|
|
case string:
|
|
|
|
p = &survey.Input{
|
|
|
|
Message: prompt.Message,
|
|
|
|
Default: t,
|
|
|
|
Help: prompt.Help,
|
|
|
|
}
|
2020-11-22 05:25:24 +00:00
|
|
|
default:
|
|
|
|
p = &survey.Input{
|
|
|
|
Message: prompt.Message,
|
|
|
|
Default: fmt.Sprintf("%v", t),
|
|
|
|
Help: prompt.Help,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var a string
|
|
|
|
if err := survey.AskOne(p, &a); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
prompts[idx].Value = a
|
|
|
|
}
|
|
|
|
|
|
|
|
return prompts, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type templatePrompts []templatePrompt
|
|
|
|
|
2020-11-24 06:10:46 +00:00
|
|
|
// ToMap converts a slice to templatePrompt into a suitable template context
|
2020-11-22 05:25:24 +00:00
|
|
|
func (t templatePrompts) ToMap() map[string]interface{} {
|
|
|
|
m := make(map[string]interface{})
|
|
|
|
for _, p := range t {
|
2020-12-29 03:21:41 +00:00
|
|
|
m[p.Key] = 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 {
|
|
|
|
m := make(map[string]interface{})
|
|
|
|
for k, v := range t.ToMap() {
|
|
|
|
vv := v // Enclosure
|
2020-11-24 06:10:46 +00:00
|
|
|
m[k] = func() interface{} {
|
|
|
|
return vv
|
2020-11-22 05:25:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2020-11-24 06:10:46 +00:00
|
|
|
// Len is for sort.Sort
|
2020-11-22 05:25:24 +00:00
|
|
|
func (t templatePrompts) Len() int {
|
|
|
|
return len(t)
|
|
|
|
}
|
|
|
|
|
2020-11-24 06:10:46 +00:00
|
|
|
// Less is for sort.Sort
|
2020-11-22 05:25:24 +00:00
|
|
|
func (t templatePrompts) Less(i, j int) bool {
|
|
|
|
return t[i].Key > t[j].Key
|
|
|
|
}
|
|
|
|
|
2020-11-24 06:10:46 +00:00
|
|
|
// Swap is for sort.Sort
|
2020-11-22 05:25:24 +00:00
|
|
|
func (t templatePrompts) Swap(i, j int) {
|
|
|
|
t[i], t[j] = t[j], t[i]
|
|
|
|
}
|