Skip to content

Commit

Permalink
cli: try fix issues reported in 'oracle' command
Browse files Browse the repository at this point in the history
Try and fix errors issues reported here:
open-policy-agent/vscode-opa#81

1. Only include .rego files in definition location checks
2. Don't hang if input not provided on stdin

Signed-off-by: Anders Eknert <[email protected]>
  • Loading branch information
anderseknert committed Feb 22, 2023
1 parent 15ecb99 commit 98153fb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
18 changes: 16 additions & 2 deletions cmd/oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,14 @@ func dofindDefinition(params findDefinitionParams, stdin io.Reader, stdout io.Wr
if len(params.bundlePaths.v) > 1 {
return errors.New("not implemented: multiple bundle paths")
}
b, err = loader.NewFileLoader().WithSkipBundleVerification(true).AsBundle(params.bundlePaths.v[0])
b, err = loader.NewFileLoader().
WithSkipBundleVerification(true).
WithFilter(func(abspath string, info os.FileInfo, depth int) bool {
// While directories may contain other things of interest for OPA (json, yaml..),
// only .rego will work reliably for the purpose of finding definitions
return strings.HasPrefix(info.Name(), ".rego")
}).
AsBundle(params.bundlePaths.v[0])
if err != nil {
return err
}
Expand All @@ -123,10 +130,17 @@ func dofindDefinition(params findDefinitionParams, stdin io.Reader, stdout io.Wr
var bs []byte

if params.stdinBuffer {
bs, err = io.ReadAll(stdin)
stat, err := os.Stdin.Stat()
if err != nil {
return err
}
// Only read from stdin when there is something actually there
if (stat.Mode() & os.ModeCharDevice) == 0 {
bs, err = io.ReadAll(stdin)
if err != nil {
return err
}
}
}

result, err := oracle.New().FindDefinition(oracle.DefinitionQuery{
Expand Down
6 changes: 5 additions & 1 deletion cmd/oracle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ p { q }
q = true`)

files := map[string]string{"test.rego": onDiskModule}
files := map[string]string{
"test.rego": onDiskModule,
"document.txt": "this should not be included",
"ignore.json": `{"neither": "should this"}`,
}

test.WithTempFS(files, func(rootDir string) {

Expand Down

0 comments on commit 98153fb

Please sign in to comment.