Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,12 @@ Commands may be chained, and attributes returned by a command can be reinjected
gli oapi CreateNic --SubnetId subnet-foo | gli oapi LinkNic -v --NicId {{.Nic.NicId}} --VmId i-foo --DeviceNumber 7
```

### Sending raw JSON

```shell
echo '{"SubnetId":"subnet-foo"}' | gli oapi CreateNic
```

### Using jq filters

```shell
Expand Down
2 changes: 1 addition & 1 deletion cmd/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func run(t *testing.T, args []string, input []byte) []byte {
os.Stdout, err = os.Create(filepath.Join(dir, "stdout")) //nolint
require.NoError(t, err)

err = runner.Prefilter()
err = runner.CheckStdin()
require.NoError(t, err)
cmd.Execute()

Expand Down
7 changes: 7 additions & 0 deletions cmd/oapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,11 @@ func TestOAPI(t *testing.T) {
require.NotNil(t, resp.Subnet)
assert.NotEmpty(t, resp.Subnet.SubnetId)
})
t.Run("JSON can be injected", func(t *testing.T) {
in := `{"IpRange":"10.0.0.0/16"}`
resp := osc.CreateNetResponse{}
runJSON(t, []string{"oapi", "CreateNet"}, []byte(in), &resp)
require.NotNil(t, resp.Net)
assert.NotEmpty(t, resp.Net.NetId)
})
}
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

func main() {
err := runner.Prefilter()
err := runner.CheckStdin()
if err != nil {
errors.ExitErr(err)
}
Expand Down
21 changes: 17 additions & 4 deletions pkg/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,24 @@ import (
"github.com/spf13/pflag"
)

var Input []byte
var (
stdinChecked bool
stdin []byte
)

func Stdin() ([]byte, bool) {
return stdin, stdinChecked && len(stdin) > 0
}

func Prefilter() error {
func CheckStdin() error {
stdinChecked = true
if isatty.IsTerminal(os.Stdin.Fd()) {
debug.Println("terminal, skipping stdin")
return nil
}
debug.Println("reading stdin")
var err error
Input, err = io.ReadAll(os.Stdin)
stdin, err = io.ReadAll(os.Stdin)
if err != nil {
return fmt.Errorf("unable to read stdin: %w", err)
}
Expand All @@ -43,7 +51,7 @@ func Prefilter() error {
}
debug.Println("templating args")
var input map[string]any
err = json.Unmarshal(Input, &input)
err = json.Unmarshal(stdin, &input)
if err != nil {
return fmt.Errorf("input is not a JSON object: %w", err)
}
Expand Down Expand Up @@ -73,14 +81,19 @@ func ToStruct(cmd *cobra.Command, arg reflect.Value, prefix string) error {
fs := cmd.Flags()
debug.Println(reflect.Indirect(arg).Type().Name())
var err error
noneset := true
fs.VisitAll(func(f *pflag.Flag) {
if f.Changed { // skipping default values
noneset = false
debug.Println(f.Name, "=>", f.Value)
if serr := set(arg, fs, f.Name, f.Name); serr != nil {
err = serr
}
}
})
if stdin, ok := Stdin(); ok && noneset {
err = json.Unmarshal(stdin, arg.Interface())
}
return err
}

Expand Down