Skip to content

Commit

Permalink
feat: use .manifest to store metrics metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
teodora-sandu committed Dec 15, 2021
1 parent f1a087a commit a925f51
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 25 deletions.
48 changes: 28 additions & 20 deletions internal/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,9 @@ type BuildCommandParams struct {
func RunBuild(args []string, params *BuildCommandParams) error {
buf := bytes.NewBuffer(nil)

var metadataFile = ""
rules, err := util.RetrieveRules(args)
if err == nil {
// choose either one of the locations for the metadata.json
// it will be included in the OPA generated data.json
metadataFile = path.Join(args[0], "metadata.json")
err = os.WriteFile(metadataFile, []byte(fmt.Sprintf("{\"numberOfRules\": %d}", len(rules))),
0644)
if err != nil {
return err
}
metadataFile, err := createManifest(args)
if err == nil && metadataFile != "" {
defer os.Remove(metadataFile)
}

var capabilities *ast.Capabilities
Expand All @@ -57,19 +49,20 @@ func RunBuild(args []string, params *BuildCommandParams) error {
compiler := compile.New().
WithCapabilities(capabilities).
WithTarget(params.Target.String()).
WithAsBundle(false).
WithAsBundle(true).
WithOptimizationLevel(0).
WithOutput(buf).
WithEntrypoints(params.Entrypoint.Strings()...).
WithPaths(args...).
WithFilter(buildCommandLoaderFilter(false, params.Ignore))
WithFilter(buildCommandLoaderFilter(true, params.Ignore))

err = compiler.Build(context.Background())
if err != nil {
return err
}

out, err := os.Create(params.OutputFile)
defer out.Close()
if err != nil {
return err
}
Expand All @@ -79,18 +72,33 @@ func RunBuild(args []string, params *BuildCommandParams) error {
return err
}

if metadataFile != "" {
_ = os.Remove(metadataFile)
}

return out.Close()
return nil
}

func buildCommandLoaderFilter(bundleMode bool, ignore []string) func(string, os.FileInfo, int) bool {
return func(abspath string, info os.FileInfo, depth int) bool {
if !info.IsDir() && strings.HasSuffix(abspath, ".tar.gz") {
return true
if !bundleMode {
if !info.IsDir() && strings.HasSuffix(abspath, ".tar.gz") {
return true
}
}
return util.LoaderFilter{Ignore: ignore}.Apply(abspath, info, depth)
}
}

func createManifest(inputPaths []string) (string, error) {
rules, err := util.RetrieveRules(inputPaths)
if err != nil {
return "", err
}

// choose either one of the locations for the metadata.json
// it will be included in the OPA generated data.json
metadataFile := path.Join(inputPaths[0], ".manifest")
err = os.WriteFile(metadataFile, []byte(fmt.Sprintf("{\"metadata\":{\"numberOfRules\":%d}}", len(rules))),
0644)
if err != nil {
return "", err
}
return metadataFile, nil
}
6 changes: 1 addition & 5 deletions internal/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,16 +310,12 @@ func TestBuildProducesMetadata(t *testing.T) {
}
assert.Nil(t, err)

if f.Name == "/data.json" {
if f.Name == "/.manifest" {
data := new(bytes.Buffer)
_, err := data.ReadFrom(tr)
assert.Nil(t, err)
assert.Contains(t, data.String(), "{\"numberOfRules\":1}")
}

if f.Name == "/metadata.json" {
t.Fatal("unexpected file:", f.Name)
}
}
})
}

0 comments on commit a925f51

Please sign in to comment.