github.com/hairyhenderson/gomplate/v4@v4.0.0-pre-2.0.20240520121557-362f058f0c93/docs/content/config.md (about) 1 --- 2 title: Configuration 3 weight: 12 4 menu: main 5 --- 6 7 In addition to [command-line arguments][], gomplate supports the use of 8 configuration files to control its behaviour. 9 10 Using a file for configuration can be useful especially when rendering templates 11 that use multiple datasources, plugins, nested templates, etc... In situations 12 where teams share templates, it can be helpful to commit config files into the 13 team's source control system. 14 15 By default, gomplate will look for a file `.gomplate.yaml` in the current working 16 diretory, but this path can be altered with the [`--config`](../usage/#config) 17 command-line argument, or the `GOMPLATE_CONFIG` environment variable. 18 19 ### Configuration precedence 20 21 [Command-line arguments][] will always take precedence over settings in a config 22 file. In the cases where configuration can be altered with an environment 23 variable, the config file will take precedence over environment variables. 24 25 So, if the `leftDelim` setting is configured in 3 ways: 26 27 ```console 28 $ export GOMPLATE_LEFT_DELIM=:: 29 $ echo "leftDelim: ((" > .gomplate.yaml 30 $ gomplate --left-delim "<<" 31 ``` 32 33 The delimiter will be `<<`. 34 35 ## File format 36 37 Currently, gomplate supports config files written in [YAML][] syntax, though other 38 structured formats may be supported in future (please [file an issue][] if this 39 is important to you!) 40 41 Roughly all of the [command-line arguments][] are able to be set in a config 42 file, with the exception of `--help`, `--verbose`, and `--version`. Some 43 environment variable based settings not configurable on the command-line are 44 also supported in config files. 45 46 Most of the configuration names are similar, though instead of using `kebab-case`, 47 multi-word names are rendered as `camelCase`. 48 49 Here is an example of a simple config file: 50 51 ```yaml 52 inputDir: in/ 53 outputDir: out/ 54 55 datasources: 56 local: 57 url: file:///tmp/data.json 58 remote: 59 url: https://example.com/api/v1/data 60 header: 61 Authorization: ["Basic aGF4MHI6c3dvcmRmaXNoCg=="] 62 63 plugins: 64 dostuff: /usr/local/bin/stuff.sh 65 ``` 66 67 ## `chmod` 68 69 See [`--chmod`](../usage/#chmod). 70 71 Sets the output file mode. 72 73 ## `context` 74 75 See [`--context`](../usage/#context-c). 76 77 Add data sources to the default context. This is a nested structure that 78 includes the URL for the data source and the optional HTTP header to send. 79 80 For example: 81 82 ```yaml 83 context: 84 data: 85 url: https://example.com/api/v1/data 86 header: 87 Authorization: ["Basic aGF4MHI6c3dvcmRmaXNoCg=="] 88 stuff: 89 url: stuff.yaml 90 ``` 91 92 This adds two datasources to the context: `data` and `stuff`, and when the `data` 93 source is retrieved, an `Authorization` header will be sent with the given value. 94 95 Note that the `.` name can also be used to set the entire context: 96 97 ```yaml 98 context: 99 .: 100 url: data.toml 101 ``` 102 103 ## `datasources` 104 105 See [`--datasource`](../usage/#datasource-d). 106 107 Define data sources. This is a nested structure that includes the URL for the data 108 source and the optional HTTP header to send. 109 110 For example: 111 112 ```yaml 113 datasources: 114 data: 115 url: https://example.com/api/v1/data 116 header: 117 Authorization: ["Basic aGF4MHI6c3dvcmRmaXNoCg=="] 118 stuff: 119 url: stuff.yaml 120 ``` 121 122 This defines two datasources: `data` and `stuff`, and when the `data` 123 source is used, an `Authorization` header will be sent with the given value. 124 125 ## `excludes` 126 127 See [`--exclude` and `--include`](../usage/#exclude-and-include). 128 129 This is an array of exclude patterns, used in conjunction with [`inputDir`](#inputdir). 130 Note that there is no `includes`, instead you can specify negative 131 exclusions by prefixing the patterns with `!`. 132 133 ```yaml 134 excludes: 135 - '*.txt' 136 - '!include-this.txt' 137 ``` 138 139 This will skip all files with the extension `.txt`, except for files named 140 `include-this.txt`, which will be processed. 141 142 ## `excludeProcessing` 143 144 See [`--exclude-processing`](../usage/#exclude-processing). 145 146 This is an array of exclude patterns, used in conjunction with [`inputDir`](#inputdir). 147 The matching files will be copied to the output directory without template rendering. 148 149 ```yaml 150 excludeProcessing: 151 - '*.jpg' 152 ``` 153 154 This will copy all files with the extension `.jpg` to the output directory. 155 156 ## `execPipe` 157 158 See [`--exec-pipe`](../usage/#exec-pipe). 159 160 Use the rendered output as the [`postExec`](#postexec) command's standard input. 161 162 Must be used in conjuction with [`postExec`](#postexec), and will override 163 any [`outputFiles`](#outputfiles) settings. 164 165 ## `experimental` 166 167 See [`--experimental`](../usage/#experimental). Can also be set with the `GOMPLATE_EXPERIMENTAL=true` environment variable. 168 169 Some functions and features are provided for early feedback behind the `experimental` configuration option. These features may change before being permanently enabled, and [feedback](https://github.com/hairyhenderson/gomplate/issues/new) is requested from early adopters! 170 171 Experimental functions are marked in the documentation with an _"(experimental)"_ annotation. 172 173 ```yaml 174 experimental: true 175 ``` 176 177 ## `in` 178 179 See [`--in`/`-i`](../usage/#file-f-in-i-and-out-o). 180 181 Provide the input template inline. Note that unlike the `--in`/`-i` commandline 182 argument, there are no shell-imposed length limits. 183 184 A simple example: 185 ```yaml 186 in: hello to {{ .Env.USER }} 187 ``` 188 189 A multi-line example (see https://yaml-multiline.info/ for more about multi-line 190 string syntax in YAML): 191 192 ```yaml 193 in: | 194 A longer multi-line 195 document: 196 {{- range .foo }} 197 {{ .bar }} 198 {{ end }} 199 ``` 200 201 May not be used with `inputDir` or `inputFiles`. 202 203 ## `inputDir` 204 205 See [`--input-dir`](../usage/#input-dir-and-output-dir). 206 207 The directory containing input template files. All files contained within will 208 be processed recursively. Must be used with [`outputDir`](#outputdir) or 209 [`outputMap`](#outputmap). Can also be used with [`excludes`](#excludes). 210 211 ```yaml 212 inputDir: templates/ 213 outputDir: out/ 214 ``` 215 216 May not be used with `in` or `inputFiles`. 217 218 ## `inputFiles` 219 220 See [`--file`/`-f`](../usage/#file-f-in-i-and-out-o). 221 222 An array of input template paths. The special value `-` means `Stdin`. Multiple 223 values can be set, but there must be a corresponding number of `outputFiles` 224 entries present. 225 226 ```yaml 227 inputFiles: 228 - first.tmpl 229 - second.tmpl 230 outputFiles: 231 - first.out 232 - second.out 233 ``` 234 235 Flow style can be more compact: 236 237 ```yaml 238 inputFiles: ['-'] 239 outputFiles: ['-'] 240 ``` 241 242 May not be used with `in` or `inputDir`. 243 244 ## `leftDelim` 245 246 See [`--left-delim`](../usage/#overriding-the-template-delimiters). 247 248 Overrides the left template delimiter. 249 250 ```yaml 251 leftDelim: '%{' 252 ``` 253 254 ## `missingKey` 255 256 See [`--missing-key`](../usage/#--missing-key). 257 258 Control the behavior during execution if a map is indexed with a key that is not present in the map 259 260 ```yaml 261 missingKey: error 262 ``` 263 264 ## `outputDir` 265 266 See [`--output-dir`](../usage/#input-dir-and-output-dir). 267 268 The directory to write rendered output files. Must be used with 269 [`inputDir`](#inputdir). 270 271 If the directory is missing, it will be created with the same permissions as the 272 `inputDir`. 273 274 ```yaml 275 inputDir: templates/ 276 outputDir: out/ 277 ``` 278 279 May not be used with `outputFiles`. 280 281 ## `outputFiles` 282 283 See [`--out`/`-o`](../usage/#file-f-in-i-and-out-o). 284 285 An array of output file paths. The special value `-` means `Stdout`. Multiple 286 values can be set, but there must be a corresponding number of `inputFiles` 287 entries present. 288 289 If any of the parent directories are missing, they will be created with the same 290 permissions as the input directories. 291 292 ```yaml 293 inputFiles: 294 - first.tmpl 295 - second.tmpl 296 outputFiles: 297 - first.out 298 - second.out 299 ``` 300 301 Can also be used with [`in`](#in): 302 303 ```yaml 304 in: >- 305 hello, 306 world! 307 outputFiles: [ hello.txt ] 308 ``` 309 310 May not be used with `inputDir`. 311 312 ## `outputMap` 313 314 See [`--output-map`](../usage/#output-map). 315 316 Must be used with [`inputDir`](#inputdir). 317 318 ```yaml 319 inputDir: in/ 320 outputMap: | 321 out/{{ .in | strings.ReplaceAll ".yaml.tmpl" ".yaml" }} 322 ``` 323 324 ## `plugins` 325 326 See [`--plugin`](../usage/#plugin). 327 328 A map that configures custom functions for use in the templates. The key is the 329 name of the function, and the value configures the plugin. The value is a map 330 containing the command (`cmd`) and the options `pipe` (boolean) and `timeout` 331 (duration). A list of optional arguments to always pass to the plugin can be set 332 with `args` (array of strings). 333 334 Alternatively, the value can be a string, which sets only `cmd`. 335 336 ```yaml 337 in: '{{ "world" | figlet | lolcat }}' 338 plugins: 339 figlet: 340 cmd: /usr/local/bin/figlet 341 args: 342 - oh 343 - hello 344 pipe: true 345 timeout: 1s 346 lolcat: /home/hairyhenderson/go/bin/lolcat 347 ``` 348 349 ### `cmd` 350 351 The path to the plugin executable (or script) to run. 352 353 ### `args` 354 355 An array of optional arguments to always pass to the plugin. These arguments 356 will be passed _before_ any arguments provided in the template. 357 358 For example: 359 360 ```yaml 361 plugins: 362 echo: 363 cmd: /bin/echo 364 args: 365 - foo 366 - bar 367 ``` 368 369 With this template: 370 ``` 371 {{ echo "baz" }} 372 ``` 373 374 Will result the command being called like this: 375 376 ```console 377 $ /bin/echo foo bar baz 378 ``` 379 380 ### `pipe` 381 382 Whether to pipe the final argument of the template function to the plugin's 383 Stdin, or provide as a separate argument. 384 385 For example, given a `myfunc` plugin with a `cmd` of `/bin/myfunc`: 386 387 With this template: 388 ``` 389 {{ print "bar" | myfunc "foo" }} 390 ``` 391 392 If `pipe` is `true`, the plugin executable will receive the input `"bar"` as its 393 Stdin, like this shell command: 394 395 ```console 396 $ echo -n "bar" | /bin/myfunc "foo" 397 ``` 398 399 If `pipe` is `false` (the default), the plugin executable will receive the 400 input `"bar"` as its last argument, like this shell command: 401 402 ```console 403 $ /bin/myfunc "foo" "bar" 404 ``` 405 406 _Note:_ in a chained pipeline (e.g. `{{ foo | bar }}`), the result of each 407 command is passed as the final argument of the next, and so the template above 408 could be written as `{{ myfunc "foo" "bar" }}`. 409 410 ### `timeout` 411 412 The plugin's timeout. After this time, the command will be terminated and the 413 template function will return an error. The value must be a valid 414 [duration][] such as `1s`, `1m`, `1h`, 415 416 The default is `5s`. 417 418 ## `pluginTimeout` 419 420 See [`--plugin`](../usage/#plugin). 421 422 Sets the timeout for all configured plugins. Overrides the default of `5s`. 423 After this time, plugin commands will be killed. The value must be a valid 424 [duration][] such as `10s` or `3m`. 425 426 ```yaml 427 plugins: 428 figlet: /usr/local/bin/figlet 429 pluginTimeout: 500ms 430 ``` 431 432 ## `postExec` 433 434 See [post-template command execution](../usage/#post-template-command-execution). 435 436 Configures a command to run after the template is rendered. 437 438 See also [`execPipe`](#execpipe) for piping output directly into the `postExec` command. 439 440 ## `rightDelim` 441 442 See [`--right-delim`](../usage/#overriding-the-template-delimiters). 443 444 Overrides the right template delimiter. 445 446 ```yaml 447 rightDelim: '))' 448 ``` 449 450 ## `templates` 451 452 See [`--template`/`-t`](../usage/#template-t). 453 454 ```yaml 455 templates: 456 t: 457 url: file:///foo/bar/helloworld.tmpl 458 remote: 459 url: https://example.com/api/v1/someremotetemplate 460 header: 461 Authorization: ["Basic aGF4MHI6c3dvcmRmaXNoCg=="] 462 ``` 463 464 [command-line arguments]: ../usage 465 [file an issue]: https://github.com/hairyhenderson/gomplate/issues/new 466 [YAML]: http://yaml.org 467 [duration]: (../functions/time/#time-parseduration)