### Get Environment Variable Examples Source: https://github.com/hairyhenderson/gomplate/blob/main/docs/content/functions/env.md Demonstrates retrieving environment variables with and without defaults, including the `_FILE` fallback for secrets. ```console $ gomplate -i 'Hello, {{env.Getenv "USER"}}' Hello, hairyhenderson ``` ```console $ gomplate -i 'Hey, {{getenv "FIRSTNAME" "you"}}!' Hey, you! ``` ```console $ echo "safe" > /tmp/mysecret $ export SECRET_FILE=/tmp/mysecret $ gomplate -i 'Your secret is {{getenv "SECRET"}}' Your secret is safe ``` -------------------------------- ### Install gomplate with `go install` Source: https://github.com/hairyhenderson/gomplate/blob/main/docs/content/installing.md For Go developers, install gomplate directly using `go install`. Note that this binary may not be versioned. ```bash $ go install github.com/hairyhenderson/gomplate/v5/cmd/gomplate@latest $ gomplate --help ... (note that this method produces a binary that isn't versioned and may not necessarily work correctly) ``` -------------------------------- ### Complex Example with Multiple Datasources and Includes Source: https://github.com/hairyhenderson/gomplate/blob/main/docs/content/_index.md A comprehensive example showcasing the use of environment variables, remote datasources, and the `include` function for complex template rendering. ```console $ # and here's something a little more complicated: $ export CITIES='city: [London, Johannesburg, Windhoek]' $ cat in.tmpl {{ range $i, $city := (ds "cities").city -}} {{ add 1 $i }}: {{ include "weather" (print $city "?0") }} {{ end }} $ gomplate -d 'cities=env:///CITIES?type=application/yaml' -d 'weather=https://wttr.in/?0' -H 'weather=User-Agent: curl' -f in.tmpl 1: Weather report: London \ / Partly cloudy _ /"নার-. 4-7 °C \_( ). ↑ 20 km/h /(___(__) 10 km 0.0 mm 2: Weather report: Johannesburg \ / Partly cloudy _ /"নার-. 15 °C \_( ). ↘ 0 km/h /(___(__) 10 km 2.2 mm 3: Weather report: Windhoek \ / Partly cloudy _ /"নার-. 20 °C \_( ). ↑ 6 km/h /(___(__) 20 km 0.0 mm ``` -------------------------------- ### Install gomplate with tea.xyz Source: https://github.com/hairyhenderson/gomplate/blob/main/docs/content/installing.md Users familiar with tea.xyz can install gomplate using this command, often useful in GitHub Actions. ```bash $ sh <(curl https://tea.xyz) +gomplate.ca sh $ gomplate --version ... ``` -------------------------------- ### Complex Example with Multiple Datasources and Includes Source: https://github.com/hairyhenderson/gomplate/blob/main/README.md A more advanced example combining environment variables, remote URLs, and template includes. It fetches weather data for a list of cities defined in an environment variable. ```console $ export CITIES='city: [London, Johannesburg, Windhoek]' $ cat in.tmpl {{ range $i, $city := (ds "cities").city -}} {{ add 1 $i }}: {{ include "weather" (print $city "?0") }} {{ end }} $ gomplate -d 'cities=env:///CITIES?type=application/yaml' -d 'weather=https://wttr.in/?0' -H 'weather=User-Agent: curl' -f in.tmpl 1: Weather report: London \ / Partly cloudy _ /"..- 4-7 °C \_( ). ↑ 20 km/h /(___(__) 10 km 0.0 mm 2: Weather report: Johannesburg \ / Partly cloudy _ /"..- 15 °C \_( ). ↘ 0 km/h /(___(__) 10 km 2.2 mm 3: Weather report: Windhoek \ / Partly cloudy _ /"..- 20 °C \_( ). ↑ 6 km/h /(___(__) 20 km 0.0 mm ``` -------------------------------- ### Install gomplate with Homebrew on macOS/Linux Source: https://github.com/hairyhenderson/gomplate/blob/main/docs/content/installing.md Use this command for the simplest installation on macOS and Linux systems with Homebrew. ```bash $ brew install gomplate ... ==> Installing gomplate ==> Pouring gomplate-3.8.0.x86_64_linux.bottle.tar.gz ➠ /home/linuxbrew/.linuxbrew/Cellar/gomplate/3.8.0: 6 files, 7.8MB ``` -------------------------------- ### Generate YescryptMCF Hash Examples Source: https://github.com/hairyhenderson/gomplate/blob/main/docs/content/functions/crypto.md Examples of using crypto.YescryptMCF to hash strings with different parameter combinations. The first example uses default parameters, while subsequent examples specify cost, block size, and/or salt. ```console $ gomplate -i '{{ "foo" | crypto.YescryptMCF }}' $y$jB5$YZZJoNING3pMhVIKFhIJJ/$n.9CRZ17bqvvDKyrAWqvghg7k5rq9M9F4rpWntnpeV0 ``` ```console $ gomplate -i '{{ crypto.YescryptMCF 10 1 "mysalt" "foo" }}' $y$j7.$hZrQVl4R$y2MSQDqiVCS0Q7PsGv7f8b4O7s/O0Kmgw.2hvgxbqL1 ``` ```console $ gomplate -i '{{ "pass" | crypto.YescryptMCF 16 4 }}' $y$jD1$g/5AI/JEAR1KV7bFkJqIu/$zRk92xeixtHGp8Mq6weG2tPlsQPDZ6ybweLJd6jv1w6 ``` ```console $ gomplate -i '{{ crypto.YescryptMCF 16 4 "pass" }}' $y$jD1$F7IQJtaHnk4Bo3HSYNIAi/$ttB6RTXx2SnSJIn1xsUPlaQmH056JdgejJlZmHyCtq1 ``` -------------------------------- ### Install gomplate with mise Source: https://github.com/hairyhenderson/gomplate/blob/main/docs/content/installing.md If you use the mise tool version manager, install gomplate with this command. ```bash mise use -g gomplate@latest ``` -------------------------------- ### Basic Action Example Source: https://github.com/hairyhenderson/gomplate/blob/main/docs/content/syntax.md Demonstrates a simple action within a template. Actions are delimited by {{ and }} and replaced with their output. ```go-template Hello, {{ print "World" }}! ``` -------------------------------- ### Example Configuration File Source: https://github.com/hairyhenderson/gomplate/blob/main/docs/content/config.md A sample YAML configuration file demonstrating various settings like input/output directories, datasources, and plugins. ```yaml inputDir: in/ outputDir: out/ datasources: local: url: file:///tmp/data.json remote: url: https://example.com/api/v1/data header: Authorization: ["Basic aGF4MHI6c3dvcmRmaXNoCg=="] plugins: dostuff: /usr/local/bin/stuff.sh ``` -------------------------------- ### Datasource Configuration Example Source: https://github.com/hairyhenderson/gomplate/blob/main/docs/content/config.md Define datasources with their URLs and optional HTTP headers for use in templates. ```yaml datasources: data: url: https://example.com/api/v1/data header: Authorization: ["Basic aGF4MHI6c3dvcmRmaXNoCg=="] stuff: url: stuff.yaml ``` -------------------------------- ### Generate Bcrypt Hash Example Source: https://github.com/hairyhenderson/gomplate/blob/main/docs/content/functions/crypto.md Demonstrates how to use the crypto.Bcrypt function to hash a string. The first example uses the default cost, while the second specifies a cost of 4. ```console $ gomplate -i '{{ "foo" | crypto.Bcrypt }}' $2a$10$jO8nKZ1etGkKK7I3.vPti.fYDAiBqwazQZLUhaFoMN7MaLhTP0SLy ``` ```console $ gomplate -i '{{ crypto.Bcrypt 4 "foo" }}' $2a$04$zjba3N38sjyYsw0Y7IRCme1H4gD0MJxH8Ixai0/sgsrf7s1MFUK1C ``` -------------------------------- ### Access All Environment Variables Examples Source: https://github.com/hairyhenderson/gomplate/blob/main/docs/content/functions/env.md Shows how to access all environment variables using the `env.Env` map. ```console $ gomplate -i 'Hello, {{env.Env.USER}}' Hello, hairyhenderson ``` ```console $ gomplate -i '{{ env.Env.HOME }}' /home/hairyhenderson ``` -------------------------------- ### Include IPv4 interfaces and get names using sockaddr.Include and sockaddr.Attr Source: https://github.com/hairyhenderson/gomplate/blob/main/docs/content/functions/sockaddr.md This example demonstrates how to first include only IPv4 interfaces using `sockaddr.Include` and then extract their names using `sockaddr.Attr`. It iterates over the filtered results. ```console $ gomplate -i '{{ range (sockaddr.GetAllInterfaces | sockaddr.Include "type" "ipv4") }}{{ . | sockaddr.Attr "name" }} {{end}}' ``` -------------------------------- ### Derive Ed25519 Public Key Examples Source: https://github.com/hairyhenderson/gomplate/blob/main/docs/content/functions/crypto.md Examples demonstrating the derivation of an Ed25519 public key. The first example derives from a generated key, and the second uses an included private key file. ```console $ gomplate -i '{{ crypto.Ed25519GenerateKey | crypto.Ed25519DerivePublicKey }}' -----BEGIN PUBLIC KEY----- ... ``` ```console $ gomplate -d key=priv.pem -i '{{ crypto.Ed25519DerivePublicKey (include "key") }}' -----BEGIN PUBLIC KEY----- ...PK ``` -------------------------------- ### Using URL-Based Datasources Source: https://github.com/hairyhenderson/gomplate/blob/main/README.md Shows how to fetch data from a remote URL, such as an API endpoint. This example uses `ipinfo.io` to get the country code. ```console $ gomplate -d ip=https://ipinfo.io -i 'country code: {{ (ds "ip").country }}' country code: CA ``` -------------------------------- ### Manual installation of gomplate Source: https://github.com/hairyhenderson/gomplate/blob/main/docs/content/installing.md Download the latest binary from the releases page, place it in your PATH, and ensure it's executable. Test with `gomplate --help`. ```bash $ curl -o /usr/local/bin/gomplate -sSL https://github.com/hairyhenderson/gomplate/releases/download//gomplate_- $ chmod 755 /usr/local/bin/gomplate $ gomplate --help ... ``` -------------------------------- ### Context Configuration Example Source: https://github.com/hairyhenderson/gomplate/blob/main/docs/content/config.md Configure datasources to be added to the default context, including URLs and optional HTTP headers. ```yaml context: data: url: https://example.com/api/v1/data header: Authorization: ["Basic aGF4MHI6c3dvcmRmaXNoCg=="] stuff: url: stuff.yaml ``` -------------------------------- ### Set Entire Context Example Source: https://github.com/hairyhenderson/gomplate/blob/main/docs/content/config.md A shorthand for setting the entire context using a single datasource. ```yaml context: .: url: data.toml ``` -------------------------------- ### Example: KebabCase Usage Source: https://github.com/hairyhenderson/gomplate/blob/main/docs/content/functions/strings.md Demonstrates converting a string to kebab-case using the `strings.KebabCase` function. ```console $ gomplate -i '{{ "Hello, World!" | strings.KebabCase }}' Hello-world ``` ```console $ gomplate -i '{{ "hello jello" | strings.KebabCase }}' hello-jello ``` -------------------------------- ### Example .env File Syntax Source: https://github.com/hairyhenderson/gomplate/blob/main/docs/content/datasources.md Demonstrates the syntax for .env files, including quoted values, shell escapes, variable expansion, and ignored comments/blank lines. ```bash FOO=a regular unquoted value export BAR=another value, exports are ignored # comments are totally ignored, as are blank lines FOO.BAR = "values can be double-quoted, and shell escapes are supported" BAZ="variable expansion: ${FOO}" QUX='single quotes ignore $variables and newlines' ``` -------------------------------- ### Example: Generate and Display UUIDs Source: https://github.com/hairyhenderson/gomplate/blob/main/docs/content/functions/uuid.md Demonstrates generating version 1 and version 4 UUIDs using gomplate's CLI. ```console $ gomplate -i '{{ uuid.V1 }}' 4d757e54-446d-11e9-a8fa-72000877c7b0 ``` ```console $ gomplate -i '{{ uuid.V4 }}' 40b3c2d2-e491-4b19-94cd-461e6fa35a60 ``` ```console $ gomplate -i '{{ uuid.Nil }}' 00000000-0000-0000-0000-000000000000 ``` -------------------------------- ### Offset and get addresses using sockaddr.Limit, sockaddr.Offset, and sockaddr.Attr Source: https://github.com/hairyhenderson/gomplate/blob/main/docs/content/functions/sockaddr.md This example first limits the interfaces to two, then applies an offset of one to skip the first interface, and finally extracts the 'address' attribute of the remaining interface. ```console $ gomplate -i '{{ sockaddr.GetAllInterfaces | sockaddr.Limit 2 | sockaddr.Offset 1 | sockaddr.Attr "address" }}' ``` -------------------------------- ### Vault AWS Auth Backend Example Source: https://github.com/hairyhenderson/gomplate/blob/main/docs/content/datasources.md Example of using the AWS auth backend with Vault. This requires setting `VAULT_AUTH_AWS_NONCE_FILE` and `VAULT_AUTH_AWS_NONCE_OUTPUT` environment variables. ```console $ export VAULT_AUTH_AWS_NONCE_FILE=/tmp/vault-aws-nonce $ export VAULT_AUTH_AWS_NONCE_OUTPUT=$VAULT_AUTH_AWS_NONCE_FILE $ gomplate -d vault=vault:///secret/foo -i '{{ (ds "vault").value }}' ... ``` -------------------------------- ### Example: RSA Decrypt and Print Source: https://github.com/hairyhenderson/gomplate/blob/main/docs/content/functions/crypto.md Demonstrates decrypting a string encrypted with `crypto.RSAEncrypt` using a private key and printing the result. ```console $ gomplate -c pubKey=./testPubKey -c privKey=./testPrivKey \ -i '{{ $enc := "hello" | crypto.RSAEncrypt .pubKey -}} \ {{ crypto.RSADecrypt .privKey $enc }}' hello ``` -------------------------------- ### Install gomplate with MacPorts on macOS Source: https://github.com/hairyhenderson/gomplate/blob/main/docs/content/installing.md For macOS users who prefer MacPorts, use this command for installation. ```bash $ sudo port install gomplate ``` -------------------------------- ### Render Directory Datasource Example Source: https://github.com/hairyhenderson/gomplate/blob/main/docs/content/datasources.md Demonstrates how to render values from a directory datasource, such as Vault, by iterating through its keys and accessing individual values. ```go {{ range (datasource "config") -}} {{ . }} = {{ (datasource "config" .).value }} {{- end }} ``` ```bash $ gomplate -d config=vault:///secret/configs/ -f template.tmpl one = v1 two = v2 three = v3 ``` -------------------------------- ### Example: CamelCase Usage Source: https://github.com/hairyhenderson/gomplate/blob/main/docs/content/functions/strings.md Demonstrates converting a string to CamelCase using the `strings.CamelCase` function. ```console $ gomplate -i '{{ "Hello, World!" | strings.CamelCase }}' HelloWorld ``` ```console $ gomplate -i '{{ "hello jello" | strings.CamelCase }}' helloJello ``` -------------------------------- ### Example: SnakeCase Usage Source: https://github.com/hairyhenderson/gomplate/blob/main/docs/content/functions/strings.md Demonstrates converting a string to snake_case using the `strings.SnakeCase` function. ```console $ gomplate -i '{{ "Hello, World!" | strings.SnakeCase }}' Hello_world ``` ```console $ gomplate -i '{{ "hello jello" | strings.SnakeCase }}' hello_jello ``` -------------------------------- ### Install gomplate with Chocolatey on Windows Source: https://github.com/hairyhenderson/gomplate/blob/main/docs/content/installing.md The recommended method for installing gomplate on Windows using the Chocolatey package manager. ```powershell choco install gomplate ``` -------------------------------- ### Example: Parse UUID and Access Properties Source: https://github.com/hairyhenderson/gomplate/blob/main/docs/content/functions/uuid.md Demonstrates parsing a UUID string and accessing its version and variant properties. ```console $ gomplate -i '{{ $u := uuid.Parse uuid.V4 }}{{ $u.Version }}, {{ $u.Variant}}' VERSION_4, RFC4122 ``` ```console $ gomplate -i '{{ (uuid.Parse "000001f5-4470-21e9-9b00-72000877c7b0").Domain }}' Person ``` -------------------------------- ### Install gomplate with npm Source: https://github.com/hairyhenderson/gomplate/blob/main/docs/content/installing.md Node.js developers can install gomplate using npm, even though it's a Go application. ```bash $ npm install -g gomplate ... ``` -------------------------------- ### Example: Splitting a comma-separated string Source: https://github.com/hairyhenderson/gomplate/blob/main/docs/content/functions/strings.md Demonstrates splitting a comma-separated string into individual elements using `strings.Split` and iterating over the result. ```go $ gomplate -i '{{range ("Bart,Lisa,Maggie" | strings.Split "," ) }}Hello, {{.}} {{end}}' Hello, Bart Hello, Lisa Hello, Maggie ``` -------------------------------- ### Install gomplate on Alpine Linux Source: https://github.com/hairyhenderson/gomplate/blob/main/docs/content/installing.md Install gomplate from Alpine's community repository using the apk package manager. Note that this version might lag behind the latest release. ```bash $ apk add --no-cache gomplate ... ``` -------------------------------- ### Example: TrimSpace Usage Source: https://github.com/hairyhenderson/gomplate/blob/main/docs/content/functions/strings.md Demonstrates trimming leading and trailing whitespace from a string using the `strings.TrimSpace` function. ```console $ gomplate -i '{{ " \n\t foo" | strings.TrimSpace }}' foo ``` -------------------------------- ### Expand Environment Variables Examples Source: https://github.com/hairyhenderson/gomplate/blob/main/docs/content/functions/env.md Illustrates expanding environment variables within strings, including the `_FILE` fallback for secrets and reading file content. ```console $ gomplate -i '{{env.ExpandEnv "Hello $USER"}}' Hello, hairyhenderson ``` ```console $ gomplate -i 'Hey, {{env.ExpandEnv "Hey, ${FIRSTNAME}!"}}' Hey, you! ``` ```console $ echo "safe" > /tmp/mysecret $ export SECRET_FILE=/tmp/mysecret $ gomplate -i '{{env.ExpandEnv "Your secret is $SECRET"}}' Your secret is safe ``` ```console $ gomplate -i '{{env.ExpandEnv (file.Read "foo")}}' contents of file "foo"... ``` -------------------------------- ### Example: Repeating a string Source: https://github.com/hairyhenderson/gomplate/blob/main/docs/content/functions/strings.md Uses `strings.Repeat` to create a new string by concatenating the input string "hello " five times. ```go $ gomplate -i '{{ "hello " | strings.Repeat 5 }}' hello hello hello hello hello ``` -------------------------------- ### AWS ARN Example Source: https://github.com/hairyhenderson/gomplate/blob/main/docs/content/functions/aws.md Illustrates how to display the AWS ARN associated with the current credentials using the `aws.ARN` function. ```bash $ gomplate -i 'Calling from {{ aws.ARN }}' Calling from arn:aws:iam::123456789012:user/Alice ``` -------------------------------- ### Generate Ed25519 Private Key from Seed Example Source: https://github.com/hairyhenderson/gomplate/blob/main/docs/content/functions/crypto.md Example of generating an Ed25519 private key from a base64 encoded seed. ```console $ gomplate -i '{{ crypto.Ed25519GenerateKeyFromSeed "base64" "MDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDA=" }}' -----BEGIN PRIVATE KEY----- ... ``` -------------------------------- ### Example: Splitting a string with a specific separator Source: https://github.com/hairyhenderson/gomplate/blob/main/docs/content/functions/strings.md Shows how to use `strings.Split` to divide a string into parts using a comma as the delimiter and printing each part on a new line. ```go $ gomplate -i '{{range strings.Split "," "One,Two,Three" }}{{.}}{{"\n"}}{{end}}' One Two Three ``` -------------------------------- ### Run gomplate with Docker Source: https://github.com/hairyhenderson/gomplate/blob/main/docs/content/installing.md Execute gomplate using a Docker image. This example shows how to check the version. ```bash $ docker run hairyhenderson/gomplate:stable --version gomplate version 3.9.0 ``` -------------------------------- ### Generate Yescrypt Hash Example Source: https://github.com/hairyhenderson/gomplate/blob/main/docs/content/functions/crypto.md Demonstrates using the crypto.Yescrypt function with all required parameters: input, salt, cost, block size, and key length. ```console $ gomplate -i '{{ crypto.Yescrypt "foo" "mysalt" 32768 8 32 }}' 8d967e29526ea6b57b914e7df477cd2c2b6804816ba68d47284d1f75aa2a617e ``` -------------------------------- ### Example: Trunc Usage Source: https://github.com/hairyhenderson/gomplate/blob/main/docs/content/functions/strings.md Demonstrates truncating a string to a specified length using the `strings.Trunc` function. ```console $ gomplate -i '{{ "hello, world" | strings.Trunc 5 }}' hello ``` -------------------------------- ### Example: Quoting a string Source: https://github.com/hairyhenderson/gomplate/blob/main/docs/content/functions/strings.md Demonstrates the `quote` alias for `strings.Quote` to enclose a string literal in double quotes. ```go $ gomplate -i '{{ "in" | quote }}' "in" ``` -------------------------------- ### Use gomplate with Docker and data sources Source: https://github.com/hairyhenderson/gomplate/blob/main/docs/content/installing.md Example of piping data to gomplate running in Docker, mounting a token, and using environment variables and a vault data source. ```bash $ echo 'My voice is my {{.Env.THING}}. {{(datasource "vault").value}}' | docker run -i -e THING=passport -v /home/me/.vault-token:/root/.vault-token hairyhenderson/gomplate -d vault=vault:///secret/sneakers -f - My voice is my passport. Verify me. ``` -------------------------------- ### Example: RSA Decrypt Bytes to String Source: https://github.com/hairyhenderson/gomplate/blob/main/docs/content/functions/crypto.md Shows how to decrypt RSA-encrypted data to a byte array and then convert it to a string using `conv.ToString`. ```console $ gomplate -c pubKey=./testPubKey -c privKey=./testPrivKey \ -i '{{ $enc := "hello" | crypto.RSAEncrypt .pubKey -}} \ {{ crypto.RSADecryptBytes .privKey $enc | conv.ToString }}' hello ``` -------------------------------- ### Example: TrimSuffix Usage Source: https://github.com/hairyhenderson/gomplate/blob/main/docs/content/functions/strings.md Demonstrates removing a specific suffix from a string using the `strings.TrimSuffix` function. ```console $ gomplate -i '{{ "hello, world" | strings.TrimSuffix "world" }}jello' hello, jello ``` -------------------------------- ### Check if Environment Variable Exists Examples Source: https://github.com/hairyhenderson/gomplate/blob/main/docs/content/functions/env.md Demonstrates checking for the existence of environment variables, including those set to an empty string. ```console $ gomplate -i '{{if env.HasEnv "FOO"}}FOO is set{{else}}FOO is not set{{end}}' FOO is not set ``` ```console $ FOO=bar gomplate -i '{{if env.HasEnv "FOO"}}FOO is set{{else}}FOO is not set{{end}}' FOO is set ``` ```console $ EMPTY= gomplate -i '{{if env.HasEnv "EMPTY"}}EMPTY is set{{end}}' EMPTY is set ``` ```console $ gomplate -i '{{ "USER" | env.HasEnv }}' true ``` -------------------------------- ### Example: Quoting a number Source: https://github.com/hairyhenderson/gomplate/blob/main/docs/content/functions/strings.md Shows how `strings.Quote` converts a numeric input to its string representation before enclosing it in double quotes. ```go $ gomplate -i '{{ strings.Quote 500 }}' "500" ``` -------------------------------- ### AWS KMS Encryption and Decryption Example Source: https://github.com/hairyhenderson/gomplate/blob/main/docs/content/functions/aws.md Demonstrates a common workflow of encrypting a string using `aws.KMSEncrypt` and then decrypting it using `aws.KMSDecrypt` via environment variables. ```bash $ export CIPHER=$(gomplate -i '{{ aws.KMSEncrypt "alias/gomplate" "hello world" }}') $ gomplate -i '{{ env.Getenv "CIPHER" | aws.KMSDecrypt }}' ``` ```bash $ export CIPHER=$(gomplate -i '{{ aws.KMSEncrypt "alias/gomplate" "hello world" }}') $ gomplate -i '{{ env.Getenv "CIPHER" | aws.KMSDecrypt }}' ``` -------------------------------- ### Example: Check UUID Validity Source: https://github.com/hairyhenderson/gomplate/blob/main/docs/content/functions/uuid.md Shows how to use `uuid.IsValid` to check if a string is a valid UUID format. ```console $ gomplate -i '{{ if uuid.IsValid "totally invalid" }}valid{{ else }}invalid{{ end }}' invalid ``` ```console $ gomplate -i '{{ uuid.IsValid "urn:uuid:12345678-90ab-cdef-fedc-ba9876543210" }}' true ``` -------------------------------- ### Example: RSA Decrypt to Bytes Array Source: https://github.com/hairyhenderson/gomplate/blob/main/docs/content/functions/crypto.md Demonstrates decrypting RSA-encrypted data and outputting the result as a byte array, shown as a list of integers. ```console $ gomplate -c pubKey=./testPubKey -c privKey=./testPrivKey \ -i '{{ $enc := "hello" | crypto.RSAEncrypt .pubKey -}} \ {{ crypto.RSADecryptBytes .privKey $enc }}' [104 101 108 108 111] ``` -------------------------------- ### Limit and join interface names using sockaddr.Limit and sockaddr.Join Source: https://github.com/hairyhenderson/gomplate/blob/main/docs/content/functions/sockaddr.md This example uses `sockaddr.Limit` to take only the first two interfaces and then `sockaddr.Join` to concatenate their names with a pipe symbol. ```console $ gomplate -i '{{ sockaddr.GetAllInterfaces | sockaddr.Limit 2 | sockaddr.Join "name" "|" }}' ``` -------------------------------- ### Example: Limiting splits with strings.SplitN Source: https://github.com/hairyhenderson/gomplate/blob/main/docs/content/functions/strings.md Illustrates using `strings.SplitN` to split a string by a colon, limiting the output to two parts. The remainder of the string is included in the second part. ```go $ gomplate -i '{{ range ("foo:bar:baz" | strings.SplitN ":" 2) }}{{.}} {{end}}' foo bar:baz ``` -------------------------------- ### Reference a Template File with an Alias and Path Source: https://github.com/hairyhenderson/gomplate/blob/main/docs/content/usage.md This example demonstrates referencing a template file using both an alias and its path. The template will be accessible via the specified alias. ```console $ gomplate --template t=foo/bar/helloworld.tmpl -i 'here are the contents of the template: [ {{ template "t" }} ]' here are the contents of the template: [ hello, world! ] ``` -------------------------------- ### Example: Replacing dots with hyphens (pipe) Source: https://github.com/hairyhenderson/gomplate/blob/main/docs/content/functions/strings.md Shows `strings.ReplaceAll` used with the pipe operator to replace all dots (`.`) with hyphens (`-`) in an IP address string. ```go $ gomplate -i '{{ "172.21.1.42" | strings.ReplaceAll "." "-" }}' 172-21-1-42 ``` -------------------------------- ### Example: Base64 Decrypt RSA Input Source: https://github.com/hairyhenderson/gomplate/blob/main/docs/content/functions/crypto.md Shows how to decrypt base64-encoded RSA ciphertext. It first decodes the base64 input using `base64.DecodeBytes` and then decrypts it with `crypto.RSADecrypt`. ```console $ export ENCRYPTED="ScTcX1NZ6p/EeDIf6R7FKLcDFjvP98YgiBhyhPE4jtehajIyTKP1GL8C72qbAWrgdQ6A2cSVjoyo3viqf/PZxpcBDUUMDJuemTaJqUUjMWaDuPG37mQbmRtcvFTuUhw1qSbKyHorDOgTX5d4DvWV4otycGtBT6dXhnmmb5V72J/w3z68vtTJ21m9wREFD7LrYVHdFFtRZiIyMBAF0ngQ+hcujrxilnmgzPkEAg6E7Ccctn28Ie2c4CojrwRbNNxXNlIWCCkC/8Vq8qlDfZ70a+BsTmJDuScE6BZbTyteo9uGYrLn+bTIHNDj90AeLCKUTyWLUJ5Edi9LhlKVBoJUNQ==" $ gomplate -c ciphertext=env:///ENCRYPTED -c privKey=./testPrivKey \ -i '{{ base64.DecodeBytes .ciphertext | crypto.RSADecrypt .privKey }}' hello ``` -------------------------------- ### Example: Replacing dots with hyphens (function call) Source: https://github.com/hairyhenderson/gomplate/blob/main/docs/content/functions/strings.md Demonstrates `strings.ReplaceAll` by replacing all occurrences of a dot (`.`) with a hyphen (`-`) in an IP address string using a direct function call. ```go $ gomplate -i '{{ strings.ReplaceAll "." "-" "172.21.1.42" }}' 172-21-1-42 ``` -------------------------------- ### List all IPv4 addresses on the system Source: https://github.com/hairyhenderson/gomplate/blob/main/docs/content/functions/sockaddr.md This example demonstrates how to list all IPv4 addresses available on the system using a pipeline of sockaddr functions. It pipes the result of GetAllInterfaces through Include to filter by type and then uses Attr to extract the address. ```go {{ range (sockaddr.GetAllInterfaces | sockaddr.Include "type" "ipv4") -}} {{ . | sockaddr.Attr "address" }} {{end}} ``` -------------------------------- ### Reference a Template File in a Subdirectory by Path Source: https://github.com/hairyhenderson/gomplate/blob/main/docs/content/usage.md This example shows how to reference a template file located in a subdirectory, using its full path as the template name. ```console $ gomplate --template foo/bar/helloworld.tmpl -i 'here are the contents of the template: [ {{ template "foo/bar/helloworld.tmpl" }} ]' here are the contents of the template: [ hello, world! ] ``` -------------------------------- ### Set and Access Context with `with` Source: https://github.com/hairyhenderson/gomplate/blob/main/docs/content/syntax.md The `with` action sets the context for its block. Use `.` to access the current context within the block. This example demonstrates setting the context to a string. ```bash $ gomplate -i '{{ with "foo" }}The context is {{ . }}{{ end }}' ``` ```go-template {{ with "foo" }}The context is {{ . }}{{ end }} ``` -------------------------------- ### Get Time Zone Offset Source: https://github.com/hairyhenderson/gomplate/blob/main/docs/content/functions/time.md Use the `time.ZoneOffset` function to get the current time zone offset in seconds. ```console $ gomplate -i '{{time.ZoneOffset}}' -14400 ``` -------------------------------- ### Reference a Single Template File by Alias Source: https://github.com/hairyhenderson/gomplate/blob/main/docs/content/usage.md This example shows how to reference a single template file using an alias. The template is made available under the specified alias name. ```console $ gomplate --template foo=file:///tmp/foo.tmpl -i 'here are the contents of the template: [ {{ template "foo" }} ]' here are the contents of the template: [ hello, world! ] ``` -------------------------------- ### Generate ECDSA Public Key Example Source: https://github.com/hairyhenderson/gomplate/blob/main/docs/content/functions/crypto.md Demonstrates deriving a public key from a generated ECDSA private key. The output is in PEM-encoded PKIX ASN.1 DER form. ```console $ gomplate -i '{{ crypto.ECDSAGenerateKey | crypto.ECDSADerivePublicKey }}' -----BEGIN PUBLIC KEY----- ... ``` ```console $ gomplate -d key=priv.pem -i '{{ crypto.ECDSADerivePublicKey (include "key") }}' -----BEGIN PUBLIC KEY----- MIGbMBAGByqGSM49AgEGBSuBBAAjA4GGAAQBZvTS1wcCJSsGYQUVoSVctynkuhke kikB38iNwx/80jzdm+Z8OmRGlwH6OE9NX1MyxjvYMimhcj6zkaOKh1/HhMABrfuY +hIz6+EUt/Db51awO7iCuRly5L4TZ+CnMAsIbtUOqsqwSQDtv0AclAuogmCst75o aztsmrD79OXXnhUlURI= -----END PUBLIC KEY----- ``` -------------------------------- ### Get Nil UUID Source: https://github.com/hairyhenderson/gomplate/blob/main/docs/content/functions/uuid.md Returns the nil UUID (`00000000-0000-0000-0000-000000000000`), primarily for testing. ```go uuid.Nil ``` -------------------------------- ### sockaddr.Offset Source: https://github.com/hairyhenderson/gomplate/blob/main/docs/content/functions/sockaddr.md Returns a slice of IfAddrs, starting from a specified offset within the array. ```APIDOC ## sockaddr.Offset Returns a slice of `IfAddr`s based on the specified offset. ### Usage ``` sockaddr.Offset offset ``` ``` | sockaddr.Offset offset ``` ### Arguments | name | description | |------|-------------| | `offset` | _(required)_ the offset | | `` | _(required)_ the array of `IfAddr`s | ### Examples ```console $ gomplate -i '{{ sockaddr.GetAllInterfaces | sockaddr.Limit 2 | sockaddr.Offset 1 | sockaddr.Attr "address" }}' ::1 ``` ``` -------------------------------- ### Basic Math Operations with Environment Variables Source: https://github.com/hairyhenderson/gomplate/blob/main/docs/content/functions/math.md Demonstrates using math functions with environment variables and various number formats. Ensure the environment variable is set before execution. ```console $ NUM=50 gomplate -i '{{ div (getenv "NUM") 10 }}' 5 ``` ```console $ gomplate -i '{{ add "0x2" "02" "2.0" "2e0" }}' 8 ``` ```console $ gomplate -i '{{ add 2.5 2.5 }}' 5.0 ``` -------------------------------- ### Pipelining with String Split Source: https://github.com/hairyhenderson/gomplate/blob/main/CONTRIBUTING.md Demonstrates natural pipelining for functions with multiple arguments. The `strings.Split` function is used here, taking the string to split as the first argument. ```go {{ "one,two,three" | strings.Split "," }} ``` -------------------------------- ### Format current time using Kitchen layout Source: https://github.com/hairyhenderson/gomplate/blob/main/docs/content/functions/time.md Demonstrates formatting the current time using the predefined `time.Kitchen` layout. Also shows adding hours to the current time and formatting it. ```console $ gomplate -i '{{ (time.Now).Format time.Kitchen }} {{ ((time.Now).Add (time.Hour 2)).Format time.Kitchen }}' 9:05AM 11:05AM ``` -------------------------------- ### aws.EC2Region Source: https://github.com/hairyhenderson/gomplate/blob/main/docs/content/functions/aws.md Queries AWS to get the region. An optional default can be provided, or returns 'unknown' if it cannot be determined. ```APIDOC ## aws.EC2Region **Alias:** `ec2region` Queries AWS to get the region. An optional default can be provided, or returns `unknown` if it can't be determined for some reason. ### Usage ``` aws.EC2Region [default] ``` ### Arguments | name | description | |------|-------------| | `default` | _(optional)_ the default value | ### Examples _In EC2_ ```console $ echo '{{ aws.EC2Region }}' | ./gomplate us-east-1 ``` _Not in EC2_ ```console $ echo '{{ aws.EC2Region }}' | ./gomplate unknown $ echo '{{ aws.EC2Region "foo" }}' | ./gomplate foo ``` ``` -------------------------------- ### Initialize Grafana Faro Web SDK and Tracing Source: https://github.com/hairyhenderson/gomplate/blob/main/docs/layouts/partials/custom-footer.html This script dynamically loads the Grafana Faro Web SDK and its tracing instrumentation. It configures the SDK with a collector URL and application details, then adds the tracing instrumentation. Append this script to the HTML head to enable performance monitoring. ```javascript (function () { var webSdkScript = document.createElement("script"); webSdkScript.src = "https://unpkg.com/@grafana/faro-web-sdk@^1.4.0/dist/bundle/faro-web-sdk.iife.js"; webSdkScript.onload = () => { window.GrafanaFaroWebSdk.initializeFaro({ url: "https://faro-collector-prod-us-central-0.grafana.net/collect/d8b5652f5dd57fb1cfec9a1c32970fe0", app: { name: "docs.gomplate.ca", version: "1.0.0", environment: "production", }, }); var webTracingScript = document.createElement("script"); webTracingScript.src = "https://unpkg.com/@grafana/faro-web-tracing@^1.4.0/dist/bundle/faro-web-tracing.iife.js"; webTracingScript.onload = () => { window.GrafanaFaroWebSdk.faro.instrumentations.add( new window.GrafanaFaroWebTracing.TracingInstrumentation() ); }; document.head.appendChild(webTracingScript); }; document.head.appendChild(webSdkScript); })(); ``` -------------------------------- ### Get All EC2 Tags Source: https://github.com/hairyhenderson/gomplate/blob/main/docs/content/functions/aws.md Queries the AWS EC2 API to retrieve all user-defined tags and their values. ```console echo '{{ range $key, $value := aws.EC2Tags }}{{(printf "%s=%s\n" $key $value)}}{{ end }}' | ./gomplate Description=foo Name=bar svc:name=foobar ``` -------------------------------- ### Using a Gomplate Configuration File Source: https://github.com/hairyhenderson/gomplate/blob/main/docs/content/usage.md Shows how to specify a custom gomplate configuration file using the --config argument. The config file defines input templates and datasources. ```yaml in: hello {{ .data.thing }} datasources: data: url: https://example.com/data.json ``` ```bash $ cat myconfig.yaml in: hello {{ .data.thing }} datasources: data: url: https://example.com/data.json $ gomplate --config myconfig.yaml hello world ``` -------------------------------- ### Generate RSA Key, Derive Public Key, Encrypt, and Decrypt Source: https://github.com/hairyhenderson/gomplate/blob/main/docs/content/functions/crypto.md Demonstrates a full RSA workflow: generating a private key, deriving the corresponding public key, encrypting a message with the public key, and then decrypting it with the private key. The output should be the original message. ```console $ gomplate -i '{{ $key := crypto.RSAGenerateKey 2048 -}} {{ $pub := crypto.RSADerivePublicKey $key -}} {{ $enc := "hello" | crypto.RSAEncrypt $pub -}} {{ crypto.RSADecrypt $key $enc }}' hello ``` -------------------------------- ### Using Standard Input as a Datasource Source: https://github.com/hairyhenderson/gomplate/blob/main/README.md Illustrates how to pipe data into gomplate and use it as a datasource. The `stdin:///` prefix is used to specify standard input. ```console $ echo '{"cities":["London", "Johannesburg", "Windhoek"]}' | gomplate -d city=stdin:///in.json -i '{{ range (ds "city").cities }}{{.}}, {{end}}' London, Johannesburg, Windhoek, ``` -------------------------------- ### Get EC2 Region Source: https://github.com/hairyhenderson/gomplate/blob/main/docs/content/functions/aws.md Retrieves the AWS region. Returns 'unknown' if it cannot be determined, or a specified default value. ```console _In EC2_ ```console $ echo '{{ aws.EC2Region }}' | ./gomplate us-east-1 ``` _Not in EC2_ ```console $ echo '{{ aws.EC2Region }}' | ./gomplate unknown $ echo '{{ aws.EC2Region "foo" }}' | ./gomplate foo ``` -------------------------------- ### Exclude Processing Example Source: https://github.com/hairyhenderson/gomplate/blob/main/docs/content/config.md Configure patterns for files that should be copied directly to the output directory without template rendering. ```yaml excludeProcessing: - '*.jpg' ``` -------------------------------- ### Basic Environment Variable Usage Source: https://github.com/hairyhenderson/gomplate/blob/main/docs/content/_index.md Demonstrates the most basic usage of gomplate by rendering a template that includes an environment variable. ```console $ # at its most basic, gomplate can be used with environment variables... $ echo 'Hello, {{ env.Getenv "USER" }}' | gomplate Hello, hairyhenderson ``` -------------------------------- ### Exclude Patterns Example Source: https://github.com/hairyhenderson/gomplate/blob/main/docs/content/config.md Specify exclude patterns for files, with support for negating exclusions to include specific files. ```yaml excludes: - '*.txt' - '!include-this.txt' ``` -------------------------------- ### Basic Usage with Environment Variables Source: https://github.com/hairyhenderson/gomplate/blob/main/README.md Demonstrates the most basic usage of gomplate by rendering a template that uses an environment variable. Ensure the environment variable is set before execution. ```console $ echo 'Hello, {{ .Env.USER }}' | gomplate Hello, hairyhenderson ``` -------------------------------- ### AWS User ID Example Source: https://github.com/hairyhenderson/gomplate/blob/main/docs/content/functions/aws.md Demonstrates how to retrieve and display the unique identifier of the calling entity using the `aws.UserID` function. ```bash $ gomplate -i 'I am {{ aws.UserID }}' I am AIDACKCEVSQ6C2EXAMPLE ``` -------------------------------- ### AWS Account ID Example Source: https://github.com/hairyhenderson/gomplate/blob/main/docs/content/functions/aws.md Shows how to embed the current AWS account ID into a string using the `aws.Account` function. ```bash $ gomplate -i 'My account is {{ aws.Account }}' My account is 123456789012 ``` -------------------------------- ### Sort network interfaces by name and address (descending) Source: https://github.com/hairyhenderson/gomplate/blob/main/docs/content/functions/sockaddr.md This example demonstrates sorting network interfaces first by interface name (ascending) and then by address (descending). The Sort function takes a comma-delimited list of selectors, where a '-' prefix indicates descending order. ```go sockaddr.GetAllInterfaces | sockaddr.Sort "name,-address" ``` -------------------------------- ### Get Time Zone Name Source: https://github.com/hairyhenderson/gomplate/blob/main/docs/content/functions/time.md Retrieves the name of the local system's time zone. Added in gomplate v2.1.0. ```go time.ZoneName ``` -------------------------------- ### Using Standard Input as a Datasource Source: https://github.com/hairyhenderson/gomplate/blob/main/docs/content/_index.md Illustrates how to pipe JSON data into gomplate via standard input and use it as a datasource. ```console $ # standard input can be used as a datasource too: $ echo '{"cities":["London", "Johannesburg", "Windhoek"]}' | gomplate -d city=stdin:///in.json -i '{{ range (ds "city").cities }}{{.}}, {{end}}' London, Johannesburg, Windhoek, ``` -------------------------------- ### Get Relative Path Source: https://github.com/hairyhenderson/gomplate/blob/main/docs/content/functions/filepath.md Calculates the relative path from a base path to a target path. This is a wrapper for Go's `filepath.Rel`. ```console $ gomplate -i '{{ filepath.Rel "/a" "/a/b/c" }}' b/c ``` -------------------------------- ### Plugin Configuration with Arguments and Pipe Source: https://github.com/hairyhenderson/gomplate/blob/main/docs/content/config.md Configure custom functions (plugins) with their command, arguments, and piping behavior. `pipe: true` sends output to Stdin. ```yaml in: '{{ "world" | figlet | lolcat }}' plugins: figlet: cmd: /usr/local/bin/figlet args: - oh - hello pipe: true timeout: 1s lolcat: /home/hairyhenderson/go/bin/lolcat ``` -------------------------------- ### Encrypt and Decrypt using Provided Keys Source: https://github.com/hairyhenderson/gomplate/blob/main/docs/content/functions/crypto.md Encrypts a message using a derived public key and then decrypts it using a provided private key. This example assumes the private key is available via the -c flag. ```console $ gomplate -c privKey=./privKey.pem \ -i '{{ $pub := crypto.RSADerivePublicKey .privKey -}} {{ $enc := "hello" | crypto.RSAEncrypt $pub -}} {{ crypto.RSADecrypt .privKey $enc }}' hello ``` -------------------------------- ### Get AWS Account ID Source: https://github.com/hairyhenderson/gomplate/blob/main/docs/content/functions/aws.md Retrieves the currently authenticated AWS account ID. This function wraps the STS GetCallerIdentity API. ```go aws.Account ``` -------------------------------- ### Add Context Data with --context Source: https://github.com/hairyhenderson/gomplate/blob/main/docs/content/usage.md Add data sources in 'name=URL' form, making them available in the default context as '.'. The special name '.' overrides the entire default context. Data is immediately loaded. ```console $ gomplate --context post=https://jsonplaceholder.typicode.com/posts/2 -i 'post title is: {{ .post.title }}' ``` ```console $ gomplate -c .=http://xkcd.com/info.0.json -i '{{ .title }}' ``` -------------------------------- ### Using File-Based Datasources Source: https://github.com/hairyhenderson/gomplate/blob/main/README.md Demonstrates how to use a local YAML file as a datasource. The `datasource` function (or its alias `ds`) is used to access the data. ```yaml foo: bar: baz: qux ``` ```console $ gomplate -d config=./config.yaml -i 'the value we want is: {{ (datasource "config").foo.bar.baz }}' the value we want is: qux ```