github.com/hairyhenderson/gomplate/v4@v4.0.0-pre-2.0.20240520121557-362f058f0c93/docs/content/functions/env.md (about) 1 --- 2 title: env functions 3 menu: 4 main: 5 parent: functions 6 --- 7 8 [12-factor]: https://12factor.net 9 [Docker Secrets]: https://docs.docker.com/engine/swarm/secrets/#build-support-for-docker-secrets-into-your-images 10 11 ## `env.Getenv` 12 13 **Alias:** `getenv` 14 15 Exposes the [os.Getenv](https://golang.org/pkg/os/#Getenv) function. 16 17 Retrieves the value of the environment variable named by the key. If the 18 variable is unset, but the same variable ending in `_FILE` is set, the contents 19 of the file will be returned. Otherwise the provided default (or an empty 20 string) is returned. 21 22 This is a more forgiving alternative to using `.Env`, since missing keys will 23 return an empty string, instead of panicking. 24 25 The `_FILE` fallback is especially useful for use with [12-factor][]-style 26 applications configurable only by environment variables, and especially in 27 conjunction with features like [Docker Secrets][]. 28 29 _Added in gomplate [v0.2.0](https://github.com/hairyhenderson/gomplate/releases/tag/v0.2.0)_ 30 ### Usage 31 32 ``` 33 env.Getenv var [default] 34 ``` 35 36 ### Arguments 37 38 | name | description | 39 |------|-------------| 40 | `var` | _(required)_ the environment variable name | 41 | `default` | _(optional)_ the default | 42 43 ### Examples 44 45 ```console 46 $ gomplate -i 'Hello, {{env.Getenv "USER"}}' 47 Hello, hairyhenderson 48 $ gomplate -i 'Hey, {{getenv "FIRSTNAME" "you"}}!' 49 Hey, you! 50 ``` 51 ```console 52 $ echo "safe" > /tmp/mysecret 53 $ export SECRET_FILE=/tmp/mysecret 54 $ gomplate -i 'Your secret is {{getenv "SECRET"}}' 55 Your secret is safe 56 ``` 57 58 ## `env.ExpandEnv` 59 60 Exposes the [os.ExpandEnv](https://golang.org/pkg/os/#ExpandEnv) function. 61 62 Replaces `${var}` or `$var` in the input string according to the values of the 63 current environment variables. References to undefined variables are replaced by the empty string. 64 65 Like [`env.Getenv`](#env-getenv), the `_FILE` variant of a variable is used. 66 67 _Added in gomplate [v2.5.0](https://github.com/hairyhenderson/gomplate/releases/tag/v2.5.0)_ 68 ### Usage 69 70 ``` 71 env.ExpandEnv input 72 ``` 73 74 ### Arguments 75 76 | name | description | 77 |------|-------------| 78 | `input` | _(required)_ the input | 79 80 ### Examples 81 82 ```console 83 $ gomplate -i '{{env.ExpandEnv "Hello $USER"}}' 84 Hello, hairyhenderson 85 $ gomplate -i 'Hey, {{env.ExpandEnv "Hey, ${FIRSTNAME}!"}}' 86 Hey, you! 87 ``` 88 ```console 89 $ echo "safe" > /tmp/mysecret 90 $ export SECRET_FILE=/tmp/mysecret 91 $ gomplate -i '{{env.ExpandEnv "Your secret is $SECRET"}}' 92 Your secret is safe 93 ``` 94 ```console 95 $ gomplate -i '{{env.ExpandEnv (file.Read "foo")}} 96 contents of file "foo"... 97 ```