diff --git a/README.md b/README.md index af80d6c..8e63754 100644 --- a/README.md +++ b/README.md @@ -12,3 +12,5 @@ `echo "{{ .foo }}" | korvike -v foo=bar => "bar"` - Read environment variables and replace them inside the template: `export FOO=bar; echo '{{env "FOO"}}' | korvike => "bar"` +- Read a file and place it inside the template: +`echo "Hello World" > hello; echo '{{file "hello"}}' | korvike => "Hello World"` diff --git a/func_file.go b/func_file.go new file mode 100644 index 0000000..699baa4 --- /dev/null +++ b/func_file.go @@ -0,0 +1,21 @@ +package main + +import ( + "io/ioutil" + "os" +) + +func init() { + registerFunction("file", func(name string, v ...string) string { + defaultValue := "" + if len(v) > 0 { + defaultValue = v[0] + } + if _, err := os.Stat(name); err == nil { + if rawValue, err := ioutil.ReadFile(name); err == nil { + return string(rawValue) + } + } + return defaultValue + }) +}