Process all template files recursively in a directory. This is useful for project templates, for example, after creating a new repository from a template repository.
All files in the specified directory will be processed as templates. For now, this project assumes all files are UTF-8 encoded. A .git directory or file (worktree) will be skipped.
For every {{param}}
found in a template, the user will be prompted to answer
unless the named parameter was already in the parameter cache you pass. This
cache can be pre-populated as well.
import (
"log"
"github.com/heaths/go-template"
)
func Example() {
params := make(map[string]string)
err := template.Apply("testdata", params,
template.WithLogger(log.Default(), true),
)
if err != nil {
log.Fatal(err)
}
}
Templates are processed using text/template
.
Template files contain a mix of text and actions surrounded by {{
and }}
e.g.,
# {{param "name" "" "What is the project name?" | titlecase}}
This is an example.
In addition to built-in functions, the following functions are also available:
param <name> [<default> [<prompt>]]
<name>
, or prompt using an optional <default>
<prompt>
. If a <prompt>
is not specified, the required<name>
is used. The type of <default>
dictates valid input. Only string
int
are supported at this time.pluralize <count> <thing>
<thing>
if <count>
is not equal to 1. <count>
can beint
or a string
representing an int
e.g., "1".lowercase <string>
<string>
to all lowercase characters.titlecase <string>
<string>
to Title Case characters.uppercase <string>
<string>
to UPPERCASE characters.replace <from> <to> <source>
<from>
to <to>
in the <source>
string.date
date.Format <layout>
time.Format
.date.Local
date
functionsdate.Local.Year
.date.Year
true
true
. Useful as a default value to accept yes/Y or no/N answers.false
false
. Useful as a default value to accept yes/Y or no/N answers.deleteFile
Note that date
functions including Format
, Local
, and Year
are function calls
and need to be closed in parenthesis if you want to pipe to another function like printf
:
{{param "copyright" ((date.Year) | printf "Copyright %d") "What is the copyright year?"}}
To require an integer when prompting for the copyright
parameter,
a better example is to pass the int
that date.Year
returns:
Copyright {{param "copyright" (date.Year) "What is the copyright year?"}}
Licensed under the MIT license.