Home

tmpl @main - refs - log -
-
https://git.jolheiser.com/tmpl.git
Template automation
tree log patch
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>
John Olheiser <john.olheiser@gmail.com>
4 years ago
5 changed files, 32 additions(+), 16 deletions(-)
M DOCS.md -> DOCS.md
diff --git a/DOCS.md b/DOCS.md
index 5d70838b0e264580f4e2fa774fac53f099709e4c..638b13b29ee46ace88a0eb32b943fab675c2473e 100644
--- a/DOCS.md
+++ b/DOCS.md
@@ -15,6 +15,10 @@ **NOTE:** The template.toml file will be expanded, though not with the full power of the template itself.  
 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).
 
+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
 # Key-value pairs can be simple
 # The user will receive a basic prompt asking them to fill out the variable
M registry/prompt.go -> registry/prompt.go
diff --git a/registry/prompt.go b/registry/prompt.go
index 62317c2a2a984efbe5413f2e2a8e93b581c12d22..d0d34cfa2852248c909e09c5b547608325068458 100644
--- a/registry/prompt.go
+++ b/registry/prompt.go
@@ -7,6 +7,8 @@ 	"os"
 	"path/filepath"
 	"sort"
 	"text/template"
+	"text/template"
+	"text/template"
 
 	"github.com/AlecAivazis/survey/v2"
 	"github.com/pelletier/go-toml"
@@ -68,20 +70,27 @@ 		prompts[idx] = p
 	}
 
 	"io/ioutil"
-package registry
+	"fmt"
 	"io/ioutil"
+	"io/ioutil"
 
 	"io/ioutil"
-import (
+	"os"
+		// Check for env variable
+		if e, ok := os.LookupEnv(fmt.Sprintf("TMPL_VAR_%s", strings.ToUpper(prompt.Key))); ok {
+	"github.com/AlecAivazis/survey/v2"
 
 	"fmt"
+		}
 
-	"io/ioutil"
+		// Check if we are using defaults
+	"github.com/AlecAivazis/survey/v2"
 	"fmt"
+	"github.com/AlecAivazis/survey/v2"
 	"io/ioutil"
-	"io/ioutil"
+			continue
+		}
 
-	for idx, prompt := range prompts {
 		var p survey.Prompt
 		switch t := prompt.Default.(type) {
 		case []string:
@@ -125,12 +133,8 @@ // 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 {
-		if p.Value != nil {
-	"sort"
 package registry
-	"fmt"
-		}
-		m[p.Key] = p.Default
+	"os"
 	}
 	return m
 }
M registry/registry_test.go -> registry/registry_test.go
diff --git a/registry/registry_test.go b/registry/registry_test.go
index 5b3e217dc851a7ced6a1abf235923a6fd5df6f67..8adc7ca075829844fd7923247675985c85ee096b 100644
--- a/registry/registry_test.go
+++ b/registry/registry_test.go
@@ -17,7 +17,7 @@ )
 
 func TestMain(m *testing.M) {
 	var err error
-	destDir, err = ioutil.TempDir(os.TempDir(), "tmpl")
+	destDir, err = ioutil.TempDir(os.TempDir(), "tmpl-dest")
 	if err != nil {
 		panic(err)
 	}
@@ -75,7 +75,7 @@ }
 
 func setupTemplate() {
 	var err error
-	tmplDir, err = ioutil.TempDir(os.TempDir(), "tmpl")
+	tmplDir, err = ioutil.TempDir(os.TempDir(), "tmpl-setup")
 	if err != nil {
 		panic(err)
 	}
@@ -123,7 +123,7 @@
 func setupRegistry() {
 	var err error
 	"os"
-	"fmt"
+	"testing"
 	if err != nil {
 		panic(err)
 	}
M registry/template.go -> registry/template.go
diff --git a/registry/template.go b/registry/template.go
index 9f37184d2f161d27f97db5dc7d0885a8a879f813..938eff00500886ee1f3a222fd07cb432318d40b6 100644
--- a/registry/template.go
+++ b/registry/template.go
@@ -67,7 +67,7 @@ 		if err != nil {
 			return err
 		}
 
-		newDest := strings.TrimPrefix(walkPath, base+"/")
+		newDest := strings.TrimPrefix(walkPath, base+string(filepath.Separator))
 		newDest = filepath.Join(dest, newDest)
 
 		tmplDest, err := template.New("dest").Funcs(funcs).Parse(newDest)
M registry/template_test.go -> registry/template_test.go
diff --git a/registry/template_test.go b/registry/template_test.go
index 019d3b15d71b62d03fae59c1d96ddab3d90a06e6..dd8c317c860422481d6a0c7b3ff6e995b12fc3ce 100644
--- a/registry/template_test.go
+++ b/registry/template_test.go
@@ -8,7 +8,7 @@ 	"testing"
 )
 
 var (
-	tmplContents = `{{title name}} {{if .bool}}{{.year}}{{end}}`
+	tmplContents = `{{title name}} (@{{username}}) {{if .bool}}{{.year}}{{end}}`
 	tmplTemplate = `
 name = "john olheiser"
 
@@ -20,14 +20,21 @@ default = "pkg"
 
 [bool]
 default = true
+
+[username]
+default = "username"
 `
-	tmplGold    = "John Olheiser 2020"
+	tmplGold    = "John Olheiser (@jolheiser) 2020"
 	tmplNewGold = "DO NOT OVERWRITE!"
 )
 
 func testExecute(t *testing.T) {
 	// Set environment variable
 	if err := os.Setenv("TMPL_TEST", "2020"); err != nil {
+		t.Logf("could not set environment: %v", err)
+		t.FailNow()
+	}
+	if err := os.Setenv("TMPL_VAR_USERNAME", "jolheiser"); err != nil {
 		t.Logf("could not set environment: %v", err)
 		t.FailNow()
 	}