Skip to content

Commit

Permalink
Add base64 encode/decode template helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
richardmarshall committed Jun 20, 2016
1 parent c6622ed commit 02d54d9
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
16 changes: 16 additions & 0 deletions docs/templates.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,22 @@ Wrapper for [net.LookupSRV](https://golang.org/pkg/net/#LookupSRV). The wrapper
{{end}}
```

### base64Encode

Returns a base64 encoded string of the value.

```
key: {{base64Encode "Value"}}
```

### base64Decode

Returns the string representing the decoded base64 value.

```
key: {{base64Decode "VmFsdWU="}}
```

#### Add keys to etcd

```
Expand Down
12 changes: 12 additions & 0 deletions resource/template/template_funcs.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package template

import (
"encoding/base64"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -30,6 +31,8 @@ func newFuncMap() map[string]interface{} {
m["lookupIP"] = LookupIP
m["lookupSRV"] = LookupSRV
m["fileExists"] = isFileExist
m["base64Encode"] = Base64Encode
m["base64Decode"] = Base64Decode
return m
}

Expand Down Expand Up @@ -123,3 +126,12 @@ func LookupSRV(service, proto, name string) []*net.SRV {
sort.Sort(sortSRV(addrs))
return addrs
}

func Base64Encode(data string) string {
return base64.StdEncoding.EncodeToString([]byte(data))
}

func Base64Decode(data string) (string, error) {
s, err := base64.StdEncoding.DecodeString(data)
return string(s), err
}
44 changes: 44 additions & 0 deletions resource/template/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,50 @@ ip: 127.0.0.1
tr.store.Set("/test/data/def", "child")
},
},
templateTest{
desc: "base64Encode test",
toml: `
[template]
src = "test.conf.tmpl"
dest = "./tmp/test.conf"
keys = [
"/test/data/",
]
`,
tmpl: `
{{$data := base64Encode (getv "/test/data") }}
key: {{$data}}
`,
expected: `
key: VmFsdWU=
`,
updateStore: func(tr *TemplateResource) {
tr.store.Set("/test/data", `Value`)
},
},
templateTest{
desc: "base64Decode test",
toml: `
[template]
src = "test.conf.tmpl"
dest = "./tmp/test.conf"
keys = [
"/test/data/",
]
`,
tmpl: `
{{$data := base64Decode (getv "/test/data") }}
key: {{$data}}
`,
expected: `
key: Value
`,
updateStore: func(tr *TemplateResource) {
tr.store.Set("/test/data", `VmFsdWU=`)
},
},
}

// TestTemplates runs all tests in templateTests
Expand Down

0 comments on commit 02d54d9

Please sign in to comment.