Skip to content

Instantly share code, notes, and snippets.

@gertd
Created Sep 29, 2021
Embed
What would you like to do?
aserto-runtime-blog-example-2
package main
import (
"context"
"fmt"
"os"
"time"
"github.com/aserto-dev/runtime"
"github.com/open-policy-agent/opa/ast"
"github.com/open-policy-agent/opa/rego"
"github.com/open-policy-agent/opa/types"
"github.com/pkg/errors"
"github.com/rs/zerolog"
)
func main() {
logger := zerolog.New(os.Stdout).
With().Timestamp().Logger().Level(zerolog.ErrorLevel)
ctx := context.Background()
// Create a runtime
r, cleanup, _ := runtime.NewRuntime(
ctx,
&logger,
&runtime.Config{
LocalBundles: runtime.LocalBundlesConfig{
Paths: []string{"./my-bundle.tar.gz"},
},
},
runtime.WithBuiltin1(&rego.Function{
Name: "hello",
Memoize: false,
Decl: types.NewFunction(types.Args(types.S), types.S),
}, func(ctx rego.BuiltinContext, name *ast.Term) (*ast.Term, error) {
strName := ""
err := ast.As(name.Value, &strName)
if err != nil {
return nil,
errors.Wrap(err, "name parameter is not a string")
}
if strName == "there" {
return ast.StringTerm("general kenobi"), nil
}
return nil, nil
}),
)
defer cleanup()
r.PluginsManager.Start(ctx)
r.WaitForPlugins(ctx, time.Second*5)
result, _ := r.Query(ctx, `x = hello("there")`,
nil, true, false, false, "",
)
fmt.Printf("%+v\n", result.Result)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment