diff --git a/DOCS.md b/DOCS.md index 889b7f0..57ffe31 100644 --- a/DOCS.md +++ b/DOCS.md @@ -11,6 +11,10 @@ A "valid" tmpl template only requires two things ## 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 b683205..dfa47ce 100644 --- a/registry/prompt.go +++ b/registry/prompt.go @@ -2,6 +2,7 @@ package registry import ( "fmt" + "io/ioutil" "os" "path/filepath" "sort" @@ -25,7 +26,15 @@ func prompt(dir string, defaults bool) (templatePrompts, error) { return nil, err } - tree, err := toml.LoadFile(templatePath) + templateBytes, err := ioutil.ReadFile(templatePath) + if err != nil { + 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 be32ced..11f2d0f 100644 --- a/registry/template_test.go +++ b/registry/template_test.go @@ -12,7 +12,7 @@ var ( tmplTemplate = `name = "john olheiser" [year] -default = 2020 +default = ${TMPL_TEST} [package] default = "pkg"` @@ -21,6 +21,12 @@ default = "pkg"` ) 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 {