Allow sourcing prompt answers from env variables (#14)
Fixes #12 Co-authored-by: jolheiser <john.olheiser@gmail.com> Reviewed-on: https://gitea.com/jolheiser/tmpl/pulls/14 Co-authored-by: John Olheiser <john.olheiser@gmail.com> Co-committed-by: John Olheiser <john.olheiser@gmail.com>pull/16/head v0.0.9
parent
19edb3a580
commit
d4c47101ab
4
DOCS.md
4
DOCS.md
|
@ -15,6 +15,10 @@ A "valid" tmpl template only requires two things
|
||||||
The template.toml file will only expand environment variables with syntax `$USER` or `${USER}`.
|
The template.toml file will only expand environment variables with syntax `$USER` or `${USER}`.
|
||||||
For full documentation on the syntax, see [os.ExpandEnv](https://golang.org/pkg/os/#ExpandEnv).
|
For full documentation on the syntax, see [os.ExpandEnv](https://golang.org/pkg/os/#ExpandEnv).
|
||||||
|
|
||||||
|
When using the `--defaults` flag, no prompts will be shown and only default values will be used.
|
||||||
|
As another alternative, any environment variable that matches a key will bypass the prompt.
|
||||||
|
For example, `author` would have the corresponding environment variable `TMPL_VAR_AUTHOR`.
|
||||||
|
|
||||||
```toml
|
```toml
|
||||||
# Key-value pairs can be simple
|
# Key-value pairs can be simple
|
||||||
# The user will receive a basic prompt asking them to fill out the variable
|
# The user will receive a basic prompt asking them to fill out the variable
|
||||||
|
|
|
@ -6,6 +6,7 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"sort"
|
"sort"
|
||||||
|
"strings"
|
||||||
"text/template"
|
"text/template"
|
||||||
|
|
||||||
"github.com/AlecAivazis/survey/v2"
|
"github.com/AlecAivazis/survey/v2"
|
||||||
|
@ -67,15 +68,22 @@ func prompt(dir string, defaults bool) (templatePrompts, error) {
|
||||||
prompts[idx] = p
|
prompts[idx] = p
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return early if we only want defaults
|
|
||||||
if defaults {
|
|
||||||
return prompts, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Sort the prompts so they are consistent
|
// Sort the prompts so they are consistent
|
||||||
sort.Sort(prompts)
|
sort.Sort(prompts)
|
||||||
|
|
||||||
for idx, prompt := range 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
|
var p survey.Prompt
|
||||||
switch t := prompt.Default.(type) {
|
switch t := prompt.Default.(type) {
|
||||||
case []string:
|
case []string:
|
||||||
|
@ -119,11 +127,7 @@ type templatePrompts []templatePrompt
|
||||||
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 {
|
||||||
if p.Value != nil {
|
m[p.Key] = p.Value
|
||||||
m[p.Key] = p.Value
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
m[p.Key] = p.Default
|
|
||||||
}
|
}
|
||||||
return m
|
return m
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,7 +17,7 @@ var (
|
||||||
|
|
||||||
func TestMain(m *testing.M) {
|
func TestMain(m *testing.M) {
|
||||||
var err error
|
var err error
|
||||||
destDir, err = ioutil.TempDir(os.TempDir(), "tmpl")
|
destDir, err = ioutil.TempDir(os.TempDir(), "tmpl-dest")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
@ -75,7 +75,7 @@ func testGetFail(t *testing.T) {
|
||||||
|
|
||||||
func setupTemplate() {
|
func setupTemplate() {
|
||||||
var err error
|
var err error
|
||||||
tmplDir, err = ioutil.TempDir(os.TempDir(), "tmpl")
|
tmplDir, err = ioutil.TempDir(os.TempDir(), "tmpl-setup")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
@ -122,7 +122,7 @@ func setupTemplate() {
|
||||||
|
|
||||||
func setupRegistry() {
|
func setupRegistry() {
|
||||||
var err error
|
var err error
|
||||||
regDir, err = ioutil.TempDir(os.TempDir(), "tmpl")
|
regDir, err = ioutil.TempDir(os.TempDir(), "tmpl-reg")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -67,7 +67,7 @@ func (t *Template) Execute(dest string, defaults, overwrite bool) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
newDest := strings.TrimPrefix(walkPath, base+"/")
|
newDest := strings.TrimPrefix(walkPath, base+string(filepath.Separator))
|
||||||
newDest = filepath.Join(dest, newDest)
|
newDest = filepath.Join(dest, newDest)
|
||||||
|
|
||||||
tmplDest, err := template.New("dest").Funcs(funcs).Parse(newDest)
|
tmplDest, err := template.New("dest").Funcs(funcs).Parse(newDest)
|
||||||
|
|
|
@ -8,7 +8,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
tmplContents = `{{title name}} {{if .bool}}{{.year}}{{end}}`
|
tmplContents = `{{title name}} (@{{username}}) {{if .bool}}{{.year}}{{end}}`
|
||||||
tmplTemplate = `
|
tmplTemplate = `
|
||||||
name = "john olheiser"
|
name = "john olheiser"
|
||||||
|
|
||||||
|
@ -20,8 +20,11 @@ default = "pkg"
|
||||||
|
|
||||||
[bool]
|
[bool]
|
||||||
default = true
|
default = true
|
||||||
|
|
||||||
|
[username]
|
||||||
|
default = "username"
|
||||||
`
|
`
|
||||||
tmplGold = "John Olheiser 2020"
|
tmplGold = "John Olheiser (@jolheiser) 2020"
|
||||||
tmplNewGold = "DO NOT OVERWRITE!"
|
tmplNewGold = "DO NOT OVERWRITE!"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -31,6 +34,10 @@ func testExecute(t *testing.T) {
|
||||||
t.Logf("could not set environment: %v", err)
|
t.Logf("could not set environment: %v", err)
|
||||||
t.FailNow()
|
t.FailNow()
|
||||||
}
|
}
|
||||||
|
if err := os.Setenv("TMPL_VAR_USERNAME", "jolheiser"); err != nil {
|
||||||
|
t.Logf("could not set environment: %v", err)
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
|
|
||||||
// Get template
|
// Get template
|
||||||
tmpl, err := reg.GetTemplate("test")
|
tmpl, err := reg.GetTemplate("test")
|
||||||
|
|
Loading…
Reference in New Issue