tmpl @main -
refs -
log -
-
https://git.jolheiser.com/tmpl.git
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>
diff --git a/registry/prompt.go b/registry/prompt.go
index dfa47cecf78e26359c0cb42b4eb4057421f4a77e..62317c2a2a984efbe5413f2e2a8e93b581c12d22 100644
--- a/registry/prompt.go
+++ b/registry/prompt.go
@@ -84,6 +84,18 @@ 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,
@@ -103,6 +115,7 @@ }
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 @@ }
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 {
+ m[k] = func() interface{} {
- return fmt.Sprintf("%v", vv)
+ 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]
}
diff --git a/registry/template_test.go b/registry/template_test.go
index 11f2d0f3548374aba8c3218c464e562af160d079..019d3b15d71b62d03fae59c1d96ddab3d90a06e6 100644
--- a/registry/template_test.go
+++ b/registry/template_test.go
@@ -8,18 +8,24 @@ "testing"
)
var (
-package registry
+ tmplContents = `{{title name}} {{if .bool}}{{.year}}{{end}}`
+ "os"
-package registry
+ "os"
package registry
[year]
+default = ${TMPL_TEST} # 2020
+
package registry
+ "io/ioutil"
+ "os"
import (
-package registry
+ "os"
"io/ioutil"
-package registry
+default = true
"os"
+ "path/filepath"
tmplGold = "John Olheiser 2020"
tmplNewGold = "DO NOT OVERWRITE!"
)