github.com/wuhuizuo/gomplate@v3.5.0+incompatible/docs/content/usage.md (about) 1 --- 2 title: Usage 3 weight: 11 4 menu: main 5 --- 6 7 The simplest usage of `gomplate` is to just replace environment 8 variables. All environment variables are available by referencing [`.Env`](../syntax/#env) 9 (or [`getenv`](../functions/env/#getenv)) in the template. 10 11 The template is read from standard in, and written to standard out. 12 13 Use it like this: 14 15 ```console 16 $ echo "Hello, {{.Env.USER}}" | gomplate 17 Hello, hairyhenderson 18 ``` 19 20 ## Commandline Arguments 21 22 ### `--file`/`-f`, `--in`/`-i`, and `--out`/`-o` 23 24 By default, `gomplate` will read from `Stdin` and write to `Stdout`. This behaviour can be changed. 25 26 - Use `--file`/`-f` to use a specific input template file. The special value `-` means `Stdin`. 27 - Use `--out`/`-o` to save output to file. The special value `-` means `Stdout`. 28 - Use `--in`/`-i` if you want to set the input template right on the commandline. This overrides `--file`. Because of shell command line lengths, it's probably not a good idea to use a very long value with this argument. 29 30 #### Multiple inputs 31 32 You can specify multiple `--file` and `--out` arguments. The same number of each much be given. This allows `gomplate` to process multiple templates _slightly_ faster than invoking `gomplate` multiple times in a row. 33 34 ### `--input-dir` and `--output-dir` 35 36 For processing multiple templates in a directory you can use `--input-dir` and `--output-dir` together. In this case all files in input directory will be processed as templates and the resulting files stored in `--output-dir`. The output directory will be created if it does not exist and the directory structure of the input directory will be preserved. 37 38 You can use the [`--exclude`](#exclude) argument and/or a [`.gomplateignore`](#ignorefile) file to exclude some of the files in the input directory. 39 40 Example: 41 42 ```bash 43 # Process all files in directory "templates" with the datasource given 44 # and store the files with the same directory structure in "config" 45 gomplate --input-dir=templates --output-dir=config --datasource config=config.yaml 46 ``` 47 48 ### `--output-map` 49 50 Sometimes a 1-to-1 mapping betwen input filenames and output filenames is not desirable. For these cases, you can supply a template string as the argument to `--output-map`. The template string is interpreted as a regular gomplate template, and all datasources and external nested templates are available to the output map template. 51 52 A new [context][] is provided, with the input filename is available at `.in`, and the original context is available at `.ctx`. For convenience, any context keys not conflicting with `in` or `ctx` are also copied. 53 54 All whitespace on the left or right sides of the output is trimmed. 55 56 For example, given an input directory `in/` containing files with the extension `.yaml.tmpl`, if we want to rename those to `.yaml`: 57 58 ```console 59 $ gomplate --input-dir=in/ --output-map='out/{{ .in | strings.ReplaceAll ".yaml.tmpl" ".yaml" }}' 60 ``` 61 62 #### Referencing complex output map template files 63 64 It may be useful to store more complex output map templates in a file. This can be done with [external templates][]. 65 66 Consider a template `out.t`: 67 68 ``` 69 {{- /* .in may contain a directory name - we want to preserve that */ -}} 70 {{ $f := filepath.Base .in -}} 71 out/{{ .in | strings.ReplaceAll $f (index .filemap $f) }}.out 72 ``` 73 74 And a datasource `filemap.json`: 75 76 ```json 77 { "eins.txt": "uno", "deux.txt": "dos" } 78 ``` 79 80 We can blend these two together: 81 82 ```console 83 $ gomplate -t out=out.t -c filemap.json --input-dir=in --output-map='{{ template "out" }}' 84 ``` 85 86 ### `--chmod` 87 88 By default, output files are created with the same file mode (permissions) as input files. If desired, the `--chmod` option can be used to override this behaviour, and set the output file mode explicitly. This can be useful for creating executable scripts or ensuring write permissions. 89 90 The value must be an octal integer in the standard UNIX `chmod` format, i.e. `644` to indicate that owner gets read+write, group gets read-only, and others get read-only permissions. See the [`chmod(1)` man page](https://linux.die.net/man/1/chmod) for more details. 91 92 **Note:** `--chmod` is not currently supported on Windows. Behaviour is undefined, but will likely not change file permissions at all. 93 94 ### `--exclude` 95 96 When using the [`--input-dir`](#input-dir-and-output-dir) argument, it can be useful to exclude certain files from being processed. You can use `--exclude` to achieve this. It takes a [`.gitignore`][]-style pattern, and any files matching the pattern will be excluded. You can also repeat the argument to provide a series of patterns to be excluded. 97 98 Patterns provided with `--exclude` are matched relative to the input directory. 99 100 _Note:_ These patterns are _not_ treated as filesystem globs, and so a pattern like `/foo/bar.json` will match relative to the input directory, not the root of the filesystem as they may appear! 101 102 Example: 103 104 ```console 105 $ gomplate --exclude example/** --exclude *.png --input-dir in/ --output-dir out/ 106 ``` 107 108 This will stop all files in the `in/example` directory from being processed, as well as all `.png` files in the `in/` directory. 109 110 #### `.gomplateignore` files 111 112 You can also use a file named `.gomplateignore` containing one exclude pattern on each line. This has the same syntax as a [`.gitignore`][] file. 113 When processing sub-directories, `.gomplateignore` files in the parent directory are also considered. Patterns are matched relative to the location of the `.gomplateignore` file. 114 115 ### `--datasource`/`-d` 116 117 Add a data source in `name=URL` form. Specify multiple times to add multiple sources. The data can then be used by the [`datasource`](../functions/data/#datasource) and [`include`](../functions/data/#include) functions. 118 119 Data sources referenced in this way are lazy-loaded: they will not be read until the template is parsed and a `datasource` or `include` function is encountered. 120 121 See [Datasources](../datasources) for full details. 122 123 A few different forms are valid: 124 - `mydata=file:///tmp/my/file.json` 125 - Create a data source named `mydata` which is read from `/tmp/my/file.json`. This form is valid for any file in any path. 126 - `mydata=file.json` 127 - Create a data source named `mydata` which is read from `file.json` (in the current working directory). This form is only valid for files in the current directory. 128 - `mydata.json` 129 - This form infers the name from the file name (without extension). Only valid for files in the current directory. 130 131 ### `--context`/`c` 132 133 Add a data source in `name=URL` form, and make it available in the [default context][] as `.<name>`. The special name `.` (period) can be used to override the entire default context. 134 135 Data sources referenced with `--context` will be immediately loaded before gomplate processes the template. This is in contrast to the `--datasource` behaviour, which lazy-loads data while processing the template. 136 137 All other rules for the [`--datasource`/`-d`](#datasource-d) flag apply. 138 139 Examples: 140 141 ```console 142 $ gomplate --context post=https://jsonplaceholder.typicode.com/posts/2 -i 'post title is: {{ .post.title }}' 143 post title is: qui est esse 144 ``` 145 146 ```console 147 $ gomplate -c .=http://xkcd.com/info.0.json -i '<a href="{{ .img }}">{{ .title }}</a>' 148 <a href="https://imgs.xkcd.com/comics/diploma_legal_notes.png">Diploma Legal Notes</a> 149 ``` 150 151 ### Overriding the template delimiters 152 153 Sometimes it's necessary to override the default template delimiters (`{{`/`}}`). 154 Use `--left-delim`/`--right-delim` or set `$GOMPLATE_LEFT_DELIM`/`$GOMPLATE_RIGHT_DELIM`. 155 156 ### `--template`/`-t` 157 158 Add a nested template that can be referenced by the main input template(s) with the [`template`](https://golang.org/pkg/text/template/#hdr-Actions) built-in or the functions in the [`tmpl`](../functions/tmpl/) namespace. Specify multiple times to add multiple template references. 159 160 A few different forms are valid: 161 162 - `--template mytemplate.t` 163 - References a file `mytemplate.t` in the current working directory. 164 - It will be available as a template named `mytemplate.t`: 165 ```console 166 $ gomplate --template helloworld.tmpl -i 'here are the contents of the template: [ {{ template "helloworld.tmpl" }} ]' 167 here are the contents of the template: [ hello, world! ] 168 ``` 169 - `--template path/to/mytemplate.t` 170 - References a file `mytemplate.t` in the path `path/to/`. 171 - It will be available as a template named `path/to/mytemplate.t`: 172 ```console 173 $ gomplate --template foo/bar/helloworld.tmpl -i 'here are the contents of the template: [ {{ template "foo/bar/helloworld.tmpl" }} ]' 174 here are the contents of the template: [ hello, world! ] 175 ``` 176 - `--template path/to/` 177 - Makes available all files in the path `path/to/`. 178 - Any files within this path can be referenced: 179 ```console 180 $ gomplate --template foo/bar/ -i 'here are the contents of the template: [ {{ template "foo/bar/helloworld.tmpl" }} ]' 181 here are the contents of the template: [ hello, world! ] 182 ``` 183 - `--template alias=path/to/mytemplate.t` 184 - References a file `mytemplate.t` in the path `path/to/` 185 - It will be available as a template named `alias`: 186 ```console 187 $ gomplate --template t=foo/bar/helloworld.tmpl -i 'here are the contents of the template: [ {{ template "t" }} ]' 188 here are the contents of the template: [ hello, world! ] 189 ``` 190 - `--template alias=path/to/` 191 - Makes available all files in the path `path/to/`. 192 - Any files within this path can be referenced, with the path replaced with `alias`: 193 ```console 194 $ gomplate --template dir=foo/bar/ -i 'here are the contents of the template: [ {{ template "dir/helloworld.tmpl" }} ]' 195 here are the contents of the template: [ hello, world! ] 196 ``` 197 198 ## Post-template command execution 199 200 Gomplate can launch other commands when template execution is successful. Simply 201 add the command to the command-line after a `--` argument: 202 203 ```console 204 $ gomplate -i 'hello world' -o out.txt -- cat out.txt 205 hello world 206 ``` 207 208 ## Suppressing empty output 209 210 Sometimes it can be desirable to suppress empty output (i.e. output consisting of only whitespace). To do so, set `GOMPLATE_SUPPRESS_EMPTY=true` in your environment: 211 212 ```console 213 $ export GOMPLATE_SUPPRESS_EMPTY=true 214 $ gomplate -i '{{ print " \n" }}' -o out 215 $ cat out 216 cat: out: No such file or directory 217 ``` 218 219 [default context]: ../syntax/#the-context 220 [context]: ../syntax/#the-context 221 [external templates]: ../syntax/#external-templates 222 [`.gitignore`]: https://git-scm.com/docs/gitignore