github.com/wuhuizuo/gomplate@v3.5.0+incompatible/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 ### Usage 30 31 ```go 32 env.Getenv var [default] 33 ``` 34 35 ### Arguments 36 37 | name | description | 38 |------|-------------| 39 | `var` | _(required)_ the environment variable name | 40 | `default` | _(optional)_ the default | 41 42 ### Examples 43 44 ```console 45 $ gomplate -i 'Hello, {{env.Getenv "USER"}}' 46 Hello, hairyhenderson 47 $ gomplate -i 'Hey, {{getenv "FIRSTNAME" "you"}}!' 48 Hey, you! 49 ``` 50 ```console 51 $ echo "safe" > /tmp/mysecret 52 $ export SECRET_FILE=/tmp/mysecret 53 $ gomplate -i 'Your secret is {{getenv "SECRET"}}' 54 Your secret is safe 55 ``` 56 57 ## `env.ExpandEnv` 58 59 Exposes the [os.ExpandEnv](https://golang.org/pkg/os/#ExpandEnv) function. 60 61 Replaces `${var}` or `$var` in the input string according to the values of the 62 current environment variables. References to undefined variables are replaced by the empty string. 63 64 Like [`env.Getenv`](#env-getenv), the `_FILE` variant of a variable is used. 65 66 ### Usage 67 68 ```go 69 env.ExpandEnv input 70 ``` 71 72 ### Arguments 73 74 | name | description | 75 |------|-------------| 76 | `input` | _(required)_ the input | 77 78 ### Examples 79 80 ```console 81 $ gomplate -i '{{env.ExpandEnv "Hello $USER"}}' 82 Hello, hairyhenderson 83 $ gomplate -i 'Hey, {{env.ExpandEnv "Hey, ${FIRSTNAME}!"}}' 84 Hey, you! 85 ``` 86 ```console 87 $ echo "safe" > /tmp/mysecret 88 $ export SECRET_FILE=/tmp/mysecret 89 $ gomplate -i '{{env.ExpandEnv "Your secret is $SECRET"}}' 90 Your secret is safe 91 ``` 92 ```console 93 $ gomplate -i '{{env.ExpandEnv (file.Read "foo")}} 94 contents of file "foo"... 95 ```