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

Types

type REPL

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

REPL represents an instance of the interactive shell.

func New

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

New returns a new instance of the REPL.

func (*REPL) Loop

func (r *REPL) Loop()

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

func (*REPL) OneShot

func (r *REPL) OneShot(line string) bool

OneShot evaluates a single line and prints the result. Returns true if caller should exit.

Example

Code:play 

package main

import (
	"bytes"
	"fmt"

	"github.com/open-policy-agent/opa/repl"
	"github.com/open-policy-agent/opa/storage"
)

func main() {

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

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

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

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

	// Query the rule defined above.
	repl.OneShot("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:

true

Source Files

repl.go

Version
v0.2.0
Published
Nov 7, 2016
Platform
js/wasm
Imports
12 packages
Last checked
32 minutes ago

Tools for package owners.