github.com/wynshop-open-source/gomplate@v3.5.0+incompatible/docs-src/content/functions/file.yml (about) 1 ns: file 2 preamble: | 3 Functions for working with files. 4 funcs: 5 - name: file.Exists 6 description: | 7 Reports whether a file or directory exists at the given path. 8 pipeline: true 9 arguments: 10 - name: path 11 required: true 12 description: The path 13 rawExamples: 14 - | 15 _`input.tmpl`:_ 16 ``` 17 {{ if (file.Exists "/tmp/foo") }}yes{{else}}no{{end}} 18 ``` 19 20 ```console 21 $ gomplate -f input.tmpl 22 no 23 $ touch /tmp/foo 24 $ gomplate -f input.tmpl 25 yes 26 ``` 27 - name: file.IsDir 28 description: | 29 Reports whether a given path is a directory. 30 pipeline: true 31 arguments: 32 - name: path 33 required: true 34 description: The path 35 rawExamples: 36 - | 37 _`input.tmpl`:_ 38 ``` 39 {{ if (file.IsDir "/tmp/foo") }}yes{{else}}no{{end}} 40 ``` 41 42 ```console 43 $ gomplate -f input.tmpl 44 no 45 $ touch /tmp/foo 46 $ gomplate -f input.tmpl 47 no 48 $ rm /tmp/foo && mkdir /tmp/foo 49 $ gomplate -f input.tmpl 50 yes 51 ``` 52 - name: file.Read 53 description: | 54 Reads a given file _as text_. Note that this will succeed if the given file is binary, but the output may be gibberish. 55 pipeline: true 56 arguments: 57 - name: path 58 required: true 59 description: The path 60 examples: 61 - | 62 $ echo "hello world" > /tmp/hi 63 $ gomplate -i '{{file.Read "/tmp/hi"}}' 64 hello world 65 - name: file.ReadDir 66 description: | 67 Reads a directory and lists the files and directories contained within. 68 pipeline: true 69 arguments: 70 - name: path 71 required: true 72 description: The path 73 examples: 74 - | 75 $ mkdir /tmp/foo 76 $ touch /tmp/foo/a; touch /tmp/foo/b; touch /tmp/foo/c 77 $ mkdir /tmp/foo/d 78 $ gomplate -i '{{ range (file.ReadDir "/tmp/foo") }}{{.}}{{"\n"}}{{end}}' 79 a 80 b 81 c 82 d 83 - name: file.Stat 84 description: | 85 Returns a [`os.FileInfo`](https://golang.org/pkg/os/#FileInfo) describing the named path. 86 87 Essentially a wrapper for Go's [`os.Stat`](https://golang.org/pkg/os/#Stat) function. 88 pipeline: true 89 arguments: 90 - name: path 91 required: true 92 description: The path 93 examples: 94 - | 95 $ echo "hello world" > /tmp/foo 96 $ gomplate -i '{{ $s := file.Stat "/tmp/foo" }}{{ $s.Mode }} {{ $s.Size }} {{ $s.Name }}' 97 -rw-r--r-- 12 foo 98 - name: file.Walk 99 description: | 100 Like a recursive [`file.ReadDir`](#file-readdir), recursively walks the file tree rooted at `path`, and returns an array of all files and directories contained within. 101 102 The files are walked in lexical order, which makes the output deterministic but means that for very large directories can be inefficient. 103 104 Walk does not follow symbolic links. 105 106 Similar to Go's [`filepath.Walk`](https://golang.org/pkg/path/filepath/#Walk) function. 107 pipeline: true 108 arguments: 109 - name: path 110 required: true 111 description: The path 112 examples: 113 - | 114 $ tree /tmp/foo 115 /tmp/foo 116 ├── one 117 ├── sub 118 │ ├── one 119 │ └── two 120 ├── three 121 └── two 122 123 1 directory, 5 files 124 $ gomplate -i '{{ range file.Walk "/tmp/foo" }}{{ if not (file.IsDir .) }}{{.}} is a file{{"\n"}}{{end}}{{end}}' 125 /tmp/foo/one is a file 126 /tmp/foo/sub/one is a file 127 /tmp/foo/sub/two is a file 128 /tmp/foo/three is a file 129 /tmp/foo/two is a file 130 - name: file.Write 131 description: | 132 Write the given data to the given file. If the file exists, it will be overwritten. 133 134 For increased security, `file.Write` will only write to files which are contained within the current working directory. Attempts to write elsewhere will fail with an error. 135 136 Non-existing directories in the output path will be created. 137 138 If the data is a byte array (`[]byte`), it will be written as-is. Otherwise, it will be converted to a string before being written. 139 pipeline: true 140 arguments: 141 - name: filename 142 required: true 143 description: The name of the file to write to 144 - name: data 145 required: true 146 description: The data to write 147 examples: 148 - | 149 $ gomplate -i '{{ file.Write "/tmp/foo" "hello world" }}' 150 $ cat /tmp/foo 151 hello world