mirror of
https://github.com/Luzifer/korvike.git
synced 2024-12-29 23:51:16 +00:00
Add support for b64decode
Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
parent
81828600a8
commit
047d4406ad
3 changed files with 25 additions and 0 deletions
|
@ -15,6 +15,12 @@
|
||||||
$ echo "{{ .foo }}" | korvike -v foo=bar
|
$ echo "{{ .foo }}" | korvike -v foo=bar
|
||||||
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> }}`
|
- `{{ b64encode <string> }}`
|
||||||
Encodes the string with base64 [StdEncoding](https://golang.org/pkg/encoding/base64/#pkg-variables)
|
Encodes the string with base64 [StdEncoding](https://golang.org/pkg/encoding/base64/#pkg-variables)
|
||||||
```console
|
```console
|
||||||
|
|
|
@ -3,6 +3,10 @@ package functions
|
||||||
import "encoding/base64"
|
import "encoding/base64"
|
||||||
|
|
||||||
func init() {
|
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 {
|
registerFunction("b64encode", func(name string, v ...string) string {
|
||||||
return base64.StdEncoding.EncodeToString([]byte(name))
|
return base64.StdEncoding.EncodeToString([]byte(name))
|
||||||
})
|
})
|
||||||
|
|
|
@ -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) {
|
func Test_env(t *testing.T) {
|
||||||
result := randomString()
|
result := randomString()
|
||||||
os.Setenv("KORVIKE_TESTING", result)
|
os.Setenv("KORVIKE_TESTING", result)
|
||||||
|
|
Loading…
Reference in a new issue