Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add metadata option to .manifest #4306

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion bundle/bundle.go
Expand Up @@ -180,7 +180,7 @@ func (m Manifest) Copy() Manifest {

func (m Manifest) String() string {
m.Init()
return fmt.Sprintf("<revision: %q, roots: %v, wasm: %+v>", m.Revision, *m.Roots, m.WasmResolvers)
return fmt.Sprintf("<revision: %q, roots: %v, wasm: %+v, metadata: %+v>", m.Revision, *m.Roots, m.WasmResolvers, m.Metadata)
}

func (m Manifest) rootSet() stringSet {
Expand Down
11 changes: 11 additions & 0 deletions compile/compile.go
Expand Up @@ -75,6 +75,7 @@ type Compiler struct {
bvc *bundle.VerificationConfig // represents the key configuration used to verify a signed bundle
bsc *bundle.SigningConfig // represents the key configuration used to generate a signed bundle
keyID string // represents the name of the default key used to verify a signed bundle
metadata *map[string]interface{} // represents additional data included in .manifest file
}

// New returns a new compiler instance that can be invoked.
Expand Down Expand Up @@ -180,6 +181,12 @@ func (c *Compiler) WithCapabilities(capabilities *ast.Capabilities) *Compiler {
return c
}

//WithMetadata sets the additional data to be included in .manifest
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nit]

Suggested change
//WithMetadata sets the additional data to be included in .manifest
// WithMetadata sets the additional data to be included in .manifest

func (c *Compiler) WithMetadata(metadata *map[string]interface{}) *Compiler {
c.metadata = metadata
return c
}

// Build compiles and links the input files and outputs a bundle to the writer.
func (c *Compiler) Build(ctx context.Context) error {

Expand Down Expand Up @@ -223,6 +230,10 @@ func (c *Compiler) Build(ctx context.Context) error {
c.bundle.Manifest.Revision = *c.revision
}

if c.metadata != nil {
c.bundle.Manifest.Metadata = *c.metadata
}

if err := c.bundle.FormatModules(false); err != nil {
return err
}
Expand Down
22 changes: 22 additions & 0 deletions compile/compile_test.go
Expand Up @@ -703,6 +703,28 @@ func TestCompilerSetRevision(t *testing.T) {
})
}

func TestCompilerSetMetadata(t *testing.T) {
files := map[string]string{
"test.rego": `package test

p = true`,
}

test.WithTempFS(files, func(root string) {
metadata := map[string]interface{}{"OPA version": "0.36.1"}
compiler := New().WithPaths(root).WithMetadata(&metadata)

err := compiler.Build(context.Background())
if err != nil {
t.Fatal(err)
}

if compiler.bundle.Manifest.Metadata["OPA version"] != "0.36.1" {
t.Fatal("expected metadata to be set but got:", compiler.bundle.Manifest)
}
})
}

func TestCompilerOutput(t *testing.T) {
// NOTE(tsandall): must use format package here because the compiler formats.
files := map[string]string{
Expand Down