Home

tmpl @4987bf8aed021ee787d751eea7934b8f1ab8693a - refs - log -
-
https://git.jolheiser.com/tmpl.git
Template automation
tmpl / registry / prompt.go
- raw
  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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package registry

import (
	"fmt"
	"io/ioutil"
	"os"
	"path/filepath"
	"sort"
	"strings"
	"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
	}

	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)
	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 {
		// 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
		}

		var p survey.Prompt
		switch t := prompt.Default.(type) {
		case []string:
			p = &survey.Select{
				Message: prompt.Message,
				Options: t,
				Help:    prompt.Help,
			}
		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,
			}
		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

// ToMap converts a slice to templatePrompt into a suitable template context
func (t templatePrompts) ToMap() map[string]interface{} {
	m := make(map[string]interface{})
	for _, p := range t {
		m[p.Key] = p.Value
	}
	return m
}

// ToFuncMap converts a slice of templatePrompt into a suitable template.FuncMap
func (t templatePrompts) ToFuncMap() template.FuncMap {
	m := make(map[string]interface{})
	for k, v := range t.ToMap() {
		vv := v // Enclosure
		m[k] = func() interface{} {
			return vv
		}
	}
	return m
}

// Len is for sort.Sort
func (t templatePrompts) Len() int {
	return len(t)
}

// Less is for sort.Sort
func (t templatePrompts) Less(i, j int) bool {
	return t[i].Key > t[j].Key
}

// Swap is for sort.Sort
func (t templatePrompts) Swap(i, j int) {
	t[i], t[j] = t[j], t[i]
}