No error/help display for JSON output (#6106)

* No error/help display for JSON output

* Fix for subcommands

* Add integration tests

* Replace NoArgs with NoArgsAndSilenceJSON

* Add more tests, in a single It
This commit is contained in:
Philippe Martin
2022-09-09 15:03:19 +02:00
committed by GitHub
parent c5f8c5aed8
commit 6c6f8ef5ad
11 changed files with 57 additions and 8 deletions

View File

@@ -204,7 +204,7 @@ func NewCmdBinding(name, fullName string) *cobra.Command {
Use: name,
Short: "Add Binding",
Long: "Add a binding between a service and the component in the devfile",
Args: cobra.NoArgs,
Args: genericclioptions.NoArgsAndSilenceJSON,
Example: fmt.Sprintf(addBindingExample, fullName),
Run: func(cmd *cobra.Command, args []string) {
genericclioptions.GenericRun(o, cmd, args)

View File

@@ -260,6 +260,11 @@ func ShowSubcommands(cmd *cobra.Command, args []string) error {
strs = append(strs, subcmd.Name())
}
}
if log.IsJSON() {
cmd.SilenceUsage = true
cmd.SilenceErrors = true
}
//revive:disable:error-strings This is a top-level error message displayed as is to the end user
return fmt.Errorf("Subcommand not found, use one of the available commands: %s", strings.Join(strs, ", "))
//revive:enable:error-strings
@@ -282,6 +287,11 @@ func ShowHelp(cmd *cobra.Command, args []string) error {
}
//revive:disable:error-strings This is a top-level error message displayed as is to the end user
if log.IsJSON() {
cmd.SilenceUsage = true
cmd.SilenceErrors = true
return errors.New("Invalid command - see available commands/subcommands by running `odo`")
}
return errors.New("Invalid command - see available commands/subcommands above")
//revive:enable:error-strings
}

View File

@@ -216,7 +216,7 @@ func NewCmdComponent(name, fullName string) *cobra.Command {
Use: name,
Short: "Delete component",
Long: "Delete component",
Args: cobra.NoArgs,
Args: genericclioptions.NoArgsAndSilenceJSON,
Example: fmt.Sprintf(deleteExample, fullName),
Run: func(cmd *cobra.Command, args []string) {
genericclioptions.GenericRun(o, cmd, args)

View File

@@ -119,7 +119,7 @@ func NewCmdBinding(name, fullName string) *cobra.Command {
Use: name,
Short: "Describe bindings",
Long: "Describe bindings",
Args: cobra.NoArgs,
Args: genericclioptions.NoArgsAndSilenceJSON,
Example: fmt.Sprintf(describeBindingExample, fullName),
Run: func(cmd *cobra.Command, args []string) {
genericclioptions.GenericRun(o, cmd, args)

View File

@@ -237,7 +237,7 @@ func NewCmdComponent(name, fullName string) *cobra.Command {
Use: name,
Short: "Describe a component",
Long: "Describe a component",
Args: cobra.NoArgs,
Args: genericclioptions.NoArgsAndSilenceJSON,
Example: fmt.Sprintf(describeExample, fullName),
Run: func(cmd *cobra.Command, args []string) {
genericclioptions.GenericRun(o, cmd, args)

View File

@@ -136,7 +136,7 @@ func NewCmdComponentList(name, fullName string) *cobra.Command {
Short: "List all components in the current namespace",
Long: "List all components in the current namespace.",
Example: fmt.Sprintf(listExample, fullName),
Args: cobra.NoArgs,
Args: genericclioptions.NoArgsAndSilenceJSON,
Annotations: map[string]string{"command": "management"},
Run: func(cmd *cobra.Command, args []string) {
genericclioptions.GenericRun(o, cmd, args)

View File

@@ -153,7 +153,7 @@ func NewCmdList(name, fullName string) *cobra.Command {
Short: "List all components in the current namespace",
Long: "List all components in the current namespace.",
Example: fmt.Sprintf(listExample, fullName),
Args: cobra.NoArgs,
Args: genericclioptions.NoArgsAndSilenceJSON,
Annotations: map[string]string{"command": "management"},
Run: func(cmd *cobra.Command, args []string) {
genericclioptions.GenericRun(o, cmd, args)

View File

@@ -60,7 +60,7 @@ func NewCmdLogout(name, fullName string) *cobra.Command {
Short: "Logout of the cluster",
Long: "Logout of the cluster.",
Example: fmt.Sprintf(example, fullName),
Args: cobra.NoArgs,
Args: genericclioptions.NoArgsAndSilenceJSON,
Run: func(cmd *cobra.Command, args []string) {
genericclioptions.GenericRun(o, cmd, args)
},

View File

@@ -86,7 +86,7 @@ func NewCmdBinding(name, fullName string) *cobra.Command {
Use: name,
Short: "Remove Binding",
Long: "Remove a binding between a service and the component from the devfile",
Args: cobra.NoArgs,
Args: genericclioptions.NoArgsAndSilenceJSON,
Example: fmt.Sprintf(removeBindingExample, fullName),
Run: func(cmd *cobra.Command, args []string) {
genericclioptions.GenericRun(o, cmd, args)

View File

@@ -233,3 +233,12 @@ func CheckMachineReadableOutputCommand(cmd *cobra.Command) error {
}
return nil
}
// NoArgsAndSilenceJSON returns the NoArgs value, and silence output when JSON output is activated
func NoArgsAndSilenceJSON(cmd *cobra.Command, args []string) error {
if log.IsJSON() {
cmd.SilenceUsage = true
cmd.SilenceErrors = true
}
return cobra.NoArgs(cmd, args)
}

View File

@@ -52,6 +52,36 @@ var _ = Describe("odo generic", func() {
Expect(output).To(ContainSubstring("Invalid command - see available commands/subcommands above"))
})
It("returns JSON error", func() {
By("using an invalid command with JSON output", func() {
res := helper.Cmd("odo", "unknown-command", "-o", "json").ShouldFail()
stdout, stderr := res.Out(), res.Err()
Expect(stdout).To(BeEmpty())
Expect(helper.IsJSON(stderr)).To(BeTrue())
})
By("using an invalid describe sub-command with JSON output", func() {
res := helper.Cmd("odo", "describe", "unknown-sub-command", "-o", "json").ShouldFail()
stdout, stderr := res.Out(), res.Err()
Expect(stdout).To(BeEmpty())
Expect(helper.IsJSON(stderr)).To(BeTrue())
})
By("using an invalid list sub-command with JSON output", func() {
res := helper.Cmd("odo", "list", "unknown-sub-command", "-o", "json").ShouldFail()
stdout, stderr := res.Out(), res.Err()
Expect(stdout).To(BeEmpty())
Expect(helper.IsJSON(stderr)).To(BeTrue())
})
By("omitting required subcommand with JSON output", func() {
res := helper.Cmd("odo", "describe", "-o", "json").ShouldFail()
stdout, stderr := res.Out(), res.Err()
Expect(stdout).To(BeEmpty())
Expect(helper.IsJSON(stderr)).To(BeTrue())
})
})
It("returns error when using an invalid command with --help", func() {
output := helper.Cmd("odo", "hello", "--help").ShouldFail().Err()
Expect(output).To(ContainSubstring("unknown command 'hello', type --help for a list of all commands"))