Templating

Templates allow you to interpolate values from Flags, Arguments and Configuration into some aspects of commands.

For example the exec command type allows you to use templates to put arguments into the command being run.

We use the Go template language at the moment, it’s not the best we might look at something else later.

Only some fields are parsed through templates, the documentation for each command type will call out what is supported.

Reference

An example template use was shown in the exec documentation:

command: |
            {{ default .Config.Cowsay "cowsay" }} "{{ .Arguments.message | escape }}"

Here we have examples of accessing the .Config and .Arguments structures and using some functions.

Available Data

KeyDescription
.ConfigData stored in the configuration file for this application
.ArgumentsData supplied by users using command arguments
.FlagsData supplied by users using command flags
.InputParsed JSON input from a previous step, available in transform contexts only

Available Functions

FunctionDescriptionExample
requireAsserts that some data is available, errors with an optional custom message on failure{{ require .Config.Password "Password not set in the configuration" }}
escapeEscapes a string for use in shell arguments{{ escape .Arguments.message }}
read_fileReads a file{{ read_file .Arguments.file }}
defaultChecks a value, if its not supplied uses a default{{ default .Config.Cowsay "cowsay" }}
envReads an environment variable{{ env "HOME" }}
UserWorkingDirReturns the directory the user is in when running the command{{ UserWorkingDir }}
AppDirReturns the directory the application definition is in{{ AppDir }}
TaskDirAlias for AppDir{{ TaskDir }}

In addition to the above, the Sprig functions library is available in most template contexts including commands, scaffolds and transforms.