Skip to content

Commit

Permalink
responds to comments
Browse files Browse the repository at this point in the history
  • Loading branch information
colinjlacy committed Jun 19, 2024
1 parent 5fda392 commit b582237
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 21 deletions.
4 changes: 2 additions & 2 deletions cmd/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ e.g., opa exec --decision /foo/bar/baz ...`,
cmd.Flags().VarP(params.LogLevel, "log-level", "l", "set log level")
cmd.Flags().Var(params.LogFormat, "log-format", "set log format")
cmd.Flags().StringVar(&params.LogTimestampFormat, "log-timestamp-format", "", "set log timestamp format (OPA_LOG_TIMESTAMP_FORMAT environment variable)")
cmd.Flags().BoolVarP(&params.StdIn, "std-in", "", false, "read input from os.Stdin rather than a static file; read timeout is 30 seconds")
cmd.Flags().BoolVarP(&params.StdIn, "stdin-input", "I", false, "read input document from stdin; read timeout is 30 seconds")
cmd.Flags().DurationVar(&params.Timeout, "timeout", 0, "set exec timeout with a Go-style duration, such as '5m 30s'. (default unlimited)")
addV1CompatibleFlag(cmd.Flags(), &params.V1Compatible, false)

Expand Down Expand Up @@ -267,7 +267,7 @@ func injectExplicitBundles(root map[string]interface{}, paths []string) error {

func validateMinimumInput(params *exec.Params) error {
if !params.StdIn && len(params.Paths) == 0 {
return errors.New("requires at least 1 path arg, or the --std-in flag")
return errors.New("requires at least 1 path arg, or the --stdin-input flag")
}
return nil
}
10 changes: 5 additions & 5 deletions cmd/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1073,7 +1073,7 @@ func TestExecWithInvalidInputOptions(t *testing.T) {
expected: "",
},
{
description: "no paths passed in as args should raise error if --std-in flag not set",
description: "no paths passed in as args should raise error if --stdin-input flag not set",
files: map[string]string{
"bundle/x.rego": `package system
Expand All @@ -1087,10 +1087,10 @@ func TestExecWithInvalidInputOptions(t *testing.T) {
}`,
},
expectError: true,
expected: "requires at least 1 path arg, or the --std-in flag",
expected: "requires at least 1 path arg, or the --stdin-input flag",
},
{
description: "should not raise error if --std-in flag is set when no paths passed in as args",
description: "should not raise error if --stdin-input flag is set when no paths passed in as args",
files: map[string]string{
"bundle/x.rego": `package system
Expand All @@ -1109,7 +1109,7 @@ func TestExecWithInvalidInputOptions(t *testing.T) {
expected: "",
},
{
description: "should raise error if --std-in flag is set and no input is given",
description: "should raise error if --stdin-input flag is set and no input is given",
files: map[string]string{
"bundle/x.rego": `package system
Expand All @@ -1125,7 +1125,7 @@ func TestExecWithInvalidInputOptions(t *testing.T) {
stdIn: true,
input: "",
expectError: true,
expected: "exec error: cannot execute on empty input; please enter valid json or yaml when using the --std-in flag",
expected: "exec error: cannot execute on empty input; please enter valid json or yaml when using the --stdin-input flag",
},
}
for _, tt := range tests {
Expand Down
6 changes: 3 additions & 3 deletions cmd/internal/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var (
}
)

const stdInPath = "--std-in"
const stdInPath = "--stdin-input"
const defaultTimeout = 30 * time.Second

// Exec executes OPA against the supplied files and outputs each result.
Expand All @@ -31,7 +31,7 @@ const defaultTimeout = 30 * time.Second
// - specialized output formats (e.g., pretty/non-JSON outputs)
// - exit codes set by convention or policy (e.g,. non-empty set => error)
// - support for new input file formats beyond JSON and YAML
// - flag for setting timeout on --std-in
// - flag for setting timeout on --stdin-input
func Exec(ctx context.Context, opa *sdk.OPA, params *Params) error {
if err := params.validateParams(); err != nil {
return err
Expand Down Expand Up @@ -62,7 +62,7 @@ func execOnStdIn() error {
if err != nil {
return err
} else if input == nil {
return errors.New("cannot execute on empty input; please enter valid json or yaml when using the --std-in flag")
return errors.New("cannot execute on empty input; please enter valid json or yaml when using the --stdin-input flag")
}
r.StoreDecision(&input, stdInPath)
return r.Close()
Expand Down
10 changes: 6 additions & 4 deletions cmd/internal/exec/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func TestExec(t *testing.T) {
},
},
{
description: "should read from std-in if flag is set",
description: "should read from stdin-input if flag is set",
files: map[string]string{
"bundle/x.rego": `package system
Expand All @@ -149,7 +149,7 @@ func TestExec(t *testing.T) {
},
},
{
description: "should raise error if --std-in flag is set and no input is given",
description: "should raise error if --stdin-input flag is set and no input is given",
files: map[string]string{
"bundle/x.rego": `package system
Expand All @@ -165,8 +165,10 @@ func TestExec(t *testing.T) {
stdIn: true,
input: "",
assertion: func(err error) {
expected := "cannot execute on empty input; please enter valid json or yaml when using the --std-in flag"
if err == nil || err.Error() != expected {
expected := "cannot execute on empty input; please enter valid json or yaml when using the --stdin-input flag"
if err == nil {
t.Fatalf("expected error %q, but no error was raised", expected)
} else if err.Error() != expected {
t.Fatalf("expected error %q, got %q", expected, err.Error())
}
},
Expand Down
22 changes: 15 additions & 7 deletions cmd/internal/exec/std_in_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,32 @@ type stdInReader struct {

func (sr *stdInReader) ReadInput() (string, error) {
input := make(chan string, 1)
done := make(chan bool, 1)
var lines []string
in := bufio.NewScanner(sr.Reader)
go func() {
go func(isDone <-chan bool) {
for {
in.Scan()
line := in.Text()
if len(line) == 0 {
input <- strings.Join(lines, "\n")
select {
case <-isDone:
return
default:
in.Scan()
line := in.Text()
if len(line) == 0 {
input <- strings.Join(lines, "\n")
return
}
lines = append(lines, line)
}
lines = append(lines, line)
}
}()
}(done)

for {
select {
case i := <-input:
return i, nil
case <-time.After(sr.Timeout):
done <- true
return "", fmt.Errorf("input timed out")
}
}
Expand Down

0 comments on commit b582237

Please sign in to comment.