-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsig.go
More file actions
54 lines (43 loc) · 1.2 KB
/
sig.go
File metadata and controls
54 lines (43 loc) · 1.2 KB
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
47
48
49
50
51
52
53
54
package main
import (
"fmt"
runtime "github.com/aserto-dev/runtime"
"github.com/open-policy-agent/opa/v1/ast"
"github.com/open-policy-agent/opa/v1/rego"
"github.com/open-policy-agent/opa/v1/types"
"github.com/pkg/errors"
)
type SigCmd struct {
Verbosity int ` short:"v" type:"counter" help:"Use to increase output verbosity." default:"0"`
}
func (c *SigCmd) Run() error {
ctx := setupLoggerAndContext(c.Verbosity)
r, err := runtime.New(ctx, &runtime.Config{},
runtime.WithBuiltin1(
®o.Function{
Name: "hello",
Memoize: false,
Decl: types.NewFunction(types.Args(types.S), types.S),
},
func(bctx rego.BuiltinContext, name *ast.Term) (*ast.Term, error) {
strName := ""
if err := ast.As(name.Value, &strName); err != nil {
return nil, errors.Wrap(err, "name parameter is not a string")
}
if strName == "there" {
return ast.StringTerm("general kenobi"), nil
}
return &ast.Term{}, nil
},
),
)
if err != nil {
return errors.Wrap(err, "failed to create runtime")
}
def, err := r.BuiltinRequirements()
if err != nil {
return errors.Wrap(err, "failed to calculate builtin requirements")
}
fmt.Println(string(def))
return nil
}