Skip to content

Commit 6c6f8ef

Browse files
authored
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
1 parent c5f8c5a commit 6c6f8ef

File tree

11 files changed

+57
-8
lines changed

11 files changed

+57
-8
lines changed

pkg/odo/cli/add/binding/binding.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ func NewCmdBinding(name, fullName string) *cobra.Command {
204204
Use: name,
205205
Short: "Add Binding",
206206
Long: "Add a binding between a service and the component in the devfile",
207-
Args: cobra.NoArgs,
207+
Args: genericclioptions.NoArgsAndSilenceJSON,
208208
Example: fmt.Sprintf(addBindingExample, fullName),
209209
Run: func(cmd *cobra.Command, args []string) {
210210
genericclioptions.GenericRun(o, cmd, args)

pkg/odo/cli/cli.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,11 @@ func ShowSubcommands(cmd *cobra.Command, args []string) error {
260260
strs = append(strs, subcmd.Name())
261261
}
262262
}
263+
264+
if log.IsJSON() {
265+
cmd.SilenceUsage = true
266+
cmd.SilenceErrors = true
267+
}
263268
//revive:disable:error-strings This is a top-level error message displayed as is to the end user
264269
return fmt.Errorf("Subcommand not found, use one of the available commands: %s", strings.Join(strs, ", "))
265270
//revive:enable:error-strings
@@ -282,6 +287,11 @@ func ShowHelp(cmd *cobra.Command, args []string) error {
282287
}
283288

284289
//revive:disable:error-strings This is a top-level error message displayed as is to the end user
290+
if log.IsJSON() {
291+
cmd.SilenceUsage = true
292+
cmd.SilenceErrors = true
293+
return errors.New("Invalid command - see available commands/subcommands by running `odo`")
294+
}
285295
return errors.New("Invalid command - see available commands/subcommands above")
286296
//revive:enable:error-strings
287297
}

pkg/odo/cli/delete/component/component.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ func NewCmdComponent(name, fullName string) *cobra.Command {
216216
Use: name,
217217
Short: "Delete component",
218218
Long: "Delete component",
219-
Args: cobra.NoArgs,
219+
Args: genericclioptions.NoArgsAndSilenceJSON,
220220
Example: fmt.Sprintf(deleteExample, fullName),
221221
Run: func(cmd *cobra.Command, args []string) {
222222
genericclioptions.GenericRun(o, cmd, args)

pkg/odo/cli/describe/binding.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ func NewCmdBinding(name, fullName string) *cobra.Command {
119119
Use: name,
120120
Short: "Describe bindings",
121121
Long: "Describe bindings",
122-
Args: cobra.NoArgs,
122+
Args: genericclioptions.NoArgsAndSilenceJSON,
123123
Example: fmt.Sprintf(describeBindingExample, fullName),
124124
Run: func(cmd *cobra.Command, args []string) {
125125
genericclioptions.GenericRun(o, cmd, args)

pkg/odo/cli/describe/component.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ func NewCmdComponent(name, fullName string) *cobra.Command {
237237
Use: name,
238238
Short: "Describe a component",
239239
Long: "Describe a component",
240-
Args: cobra.NoArgs,
240+
Args: genericclioptions.NoArgsAndSilenceJSON,
241241
Example: fmt.Sprintf(describeExample, fullName),
242242
Run: func(cmd *cobra.Command, args []string) {
243243
genericclioptions.GenericRun(o, cmd, args)

pkg/odo/cli/list/component/list.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func NewCmdComponentList(name, fullName string) *cobra.Command {
136136
Short: "List all components in the current namespace",
137137
Long: "List all components in the current namespace.",
138138
Example: fmt.Sprintf(listExample, fullName),
139-
Args: cobra.NoArgs,
139+
Args: genericclioptions.NoArgsAndSilenceJSON,
140140
Annotations: map[string]string{"command": "management"},
141141
Run: func(cmd *cobra.Command, args []string) {
142142
genericclioptions.GenericRun(o, cmd, args)

pkg/odo/cli/list/list.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ func NewCmdList(name, fullName string) *cobra.Command {
153153
Short: "List all components in the current namespace",
154154
Long: "List all components in the current namespace.",
155155
Example: fmt.Sprintf(listExample, fullName),
156-
Args: cobra.NoArgs,
156+
Args: genericclioptions.NoArgsAndSilenceJSON,
157157
Annotations: map[string]string{"command": "management"},
158158
Run: func(cmd *cobra.Command, args []string) {
159159
genericclioptions.GenericRun(o, cmd, args)

pkg/odo/cli/logout/logout.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func NewCmdLogout(name, fullName string) *cobra.Command {
6060
Short: "Logout of the cluster",
6161
Long: "Logout of the cluster.",
6262
Example: fmt.Sprintf(example, fullName),
63-
Args: cobra.NoArgs,
63+
Args: genericclioptions.NoArgsAndSilenceJSON,
6464
Run: func(cmd *cobra.Command, args []string) {
6565
genericclioptions.GenericRun(o, cmd, args)
6666
},

pkg/odo/cli/remove/binding/binding.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ func NewCmdBinding(name, fullName string) *cobra.Command {
8686
Use: name,
8787
Short: "Remove Binding",
8888
Long: "Remove a binding between a service and the component from the devfile",
89-
Args: cobra.NoArgs,
89+
Args: genericclioptions.NoArgsAndSilenceJSON,
9090
Example: fmt.Sprintf(removeBindingExample, fullName),
9191
Run: func(cmd *cobra.Command, args []string) {
9292
genericclioptions.GenericRun(o, cmd, args)

pkg/odo/genericclioptions/runnable.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,3 +233,12 @@ func CheckMachineReadableOutputCommand(cmd *cobra.Command) error {
233233
}
234234
return nil
235235
}
236+
237+
// NoArgsAndSilenceJSON returns the NoArgs value, and silence output when JSON output is activated
238+
func NoArgsAndSilenceJSON(cmd *cobra.Command, args []string) error {
239+
if log.IsJSON() {
240+
cmd.SilenceUsage = true
241+
cmd.SilenceErrors = true
242+
}
243+
return cobra.NoArgs(cmd, args)
244+
}

0 commit comments

Comments
 (0)