From b1b346e3445c89bb44fb5886e8ac90d4938c83cb Mon Sep 17 00:00:00 2001 From: Sergiu Ghitea Date: Tue, 29 Nov 2022 14:43:55 +0100 Subject: [PATCH] Add warning when json data is passed both as a flag and as piped input --- internal/cli/api.go | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/internal/cli/api.go b/internal/cli/api.go index f928152c4..97b7771f2 100644 --- a/internal/cli/api.go +++ b/internal/cli/api.go @@ -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" ) @@ -53,6 +54,8 @@ type ( } apiCmdInputs struct { + renderer *display.Renderer + RawMethod string RawURI string RawData string @@ -64,7 +67,9 @@ type ( ) func apiCmd(cli *cli) *cobra.Command { - var inputs apiCmdInputs + inputs := apiCmdInputs{ + renderer: cli.renderer, + } cmd := &cobra.Command{ Use: "api ", @@ -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) }