repl

package
v1.19.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 30, 2026 License: Apache-2.0 Imports: 28 Imported by: 1

Documentation

Overview

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. nolint: goconst // String reuse here doesn't make sense to deduplicate.

Index

Examples

Constants

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

Variables

This section is empty.

Functions

This section is empty.

Types

type Error

type Error struct {
	Code    string
	Message string
}

Error is the error type returned by the REPL.

func (*Error) Error

func (err *Error) Error() string

type REPL

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

REPL represents an instance of the interactive shell.

func New

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

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

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

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

func (*REPL) Loop

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

Loop reads, evaluates, and prints query results until the input is exhausted (EOF), the user exits, or an unexpected error occurs.

An interactive terminal gets the full readline editor (history, completion, bracketed paste); any non-terminal input (a pipe, a file, /dev/null) gets a plain line reader that stops cleanly at EOF. The readline editor must not be driven against a non-TTY: on macOS it busy-loops at 100% CPU without ever seeing EOF.

func (*REPL) OneShot

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.

Example

nolint // example code

package main

import (
	"bytes"
	"context"
	"fmt"

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

func main() {
	// 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.
	r := repl.New(store, "", &buf, "json", 0, "").
		WithRegoVersion(ast.RegoV1)

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

	// Query the rule defined above.
	r.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) SetOPAVersionReport

func (r *REPL) SetOPAVersionReport(report [][2]string)

SetOPAVersionReport sets the information about the latest OPA release.

func (*REPL) WithCapabilities

func (r *REPL) WithCapabilities(capabilities *ast.Capabilities) *REPL

func (*REPL) WithConsoleInput added in v1.19.0

func (r *REPL) WithConsoleInput(in io.Reader) *REPL

WithConsoleInput sets the reader the REPL reads query input from. It defaults to os.Stdin; a nil reader is ignored. Non-terminal input (a pipe, a file, /dev/null) uses a plain line reader instead of the terminal editor.

func (*REPL) WithInitBundles

func (r *REPL) WithInitBundles(b map[string]*bundle.Bundle) *REPL

func (*REPL) WithRegoVersion

func (r *REPL) WithRegoVersion(v ast.RegoVersion) *REPL

WithRegoVersion sets the Rego version to v.

func (*REPL) WithRuntime

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

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

func (*REPL) WithStderrWriter

func (r *REPL) WithStderrWriter(w io.Writer) *REPL

func (*REPL) WithV1Compatible deprecated

func (r *REPL) WithV1Compatible(v1Compatible bool) *REPL

WithV1Compatible sets the Rego version to v1.

Deprecated: Use WithRegoVersion instead.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL