From 5d1b2f7af5dad484a65f971860fdc4be26f9afa0 Mon Sep 17 00:00:00 2001 From: jolestar Date: Tue, 11 Jul 2017 13:33:09 +0800 Subject: [PATCH 1/5] add base64 function. --- docs/templates.md | 8 +++++ resource/template/template_funcs.go | 1 + resource/template/template_funcs_ext.go | 18 ++++++++++ resource/template/template_funcs_ext_test.go | 38 ++++++++++++++++++-- resource/template/template_test.go | 24 +++++++++++++ 5 files changed, 86 insertions(+), 3 deletions(-) diff --git a/docs/templates.md b/docs/templates.md index 800b048a0..016577cf7 100644 --- a/docs/templates.md +++ b/docs/templates.md @@ -361,6 +361,14 @@ hosts:{{$hosts := getvs "/test/data/*/id"}} {{toYaml $hosts}} ``` +### base64 + +base64 encoding + +``` +{{getv "/test/data/password" |base64 }} +``` + ## Example Usage ```Bash diff --git a/resource/template/template_funcs.go b/resource/template/template_funcs.go index c516390fe..42dc3a94f 100644 --- a/resource/template/template_funcs.go +++ b/resource/template/template_funcs.go @@ -47,6 +47,7 @@ func newFuncMap() map[string]interface{} { m["filter"] = Filter m["toJson"] = ToJson m["toYaml"] = ToYaml + m["base64"] = Base64 return m } diff --git a/resource/template/template_funcs_ext.go b/resource/template/template_funcs_ext.go index b88bccf0d..27e2c4e09 100644 --- a/resource/template/template_funcs_ext.go +++ b/resource/template/template_funcs_ext.go @@ -1,6 +1,7 @@ package template import ( + "encoding/base64" "encoding/json" "errors" "fmt" @@ -459,3 +460,20 @@ func ToYaml(v interface{}) (string, error) { } return string(b), nil } + +func Base64(v interface{}) (string, error) { + var input []byte + s, ok := v.(string) + if ok { + input = []byte(s) + } else { + b, ok := v.([]byte) + if ok { + input = b + } + } + if input != nil { + return base64.StdEncoding.EncodeToString(input), nil + } + return "", fmt.Errorf("unsupported type %s", reflect.ValueOf(v).Kind().String()) +} diff --git a/resource/template/template_funcs_ext_test.go b/resource/template/template_funcs_ext_test.go index 0f4873092..4c06223d8 100644 --- a/resource/template/template_funcs_ext_test.go +++ b/resource/template/template_funcs_ext_test.go @@ -665,8 +665,11 @@ func TestToJsonAndYaml(t *testing.T) { {[]string{"a1", "b1"}, false, `["a1","b1"]`, "- a1\n- b1\n"}, {"a1", false, `"a1"`, "a1\n"}, {1, false, "1", "1\n"}, - {struct {Name string}{Name:"test"}, false, `{"Name":"test"}`, "name: test\n"}, - {struct {Name string; Addr []string}{Name:"test", Addr: []string{"a1", "a2"}}, false, `{"Name":"test","Addr":["a1","a2"]}`, + {struct{ Name string }{Name: "test"}, false, `{"Name":"test"}`, "name: test\n"}, + {struct { + Name string + Addr []string + }{Name: "test", Addr: []string{"a1", "a2"}}, false, `{"Name":"test","Addr":["a1","a2"]}`, `name: test addr: - a1 @@ -703,4 +706,33 @@ addr: } } } -} \ No newline at end of file +} + +func TestBase64(t *testing.T) { + for i, this := range []struct { + input interface{} + err bool + expect string + }{ + {"", false, ""}, + {"aaa", false, "YWFh"}, + {[]byte("aaa"), false, "YWFh"}, + {"user:123Abc", false, "dXNlcjoxMjNBYmM="}, + {struct{}{}, true, ""}, + } { + result, err := Base64(this.input) + if this.err { + if err == nil { + t.Errorf("[%d] Base64 didn't return an expected error", i) + } + } else { + if err != nil { + t.Errorf("[%d] failed: %s", i, err) + continue + } + if !reflect.DeepEqual(result, this.expect) { + t.Errorf("[%d] Base64 [%v] got %v but expected %v", i, this.input, result, this.expect) + } + } + } +} diff --git a/resource/template/template_test.go b/resource/template/template_test.go index e3bec3827..14dcfaeba 100644 --- a/resource/template/template_test.go +++ b/resource/template/template_test.go @@ -522,6 +522,30 @@ ids: tr.store.Set("/test/data/ids", "b1 b2") }, }, + templateTest{ + desc: "base64 test", + toml: ` +[template] +src = "test.conf.tmpl" +dest = "./tmp/test.conf" +keys = [ + "/test/data/username", + "/test/data/password", +] +`, + tmpl: ` +{{$username := getv "/test/data/username"}}{{$password := getv "/test/data/password"}}{{$pair := printf "%s:%s" $username $password}} +base64:{{base64 $pair}} +`, + expected: ` + +base64:dXNlcjoxMjNBYmM= +`, + updateStore: func(tr *TemplateResource) { + tr.store.Set("/test/data/username", "user") + tr.store.Set("/test/data/password", "123Abc") + }, + }, } // TestTemplates runs all tests in templateTests From e61b3be547b8b473bf9d416338f5161a321fed8b Mon Sep 17 00:00:00 2001 From: jolestar Date: Tue, 11 Jul 2017 13:34:57 +0800 Subject: [PATCH 2/5] gofmt --- backends/redis/client.go | 10 +++++----- resource/template/util_unix.go | 4 ++-- resource/template/util_windows.go | 2 +- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/backends/redis/client.go b/backends/redis/client.go index 6a0e56e14..92635c908 100644 --- a/backends/redis/client.go +++ b/backends/redis/client.go @@ -3,10 +3,10 @@ package redis import ( "fmt" "github.com/garyburd/redigo/redis" + "github.com/kelseyhightower/confd/log" "os" "strings" "time" - "github.com/kelseyhightower/confd/log" ) // Client is a wrapper around the redis client @@ -28,7 +28,7 @@ func tryConnect(machines []string, password string) (redis.Conn, error) { network = "unix" } log.Debug(fmt.Sprintf("Trying to connect to redis node %s", address)) - + dialops := []redis.DialOption{ redis.DialConnectTimeout(time.Second), redis.DialReadTimeout(time.Second), @@ -58,8 +58,8 @@ func (c *Client) connectedClient() (redis.Conn, error) { resp, err := c.client.Do("PING") if (err != nil && err == redis.ErrNil) || resp != "PONG" { - log.Error(fmt.Sprintf("Existing redis connection no longer usable. " + - "Will try to re-establish. Error: %s", err.Error())) + log.Error(fmt.Sprintf("Existing redis connection no longer usable. "+ + "Will try to re-establish. Error: %s", err.Error())) c.client = nil } } @@ -80,7 +80,7 @@ func (c *Client) connectedClient() (redis.Conn, error) { // It returns an error if a connection to the cluster cannot be made. func NewRedisClient(machines []string, password string) (*Client, error) { var err error - clientWrapper := &Client{ machines : machines, password: password, client: nil } + clientWrapper := &Client{machines: machines, password: password, client: nil} clientWrapper.client, err = tryConnect(machines, password) return clientWrapper, err } diff --git a/resource/template/util_unix.go b/resource/template/util_unix.go index 4aea1c6f9..b3471ccd6 100644 --- a/resource/template/util_unix.go +++ b/resource/template/util_unix.go @@ -8,8 +8,8 @@ import ( "fmt" "io" "os" - "syscall" "os/exec" + "syscall" ) // fileStat return a fileInfo describing the named file. @@ -35,4 +35,4 @@ func fileStat(name string) (fi fileInfo, err error) { func command(cmd string) *exec.Cmd { return exec.Command("/bin/sh", "-c", cmd) -} \ No newline at end of file +} diff --git a/resource/template/util_windows.go b/resource/template/util_windows.go index b36203e89..127385c23 100644 --- a/resource/template/util_windows.go +++ b/resource/template/util_windows.go @@ -33,4 +33,4 @@ func fileStat(name string) (fi fileInfo, err error) { func command(cmd string) *exec.Cmd { return exec.Command("cmd", "/C", cmd) -} \ No newline at end of file +} From 1a09d3c1b3b9d6ea02a794d48991f0aab6bce6db Mon Sep 17 00:00:00 2001 From: jolestar Date: Fri, 11 Aug 2017 17:46:08 +0800 Subject: [PATCH 3/5] Add base64Decode function. --- docs/templates.md | 12 +++++- resource/template/template_funcs.go | 3 +- resource/template/template_funcs_ext.go | 14 ++++++- resource/template/template_funcs_ext_test.go | 39 ++++++++++++++++++-- resource/template/template_test.go | 24 +++++++++++- 5 files changed, 83 insertions(+), 9 deletions(-) diff --git a/docs/templates.md b/docs/templates.md index 016577cf7..d3812c5b6 100644 --- a/docs/templates.md +++ b/docs/templates.md @@ -361,12 +361,20 @@ hosts:{{$hosts := getvs "/test/data/*/id"}} {{toYaml $hosts}} ``` -### base64 +### base64Encode base64 encoding ``` -{{getv "/test/data/password" |base64 }} +{{getv "/test/data/password" |base64Encode }} +``` + +### base64Decode + +base64 decoding + +``` +{{getv "/test/data/data" |base64Decode }} ``` ## Example Usage diff --git a/resource/template/template_funcs.go b/resource/template/template_funcs.go index 42dc3a94f..0ed75d28b 100644 --- a/resource/template/template_funcs.go +++ b/resource/template/template_funcs.go @@ -47,7 +47,8 @@ func newFuncMap() map[string]interface{} { m["filter"] = Filter m["toJson"] = ToJson m["toYaml"] = ToYaml - m["base64"] = Base64 + m["base64Encode"] = Base64Encode + m["base64Decode"] = Base64Decode return m } diff --git a/resource/template/template_funcs_ext.go b/resource/template/template_funcs_ext.go index 27e2c4e09..3a23df1b8 100644 --- a/resource/template/template_funcs_ext.go +++ b/resource/template/template_funcs_ext.go @@ -461,7 +461,7 @@ func ToYaml(v interface{}) (string, error) { return string(b), nil } -func Base64(v interface{}) (string, error) { +func Base64Encode(v interface{}) (string, error) { var input []byte s, ok := v.(string) if ok { @@ -477,3 +477,15 @@ func Base64(v interface{}) (string, error) { } return "", fmt.Errorf("unsupported type %s", reflect.ValueOf(v).Kind().String()) } + +func Base64Decode(v interface{}) (string, error) { + var input string + s, ok := v.(string) + if ok { + input = s + } else { + return "", fmt.Errorf("unsupported type %s", reflect.ValueOf(v).Kind().String()) + } + r, err := base64.StdEncoding.DecodeString(input) + return string(r), err +} diff --git a/resource/template/template_funcs_ext_test.go b/resource/template/template_funcs_ext_test.go index 4c06223d8..89731a0e1 100644 --- a/resource/template/template_funcs_ext_test.go +++ b/resource/template/template_funcs_ext_test.go @@ -708,7 +708,7 @@ addr: } } -func TestBase64(t *testing.T) { +func TestBase64Encode(t *testing.T) { for i, this := range []struct { input interface{} err bool @@ -718,12 +718,13 @@ func TestBase64(t *testing.T) { {"aaa", false, "YWFh"}, {[]byte("aaa"), false, "YWFh"}, {"user:123Abc", false, "dXNlcjoxMjNBYmM="}, + {"user:\n123Abc", false, "dXNlcjoKMTIzQWJj"}, {struct{}{}, true, ""}, } { - result, err := Base64(this.input) + result, err := Base64Encode(this.input) if this.err { if err == nil { - t.Errorf("[%d] Base64 didn't return an expected error", i) + t.Errorf("[%d] Base64Encode didn't return an expected error", i) } } else { if err != nil { @@ -731,7 +732,37 @@ func TestBase64(t *testing.T) { continue } if !reflect.DeepEqual(result, this.expect) { - t.Errorf("[%d] Base64 [%v] got %v but expected %v", i, this.input, result, this.expect) + t.Errorf("[%d] Base64Encode [%v] got %v but expected %v", i, this.input, result, this.expect) + } + } + } +} + + +func TestBase64Decode(t *testing.T) { + for i, this := range []struct { + input interface{} + err bool + expect string + }{ + {"", false, ""}, + {"YWFh", false, "aaa"}, + {"dXNlcjoxMjNBYmM=", false, "user:123Abc"}, + {"dXNlcjoKMTIzQWJj", false, "user:\n123Abc"}, + {struct{}{}, true, ""}, + } { + result, err := Base64Decode(this.input) + if this.err { + if err == nil { + t.Errorf("[%d] Base64Decode didn't return an expected error", i) + } + } else { + if err != nil { + t.Errorf("[%d] failed: %s", i, err) + continue + } + if !reflect.DeepEqual(result, this.expect) { + t.Errorf("[%d] Base64Decode [%v] got %v but expected %v", i, this.input, result, this.expect) } } } diff --git a/resource/template/template_test.go b/resource/template/template_test.go index 14dcfaeba..307e3c6d3 100644 --- a/resource/template/template_test.go +++ b/resource/template/template_test.go @@ -535,7 +535,7 @@ keys = [ `, tmpl: ` {{$username := getv "/test/data/username"}}{{$password := getv "/test/data/password"}}{{$pair := printf "%s:%s" $username $password}} -base64:{{base64 $pair}} +base64:{{base64Encode $pair}} `, expected: ` @@ -546,6 +546,28 @@ base64:dXNlcjoxMjNBYmM= tr.store.Set("/test/data/password", "123Abc") }, }, + templateTest{ + desc: "base64 test", + toml: ` +[template] +src = "test.conf.tmpl" +dest = "./tmp/test.conf" +keys = [ + "/test/data/encoded", +] +`, + tmpl: ` +{{$data := getv "/test/data/encoded"}} +value:{{base64Decode $data}} +`, + expected: ` + +value:user:123Abc +`, + updateStore: func(tr *TemplateResource) { + tr.store.Set("/test/data/encoded", "dXNlcjoxMjNBYmM=") + }, + }, } // TestTemplates runs all tests in templateTests From 7a4e39420c550c22f89ad34b9136e080294e7bf7 Mon Sep 17 00:00:00 2001 From: jolestar Date: Fri, 11 Aug 2017 17:46:37 +0800 Subject: [PATCH 4/5] go fmt. --- resource/template/template_funcs_ext_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/resource/template/template_funcs_ext_test.go b/resource/template/template_funcs_ext_test.go index 89731a0e1..0736458bc 100644 --- a/resource/template/template_funcs_ext_test.go +++ b/resource/template/template_funcs_ext_test.go @@ -738,7 +738,6 @@ func TestBase64Encode(t *testing.T) { } } - func TestBase64Decode(t *testing.T) { for i, this := range []struct { input interface{} From d1a58c932a6ac10ac9695c7c1fbee0c3e92714b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A8=8A=E5=B0=9A=E4=BA=AB?= Date: Thu, 12 Oct 2017 19:47:43 +0800 Subject: [PATCH 5/5] trigger travis build --- resource/template/template_funcs_ext_test.go | 29 +++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/resource/template/template_funcs_ext_test.go b/resource/template/template_funcs_ext_test.go index 0736458bc..595c0f57f 100644 --- a/resource/template/template_funcs_ext_test.go +++ b/resource/template/template_funcs_ext_test.go @@ -1,12 +1,13 @@ package template import ( - "github.com/kelseyhightower/memkv" "path" "reflect" "runtime" "testing" "time" + + "github.com/kelseyhightower/memkv" ) type tstCompareType int @@ -60,6 +61,32 @@ val: 2 tr.store.Set("/test/key", "1") }, }, + templateTest{ + desc: "add float test", + toml: ` +[template] +src = "test.conf.tmpl" +dest = "./tmp/test.conf" +keys = [ + "/test/key", +] +`, + tmpl: ` +{{with get "/test/key"}} +key: {{base .Key}} +val: {{add .Value 1}} +{{end}} +`, + expected: ` + +key: key +val: 2 + +`, + updateStore: func(tr *TemplateResource) { + tr.store.Set("/test/key", "1.0") + }, + }, templateTest{ desc: "sub test", toml: `