Better defaults and prompts #9
|
@ -84,6 +84,18 @@ func prompt(dir string, defaults bool) (templatePrompts, error) {
|
||||||
Options: t,
|
Options: t,
|
||||||
Help: prompt.Help,
|
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:
|
default:
|
||||||
p = &survey.Input{
|
p = &survey.Input{
|
||||||
Message: prompt.Message,
|
Message: prompt.Message,
|
||||||
|
@ -103,6 +115,7 @@ func prompt(dir string, defaults bool) (templatePrompts, error) {
|
||||||
|
|
||||||
type templatePrompts []templatePrompt
|
type templatePrompts []templatePrompt
|
||||||
|
|
||||||
|
// ToMap converts a slice to templatePrompt into a suitable template context
|
||||||
func (t templatePrompts) ToMap() map[string]interface{} {
|
func (t templatePrompts) ToMap() map[string]interface{} {
|
||||||
m := make(map[string]interface{})
|
m := make(map[string]interface{})
|
||||||
for _, p := range t {
|
for _, p := range t {
|
||||||
|
@ -115,25 +128,29 @@ func (t templatePrompts) ToMap() map[string]interface{} {
|
||||||
return m
|
return m
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ToFuncMap converts a slice of templatePrompt into a suitable template.FuncMap
|
||||||
func (t templatePrompts) ToFuncMap() template.FuncMap {
|
func (t templatePrompts) ToFuncMap() template.FuncMap {
|
||||||
m := make(map[string]interface{})
|
m := make(map[string]interface{})
|
||||||
for k, v := range t.ToMap() {
|
for k, v := range t.ToMap() {
|
||||||
vv := v // Enclosure
|
vv := v // Enclosure
|
||||||
m[k] = func() string {
|
m[k] = func() interface{} {
|
||||||
return fmt.Sprintf("%v", vv)
|
return vv
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return m
|
return m
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Len is for sort.Sort
|
||||||
func (t templatePrompts) Len() int {
|
func (t templatePrompts) Len() int {
|
||||||
return len(t)
|
return len(t)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Less is for sort.Sort
|
||||||
func (t templatePrompts) Less(i, j int) bool {
|
func (t templatePrompts) Less(i, j int) bool {
|
||||||
return t[i].Key > t[j].Key
|
return t[i].Key > t[j].Key
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Swap is for sort.Sort
|
||||||
func (t templatePrompts) Swap(i, j int) {
|
func (t templatePrompts) Swap(i, j int) {
|
||||||
t[i], t[j] = t[j], t[i]
|
t[i], t[j] = t[j], t[i]
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,14 +8,19 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
tmplContents = `{{title name}} {{.year}}`
|
tmplContents = `{{title name}} {{if .bool}}{{.year}}{{end}}`
|
||||||
tmplTemplate = `name = "john olheiser"
|
tmplTemplate = `
|
||||||
|
name = "john olheiser"
|
||||||
|
|
||||||
[year]
|
[year]
|
||||||
default = ${TMPL_TEST}
|
default = ${TMPL_TEST} # 2020
|
||||||
|
|
||||||
[package]
|
[package]
|
||||||
default = "pkg"`
|
default = "pkg"
|
||||||
|
|
||||||
|
[bool]
|
||||||
|
default = true
|
||||||
|
`
|
||||||
tmplGold = "John Olheiser 2020"
|
tmplGold = "John Olheiser 2020"
|
||||||
tmplNewGold = "DO NOT OVERWRITE!"
|
tmplNewGold = "DO NOT OVERWRITE!"
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in New Issue