From 6c24ab7d9e5781960a33c54545a02d224da40e86 Mon Sep 17 00:00:00 2001 From: marensws Date: Mon, 31 Jan 2022 10:26:05 +0100 Subject: [PATCH] compile: adds metadata field to .manifest fixes: #4289 Signed-off-by: marensws --- bundle/bundle.go | 2 +- compile/compile.go | 11 +++++++++++ compile/compile_test.go | 22 ++++++++++++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/bundle/bundle.go b/bundle/bundle.go index 275631a079..c191daa64d 100644 --- a/bundle/bundle.go +++ b/bundle/bundle.go @@ -180,7 +180,7 @@ func (m Manifest) Copy() Manifest { func (m Manifest) String() string { m.Init() - return fmt.Sprintf("", m.Revision, *m.Roots, m.WasmResolvers) + return fmt.Sprintf("", m.Revision, *m.Roots, m.WasmResolvers, m.Metadata) } func (m Manifest) rootSet() stringSet { diff --git a/compile/compile.go b/compile/compile.go index bce2286da9..39713ef54a 100644 --- a/compile/compile.go +++ b/compile/compile.go @@ -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. @@ -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 { @@ -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 } diff --git a/compile/compile_test.go b/compile/compile_test.go index 800516c9f0..af895c119c 100644 --- a/compile/compile_test.go +++ b/compile/compile_test.go @@ -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{