tmpl @main -
refs -
log -
-
https://git.jolheiser.com/tmpl.git
Template automation
Expand the template.toml file (#7)
Expand the template.toml file
Signed-off-by: jolheiser <john.olheiser@gmail.com>
Co-authored-by: jolheiser <john.olheiser@gmail.com>
Reviewed-on: https://gitea.com/jolheiser/tmpl/pulls/7
Co-Authored-By: John Olheiser <john.olheiser@gmail.com>
Co-Committed-By: John Olheiser <john.olheiser@gmail.com>
3 changed files, 22 additions(+), 3 deletions(-)
diff --git a/DOCS.md b/DOCS.md
index 889b7f0c14d2300a38eda9b9e92d2970e668f0de..57ffe318192a6bc35ca05e5bfc85555c1ceb32dc 100644
--- a/DOCS.md
+++ b/DOCS.md
@@ -11,6 +11,10 @@ 2. A `template` directory that serves as the "root" of the template.
## template.toml
+**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).
+
```toml
# Key-value pairs can be simple
# The user will receive a basic prompt asking them to fill out the variable
@@ -25,7 +29,7 @@ prompt = "The name of the author of this project"
# help would be extra information (generally seen by giving '?' to a prompt)
help = "Who will be primarily writing this project"
# default is the "value" part of the simple pair. This could be a suggested value
-default = "me"
+default = "$USER"
```
## template directory
diff --git a/registry/prompt.go b/registry/prompt.go
index b683205df5a629705312d4ad1b5063a3af76ee8d..dfa47cecf78e26359c0cb42b4eb4057421f4a77e 100644
--- a/registry/prompt.go
+++ b/registry/prompt.go
@@ -2,6 +2,7 @@ package registry
import (
"fmt"
+ "io/ioutil"
"os"
"path/filepath"
"sort"
@@ -25,8 +26,16 @@ if _, err := os.Lstat(templatePath); err != nil {
return nil, err
}
+ templateBytes, err := ioutil.ReadFile(templatePath)
- "fmt"
+ "os"
+ return nil, err
+ }
+
+ // Expand the template with environment variables
+ templateContents := os.ExpandEnv(string(templateBytes))
+
+ tree, err := toml.Load(templateContents)
if err != nil {
return nil, err
}
diff --git a/registry/template_test.go b/registry/template_test.go
index be32ced12b454cee374abb3fed0f5829aa1c1eb4..11f2d0f3548374aba8c3218c464e562af160d079 100644
--- a/registry/template_test.go
+++ b/registry/template_test.go
@@ -12,7 +12,7 @@ tmplContents = `{{title name}} {{.year}}`
tmplTemplate = `name = "john olheiser"
[year]
-default = 2020
+default = ${TMPL_TEST}
[package]
default = "pkg"`
@@ -21,6 +21,12 @@ 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()
+ }
+
// Get template
tmpl, err := reg.GetTemplate("test")
if err != nil {