summaryrefslogtreecommitdiff
path: root/docs-src/content/functions/file.yml
blob: 855480e032ff579fdc3f0c306f8f3952591d78db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
ns: file
preamble: |
  Functions for working with files.
funcs:
  - name: file.Exists
    released: v2.4.0
    description: |
      Reports whether a file or directory exists at the given path.
    pipeline: true
    arguments:
      - name: path
        required: true
        description: The path
    rawExamples:
      - |
        _`input.tmpl`:_
        ```
        {{ if (file.Exists "/tmp/foo") }}yes{{else}}no{{end}}
        ```

        ```console
        $ gomplate -f input.tmpl
        no
        $ touch /tmp/foo
        $ gomplate -f input.tmpl
        yes
        ```
  - name: file.IsDir
    released: v2.4.0
    description: |
      Reports whether a given path is a directory.
    pipeline: true
    arguments:
      - name: path
        required: true
        description: The path
    rawExamples:
      - |
        _`input.tmpl`:_
        ```
        {{ if (file.IsDir "/tmp/foo") }}yes{{else}}no{{end}}
        ```

        ```console
        $ gomplate -f input.tmpl
        no
        $ touch /tmp/foo
        $ gomplate -f input.tmpl
        no
        $ rm /tmp/foo && mkdir /tmp/foo
        $ gomplate -f input.tmpl
        yes
        ```
  - name: file.Read
    released: v2.4.0
    description: |
      Reads a given file _as text_. Note that this will succeed if the given file is binary, but the output may be gibberish.
    pipeline: true
    arguments:
      - name: path
        required: true
        description: The path
    examples:
      - |
        $ echo "hello world" > /tmp/hi
        $ gomplate -i '{{file.Read "/tmp/hi"}}'
        hello world
  - name: file.ReadDir
    released: v2.4.0
    description: |
      Reads a directory and lists the files and directories contained within.
    pipeline: true
    arguments:
      - name: path
        required: true
        description: The path
    examples:
      - |
        $ mkdir /tmp/foo
        $ touch /tmp/foo/a; touch /tmp/foo/b; touch /tmp/foo/c
        $ mkdir /tmp/foo/d
        $ gomplate -i '{{ range (file.ReadDir "/tmp/foo") }}{{.}}{{"\n"}}{{end}}'
        a
        b
        c
        d
  - name: file.Stat
    released: v2.4.0
    description: |
      Returns a [`os.FileInfo`](https://pkg.go.dev/os/#FileInfo) describing the named path.

      Essentially a wrapper for Go's [`os.Stat`](https://pkg.go.dev/os/#Stat) function.
    pipeline: true
    arguments:
      - name: path
        required: true
        description: The path
    examples:
      - |
        $ echo "hello world" > /tmp/foo
        $ gomplate -i '{{ $s := file.Stat "/tmp/foo" }}{{ $s.Mode }} {{ $s.Size }} {{ $s.Name }}'
        -rw-r--r-- 12 foo
  - name: file.Walk
    released: v2.6.0
    description: |
      Like a recursive [`file.ReadDir`](#filereaddir), recursively walks the file tree rooted at `path`, and returns an array of all files and directories contained within.

      The files are walked in lexical order, which makes the output deterministic but means that for very large directories can be inefficient.

      Walk does not follow symbolic links.

      Similar to Go's [`filepath.Walk`](https://pkg.go.dev/path/filepath/#Walk) function.
    pipeline: true
    arguments:
      - name: path
        required: true
        description: The path
    examples:
      - |
        $ tree /tmp/foo
        /tmp/foo
        ├── one
        ├── sub
        │   ├── one
        │   └── two
        ├── three
        └── two

        1 directory, 5 files
        $ gomplate -i '{{ range file.Walk "/tmp/foo" }}{{ if not (file.IsDir .) }}{{.}} is a file{{"\n"}}{{end}}{{end}}'
        /tmp/foo/one is a file
        /tmp/foo/sub/one is a file
        /tmp/foo/sub/two is a file
        /tmp/foo/three is a file
        /tmp/foo/two is a file
  - name: file.Write
    released: v2.4.0
    description: |
      Write the given data to the given file. If the file exists, it will be overwritten.

      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.

      Non-existing directories in the output path will be created.

      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.
    pipeline: true
    arguments:
      - name: filename
        required: true
        description: The name of the file to write to
      - name: data
        required: true
        description: The data to write
    examples:
      - |
        $ gomplate -i '{{ file.Write "/tmp/foo" "hello world" }}'
        $ cat /tmp/foo
        hello world