opa: github.com/open-policy-agent/opa/repl Index | Examples | Files

package repl

import "github.com/open-policy-agent/opa/repl"

Package repl implements a Read-Eval-Print-Loop (REPL) for interacting with the policy engine.

The REPL is typically used from the command line, however, it can also be used as a library.

Index

Examples

Package Files

errors.go repl.go

Constants

const (
    // BadArgsErr indicates bad arguments were provided to a built-in REPL
    // command.
    BadArgsErr string = "bad arguments"
)

type Error Uses

type Error struct {
    Code    string
    Message string
}

Error is the error type returned by the REPL.

func (*Error) Error Uses

func (err *Error) Error() string

type REPL Uses

type REPL struct {
    // contains filtered or unexported fields
}

REPL represents an instance of the interactive shell.

func New Uses

func New(store storage.Store, historyPath string, output io.Writer, outputFormat string, errLimit int, banner string) *REPL

New returns a new instance of the REPL.

func (*REPL) DisableMultiLineBuffering Uses

func (r *REPL) DisableMultiLineBuffering(yes bool) *REPL

DisableMultiLineBuffering causes the REPL to not buffer lines when a parse error occurs. Instead, the error will be returned to the caller.

func (*REPL) DisableUndefinedOutput Uses

func (r *REPL) DisableUndefinedOutput(yes bool) *REPL

DisableUndefinedOutput causes the REPL to not print any output when the query is undefined.

func (*REPL) Loop Uses

func (r *REPL) Loop(ctx context.Context)

Loop will run until the user enters "exit", Ctrl+C, Ctrl+D, or an unexpected error occurs.

func (*REPL) OneShot Uses

func (r *REPL) OneShot(ctx context.Context, line string) error

OneShot evaluates the line and prints the result. If an error occurs it is returned for the caller to display.

Code:

// Initialize context for the example. Normally the caller would obtain the
// context from an input parameter or instantiate their own.
ctx := context.Background()

// Instantiate the policy engine's storage layer.
store := inmem.New()

// Create a buffer that will receive REPL output.
var buf bytes.Buffer

// Create a new REPL.
repl := repl.New(store, "", &buf, "json", 0, "")

// Define a rule inside the REPL.
repl.OneShot(ctx, "p { a = [1, 2, 3, 4]; a[_] > 3 }")

// Query the rule defined above.
repl.OneShot(ctx, "p")

// Inspect the output. Defining rules does not produce output so we only expect
// output from the second line of input.
fmt.Println(buf.String())

Output:

{
  "result": [
    {
      "expressions": [
        {
          "value": true,
          "text": "p",
          "location": {
            "row": 1,
            "col": 1
          }
        }
      ]
    }
  ]
}

func (*REPL) WithRuntime Uses

func (r *REPL) WithRuntime(term *ast.Term) *REPL

WithRuntime sets the runtime data to provide to the evaluation engine.

Package repl imports 17 packages (graph) and is imported by 6 packages. Updated 2020-04-09. Refresh now. Tools for package owners.