-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
jsonnet.go
42 lines (39 loc) · 885 Bytes
/
jsonnet.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
package lambroll
import (
"fmt"
"os"
"github.com/google/go-jsonnet"
"github.com/google/go-jsonnet/ast"
)
func DefaultJsonnetNativeFuncs() []*jsonnet.NativeFunction {
return []*jsonnet.NativeFunction{
{
Name: "env",
Params: []ast.Identifier{"name", "default"},
Func: func(args []any) (any, error) {
key, ok := args[0].(string)
if !ok {
return nil, fmt.Errorf("env: name must be a string")
}
if v := os.Getenv(key); v != "" {
return v, nil
}
return args[1], nil
},
},
{
Name: "must_env",
Params: []ast.Identifier{"name"},
Func: func(args []any) (any, error) {
key, ok := args[0].(string)
if !ok {
return nil, fmt.Errorf("must_env: name must be a string")
}
if v, ok := os.LookupEnv(key); ok {
return v, nil
}
return nil, fmt.Errorf("must_env: %s is not set", key)
},
},
}
}