Skip to content

Add support for file function in template mode #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: go
go:
- 1.8
- 1.9
- tip
install:
- go get
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,14 @@ Reuslt file `daemon.conf`:
<VirtualHost>
```

### BuiltIn Functions

There is support for additional builtin functions that can be used.

List with example:

* file - `{{ file "example.txt" }}`

## Installation

```bash
Expand Down
100 changes: 100 additions & 0 deletions builtin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// The implementation is based of
// https://github.com/gliderlabs/sigil/blob/master/builtin/builtin.go
package main

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"text/template"
"io"
)

type NamedReader struct {
io.Reader
Name string
}

// Merge template.FuncMaps into a single map
// First entry win's
func Merge(ms ...template.FuncMap) template.FuncMap {
res := make(template.FuncMap)
for _, m := range ms {
srcMap:
for k, v := range m {
// Check if (k,v) was added before:
_, ok := res[k]
if ok {
continue srcMap
}
res[k] = v
}
}
return res
}

// Extract data from the interface
func String(in interface{}) (string, string, bool) {
switch obj := in.(type) {
case string:
return obj, "", true
case NamedReader:
data, err := ioutil.ReadAll(obj)
if err != nil {
panic(err)
}
return string(data), obj.Name, true
case fmt.Stringer:
return obj.String(), "", true
default:
return "", "", false
}
}

func LookPath(file string) (string, error) {
if strings.HasPrefix(file, "/") {
return file, nil
}
cwd, _ := os.Getwd()
search := []string{cwd}
for _, path := range search {
filepath := filepath.Join(path, file)
if _, err := os.Stat(filepath); err == nil {
return filepath, nil
}
}
return "", fmt.Errorf("Not found in path: %s", file)
}

func builtInFunctions() template.FuncMap {
return template.FuncMap{
"file": File,
}
}

func read(file interface{}) ([]byte, error) {
reader, ok := file.(NamedReader)
if ok {
return ioutil.ReadAll(reader)
}
path, _, ok := String(file)
if !ok {
return []byte{}, fmt.Errorf("file must be stream or path string")
}
fp, err := LookPath(path)
if err != nil {
return []byte{}, err
}
data, err := ioutil.ReadFile(fp)
if err != nil {
return []byte{}, err
}
return data, nil
}

func File(filename interface{}) (interface{}, error) {
str, err := read(filename)
return string(str), err
}
2 changes: 1 addition & 1 deletion template.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type templateData struct {

func createTemplate() *template.Template {
tmpl := template.New("base")
tmpl.Funcs(sprig.TxtFuncMap())
tmpl.Funcs(Merge(sprig.TxtFuncMap(), builtInFunctions()))
tmpl.Option("missingkey=zero")

return tmpl
Expand Down
12 changes: 12 additions & 0 deletions tests/template.test
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,15 @@ Testing template with functions:
$ cat test.txt
bar
FOO

Testing template with reading from file:

$ cat > test.txt <<EOF
> {{file "foo.txt"}}
> {{file .Arg.foo}}
> EOF
$ printf "FOO" > foo.txt
$ go-replace --mode=template -s foo -r foo.txt test.txt
$ cat test.txt
FOO
FOO