Skip to content

Commit

Permalink
Fix an issue where push would only load json data (#342)
Browse files Browse the repository at this point in the history
Signed-off-by: Lennard Eijsackers <[email protected]>
  • Loading branch information
Blokje5 authored Jul 5, 2020
1 parent c5346c7 commit 8415206
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 22 deletions.
30 changes: 8 additions & 22 deletions internal/commands/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
auth "github.com/deislabs/oras/pkg/auth/docker"
"github.com/deislabs/oras/pkg/content"
"github.com/deislabs/oras/pkg/oras"
"github.com/open-policy-agent/conftest/policy"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -134,32 +135,17 @@ func buildLayers(memoryStore *content.Memorystore, path string) ([]ocispec.Descr
return nil, fmt.Errorf("get abs path: %w", err)
}

var policy []string
var data []string
err = filepath.Walk(root, func(currentPath string, info os.FileInfo, err error) error {
if err != nil {
return fmt.Errorf("walk path: %w", err)
}

if info.IsDir() {
return nil
}

if filepath.Ext(currentPath) == ".rego" {
policy = append(policy, currentPath)
}

if filepath.Ext(currentPath) == ".json" {
data = append(data, currentPath)
}
policies, err := policy.ReadFilesWithTests(root)
if err != nil {
return nil, fmt.Errorf("load policy paths: %w", err)
}

return nil
})
data, err := policy.ReadDataFiles(root)
if err != nil {
return nil, err
return nil, fmt.Errorf("load data paths: %w", err)
}

policyLayers, err := buildLayer(policy, root, memoryStore, openPolicyAgentPolicyLayerMediaType)
policyLayers, err := buildLayer(policies, root, memoryStore, openPolicyAgentPolicyLayerMediaType)
if err != nil {
return nil, fmt.Errorf("build policy layer: %w", err)
}
Expand Down
31 changes: 31 additions & 0 deletions policy/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,37 @@ func StoreFromDataFiles(paths []string) (storage.Store, error) {
return store, nil
}

// ReadDataFiles returns the paths to all data documents
// at the given path. Any JSON or YAML document
// could be a valid document.
func ReadDataFiles(path string) ([]string, error) {
var data []string
err := filepath.Walk(path, func(currentPath string, info os.FileInfo, err error) error {
if err != nil {
return fmt.Errorf("walk path: %w", err)
}

if info.IsDir() {
if info.Name() == ".git" {
return filepath.SkipDir
}

return nil
}

if filepath.Ext(currentPath) == ".json" || filepath.Ext(currentPath) == ".yaml" || filepath.Ext(currentPath) == ".yml" {
data = append(data, currentPath)
}

return nil
})
if err != nil {
return nil, fmt.Errorf("find data files: %w", err)
}

return data, nil
}

func filterDataFiles(abspath string, info os.FileInfo, depth int) bool {
pattern := "*.rego"
match, _ := filepath.Match(pattern, info.Name())
Expand Down

0 comments on commit 8415206

Please sign in to comment.