mirror of
https://github.com/open-policy-agent/opa.git
synced 2026-08-12 19:32:48 -06:00
compile: adds metadata field to .manifest (#4306)
Fixes: #4289 Signed-off-by: marensws <msws@live.no>
This commit is contained in:
committed by
GitHub
parent
01ca4a6902
commit
a18f53d187
+1
-1
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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{
|
||||
|
||||
Reference in New Issue
Block a user