Better defaults and prompts (#9)
Add true bools and better defaults, fix map/funcmap differences Signed-off-by: jolheiser <john.olheiser@gmail.com> Co-authored-by: jolheiser <john.olheiser@gmail.com> Reviewed-on: https://gitea.com/jolheiser/tmpl/pulls/9 Co-Authored-By: John Olheiser <john.olheiser@gmail.com> Co-Committed-By: John Olheiser <john.olheiser@gmail.com>pull/10/head v0.0.7
parent
47ef9ea0be
commit
36832f93de
|
@ -84,6 +84,18 @@ func prompt(dir string, defaults bool) (templatePrompts, error) {
|
|||
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,
|
||||
|
@ -103,6 +115,7 @@ func prompt(dir string, defaults bool) (templatePrompts, error) {
|
|||
|
||||
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 {
|
||||
|
@ -115,25 +128,29 @@ func (t templatePrompts) ToMap() map[string]interface{} {
|
|||
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() string {
|
||||
return fmt.Sprintf("%v", vv)
|
||||
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]
|
||||
}
|
||||
|
|
|
@ -8,14 +8,19 @@ import (
|
|||
)
|
||||
|
||||
var (
|
||||
tmplContents = `{{title name}} {{.year}}`
|
||||
tmplTemplate = `name = "john olheiser"
|
||||
tmplContents = `{{title name}} {{if .bool}}{{.year}}{{end}}`
|
||||
tmplTemplate = `
|
||||
name = "john olheiser"
|
||||
|
||||
[year]
|
||||
default = ${TMPL_TEST}
|
||||
default = ${TMPL_TEST} # 2020
|
||||
|
||||
[package]
|
||||
default = "pkg"`
|
||||
default = "pkg"
|
||||
|
||||
[bool]
|
||||
default = true
|
||||
`
|
||||
tmplGold = "John Olheiser 2020"
|
||||
tmplNewGold = "DO NOT OVERWRITE!"
|
||||
)
|
||||
|
|
Loading…
Reference in New Issue