Table of Contents

OpaDotNet Cli Compiler

Cli compiler is simple wrapper over opa CLI tool.

Important

Internally RegoCliCompiler uses opa CLI so you need to ensure it is in your PATH environment variable or you provided full path in RegoCliCompilerOptions.OpaToolPath.

Note

RegoCliCompiler requires write access to the file system to store intermediate artifacts and outputs. By default it will use parent directory if source is file (/data/policy.rego => /data) or bundle directory if source is bundle (/data/policy => /data/policy). You can override artifacts directory with CompilationParameters.OutputPath property.

Add usings


using OpaDotNet.Wasm;
using OpaDotNet.Compilation.Cli;

Compiling single policy file


var compiler = new RegoCliCompiler();

var policy = await compiler.CompileFileAsync(

    // Policy source file.
    "quickstart/example.rego",
    new()
    {
        // Entrypoints (same you would pass for -e parameter for opa build).
        Entrypoints = ["example/hello"],
    },
    cancellationToken
    );

// RegoCliCompiler will always produce bundle.
using var engine = OpaBundleEvaluatorFactory.Create(policy);

Compiling bundle

Create bundle directory


var compiler = new RegoCliCompiler();

var policy = await compiler.CompileBundleAsync(

    // Directory with bundle sources.
    "quickstart",
    new()
    {
        // Entrypoints (same you would pass for -e parameter for opa build).
        Entrypoints = ["example/hello"],
    },
    cancellationToken
    );

// RegoCliCompiler will always produce bundle.
using var engine = OpaBundleEvaluatorFactory.Create(policy);

Compiling policy source


IRegoCompiler compiler = new RegoCliCompiler();

var policySource = """
    package example

    default hello = false

    hello {
        x := input.message
        x == data.world
    }
    """;

var policy = await compiler.CompileSourceAsync(

    // Policy source code.
    policySource,
    new()
    {
        // Entrypoints (same you would pass for -e parameter for opa build).
        Entrypoints = ["example/hello"],
    },
    cancellationToken
    );

// RegoCliCompiler will always produce bundle.
using var engine = OpaBundleEvaluatorFactory.Create(policy);