1
0
Fork 0
mirror of https://github.com/Luzifer/korvike.git synced 2024-11-08 15:30:05 +00:00

Add support for b64decode

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2021-08-30 15:55:52 +02:00
parent 81828600a8
commit 047d4406ad
Signed by: luzifer
GPG key ID: 0066F03ED215AD7D
3 changed files with 25 additions and 0 deletions

View file

@ -15,6 +15,12 @@
$ echo "{{ .foo }}" | korvike -v foo=bar
bar
```
- `{{ b64decode <string> }}`
Decodes the string with base64 [StdEncoding](https://golang.org/pkg/encoding/base64/#pkg-variables)
```console
$ echo '{{ b64decode "SGVsbG8gV29ybGQ=" }}' | korvike
Hello World
```
- `{{ b64encode <string> }}`
Encodes the string with base64 [StdEncoding](https://golang.org/pkg/encoding/base64/#pkg-variables)
```console

View file

@ -3,6 +3,10 @@ package functions
import "encoding/base64"
func init() {
registerFunction("b64decode", func(name string, v ...string) (string, error) {
b, err := base64.StdEncoding.DecodeString(name)
return string(b), err
})
registerFunction("b64encode", func(name string, v ...string) string {
return base64.StdEncoding.EncodeToString([]byte(name))
})

View file

@ -47,6 +47,21 @@ func Test_GetFunctionMap(t *testing.T) {
}
}
func Test_base64(t *testing.T) {
var (
b64 = "aGVsbG8="
plain = "hello"
)
if r := renderHelper(fmt.Sprintf(`{{b64decode "%s"}}`, b64), nil); r != plain {
t.Errorf("[b64decode] did not yield expected string: %q (expected %q)", r, plain)
}
if r := renderHelper(fmt.Sprintf(`{{b64encode "%s"}}`, plain), nil); r != b64 {
t.Errorf("[b64encode] did not yield expected string: %q (expected %q)", r, b64)
}
}
func Test_env(t *testing.T) {
result := randomString()
os.Setenv("KORVIKE_TESTING", result)