Skip to content

Commit

Permalink
fix: Conftest can now successfully load files using a file URL (e.g.,…
Browse files Browse the repository at this point in the history
… file:///C:/path/to/data.yaml) on windows

Removing duplicate code

Signed-off-by: Punith C K <[email protected]>
  • Loading branch information
Punith C K authored and pckvcode committed Feb 11, 2025
1 parent 47385ce commit 624f928
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions policy/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,22 +121,14 @@ func LoadWithData(policyPaths []string, dataPaths []string, capabilities string,
return nil, fmt.Errorf("loading policies: %w", err)
}
}

// FilteredPaths will recursively find all file paths that contain a valid document
// extension from the given list of data paths.
allDocumentPaths, err := loader.FilteredPaths(dataPaths, func(_ string, info os.FileInfo, _ int) bool {
filter := func(_ string, info os.FileInfo, _ int) bool {
if info.IsDir() {
return false
}
return !contains([]string{".yaml", ".yml", ".json"}, filepath.Ext(info.Name()))
})
if err != nil {
return nil, fmt.Errorf("filter data paths: %w", err)
}

documents, err := loader.NewFileLoader().Filtered(dataPaths, func(_ string, info os.FileInfo, _ int) bool {
return !info.IsDir() && !contains([]string{".yaml", ".yml", ".json"}, filepath.Ext(info.Name()))
})
documents, err := loader.NewFileLoader().Filtered(dataPaths, filter)
if err != nil {
return nil, fmt.Errorf("load documents: %w", err)
}
Expand All @@ -145,6 +137,12 @@ func LoadWithData(policyPaths []string, dataPaths []string, capabilities string,
return nil, fmt.Errorf("get documents store: %w", err)
}

// FilteredPaths will recursively find all file paths that contain a valid document
// extension from the given list of data paths.
allDocumentPaths, err := loader.FilteredPaths(dataPaths, filter)
if err != nil {
return nil, fmt.Errorf("filter data paths: %w", err)
}
documentContents := make(map[string]string)
for _, documentPath := range allDocumentPaths {
contents, err := os.ReadFile(documentPath)
Expand Down

0 comments on commit 624f928

Please sign in to comment.