gosubst is an envsubst on steroids. Receives a template file in the stdin replaces all variables and sends it to stdout.
Get the latest gosubst for your platform on the releases page
curl -o /usr/local/bin/gosubst -sSL https://github.com/luizbafilho/gosubst/releases/download/<version>/gosubst_<os>-<arch>
chmod +x /usr/local/bin/gosubst
gosubst copies the stdin to stdout replacing all variables.
gosubst copies stardard input to standard output replacing all variables present in values file
Usage:
gosubst [flags]
Flags:
--type string values type (toml, yaml or json) (default "yaml")
-v, --values string values fileValues file sample:
foo: "bar"
baz:
boo: "bla"
zoo: "mee"Template file sample:
Sample access: {{.foo}}
Accessing variables: {{.baz.boo}} | {{.baz.zoo}}
$ gosubst -f values.yaml < template.conf
Sample access: bar
Accessing variables: bla | meeThe values file provides all the values to be replaced when the template is processed. gosubst supports yaml, json or toml files.
gosubst makes all environment variables available in the template.
$ echo "Home path: {{.Env.HOME}}" | gosubst
Home path: /home/vagrantGiven that gosubst uses Go Templates you can leverage all the power Go provide. Here is a small sample:
{{with .Account -}}
Dear {{.FirstName}} {{.LastName}},
{{- end}}
Below are your account statement details for period from {{.FromDate}} to {{.ToDate}}.
{{if .Purchases -}}
Your purchases:
{{- range .Purchases }}
{{ .Date}} {{ printf "%-20s" .Description }} {{.AmountInCents -}}
{{- end}}
{{- else}}
You didn't make any purchases during the period.
{{- end}}
{{$note := .Account.Note -}}
{{if $note -}}
Note: {{$note}}
{{- end}}
Best Wishes,
Customer Service
For more details: https://golang.org/pkg/text/template/#hdr-Text_and_spaces
gosubst makes available a lot of usefull functions provided by Sprig
$ echo "generated-id: {{uuidv4}}" | gosubst
generated-id: 00a315b7-c846-4751-b2ee-b50eb5359bac
$ echo "generated-sha: {{sha256sum .Env.USER | trunc 7}}" | gosubst
generated-sha: 4b569e8Check it out all functions available: https://github.com/Masterminds/sprig#functions