Skip to content

Commit

Permalink
Add warning when json data is passed both as a flag and as piped input
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiught committed Nov 29, 2022
1 parent 304faa1 commit b1b346e
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions internal/cli/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/auth0/auth0-cli/internal/ansi"
"github.com/auth0/auth0-cli/internal/buildinfo"
"github.com/auth0/auth0-cli/internal/display"
"github.com/auth0/auth0-cli/internal/iostream"
"github.com/auth0/auth0-cli/internal/prompt"
)
Expand Down Expand Up @@ -53,6 +54,8 @@ type (
}

apiCmdInputs struct {
renderer *display.Renderer

RawMethod string
RawURI string
RawData string
Expand All @@ -64,7 +67,9 @@ type (
)

func apiCmd(cli *cli) *cobra.Command {
var inputs apiCmdInputs
inputs := apiCmdInputs{
renderer: cli.renderer,
}

cmd := &cobra.Command{
Use: "api <method> <uri>",
Expand Down Expand Up @@ -200,10 +205,17 @@ func (i *apiCmdInputs) validateAndSetData() error {
}

pipedRawData := iostream.PipedInput()
if pipedRawData != nil && data == nil {
if len(pipedRawData) > 0 && data == nil {
data = pipedRawData
}

if len(pipedRawData) > 0 && len(i.RawData) > 0 {
i.renderer.Warnf(
"JSON data was passed using both the flag and as piped input. " +
"The command will use only the data from the flag.",
)
}

if len(data) > 0 && !json.Valid(data) {
return fmt.Errorf("invalid json data given: %s", data)
}
Expand Down

0 comments on commit b1b346e

Please sign in to comment.