-
Notifications
You must be signed in to change notification settings - Fork 15
/
base64.go
46 lines (41 loc) · 1.12 KB
/
base64.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package lib
import (
"encoding/base64"
"github.com/zclconf/go-cty/cty"
"github.com/zclconf/go-cty/cty/function"
)
var (
Base64DecodeFunc = newBase64DecodeFunction()
Base64EncodeFunc = newBase64EncodeFunction()
)
func newBase64DecodeFunction() function.Function {
return function.New(&function.Spec{
Params: []function.Parameter{{
Name: "base64_decode",
Type: cty.String,
}},
Type: function.StaticReturnType(cty.String),
Impl: func(args []cty.Value, _ cty.Type) (ret cty.Value, err error) {
first := args[0]
result, err := base64.StdEncoding.DecodeString(first.AsString())
if err != nil {
return cty.StringVal(""), err
}
return cty.StringVal(string(result)), nil
},
})
}
func newBase64EncodeFunction() function.Function {
return function.New(&function.Spec{
Params: []function.Parameter{{
Name: "base64_encode",
Type: cty.String,
}},
Type: function.StaticReturnType(cty.String),
Impl: func(args []cty.Value, _ cty.Type) (ret cty.Value, err error) {
first := args[0]
result := base64.StdEncoding.EncodeToString([]byte(first.AsString()))
return cty.StringVal(result), nil
},
})
}