Skip to content

Commit

Permalink
compile: adds metadata field to .manifest
Browse files Browse the repository at this point in the history
fixes: #4289
Signed-off-by: marensws <msws@live.no>
  • Loading branch information
marensws committed Jan 31, 2022
1 parent 01ca4a6 commit 6c24ab7
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
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
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

0 comments on commit 6c24ab7

Please sign in to comment.