file functions

file.Exists

Reports whether a file or directory exists at the given path.

Usage

file.Exists path

Example

input.tmpl:

{{ if (file.Exists "/tmp/foo") }}yes{{else}}no{{end}}
$ gomplate -f input.tmpl
no
$ touch /tmp/foo
$ gomplate -f input.tmpl
yes

file.IsDir

Reports whether a given path is a directory.

Usage

file.IsDir path

Example

input.tmpl:

{{ if (file.IsDir "/tmp/foo") }}yes{{else}}no{{end}}
$ gomplate -f input.tmpl
no
$ touch /tmp/foo
$ gomplate -f input.tmpl
no
$ rm /tmp/foo && mkdir /tmp/foo
$ gomplate -f input.tmpl
yes

file.Read

Reads a given file as text. Note that this will succeed if the given file is binary, but

Usage

file.Read path

Examples

$ echo "hello world" > /tmp/hi
$ gomplate -i '{{file.Read "/tmp/hi"}}'
hello world

file.ReadDir

Reads a directory and lists the files and directories contained within.

Usage

file.ReadDir 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

file.Stat

Returns a os.FileInfo describing the named path. Essentially a wrapper for Go’s os.Stat function.

Usage

file.Stat 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

file.Walk

Like a recursive file.ReadDir, 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 function.

Usage

file.Walk 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