diff --git a/pkg/odo/cli/application/describe.go b/pkg/odo/cli/application/describe.go index 09c28feef59..d561b5c02eb 100644 --- a/pkg/odo/cli/application/describe.go +++ b/pkg/odo/cli/application/describe.go @@ -112,17 +112,19 @@ func (o *DescribeOptions) Run() (err error) { func NewCmdDescribe(name, fullName string) *cobra.Command { o := NewDescribeOptions() command := &cobra.Command{ - Use: fmt.Sprintf("%s [application_name]", name), - Short: "Describe the given application", - Long: "Describe the given application", - Example: fmt.Sprintf(describeExample, fullName), - Args: cobra.MaximumNArgs(1), + Use: fmt.Sprintf("%s [application_name]", name), + Short: "Describe the given application", + Long: "Describe the given application", + Example: fmt.Sprintf(describeExample, fullName), + Args: cobra.MaximumNArgs(1), + Annotations: map[string]string{"machineoutput": "json"}, Run: func(cmd *cobra.Command, args []string) { genericclioptions.GenericRun(o, cmd, args) }, } completion.RegisterCommandHandler(command, completion.AppCompletionHandler) + project.AddProjectFlag(command) return command } diff --git a/pkg/odo/cli/application/list.go b/pkg/odo/cli/application/list.go index 71640101efe..9124d4c77ba 100644 --- a/pkg/odo/cli/application/list.go +++ b/pkg/odo/cli/application/list.go @@ -108,11 +108,12 @@ func (o *ListOptions) Run() (err error) { func NewCmdList(name, fullName string) *cobra.Command { o := NewListOptions() command := &cobra.Command{ - Use: name, - Short: "List all applications in the current project", - Long: "List all applications in the current project", - Example: fmt.Sprintf(listExample, fullName), - Args: cobra.NoArgs, + Use: name, + Short: "List all applications in the current project", + Long: "List all applications in the current project", + Example: fmt.Sprintf(listExample, fullName), + Args: cobra.NoArgs, + Annotations: map[string]string{"machineoutput": "json"}, Run: func(cmd *cobra.Command, args []string) { genericclioptions.GenericRun(o, cmd, args) }, diff --git a/pkg/odo/cli/catalog/list/components.go b/pkg/odo/cli/catalog/list/components.go index 999c3ce24dd..ec69074296c 100644 --- a/pkg/odo/cli/catalog/list/components.go +++ b/pkg/odo/cli/catalog/list/components.go @@ -98,10 +98,11 @@ func NewCmdCatalogListComponents(name, fullName string) *cobra.Command { o := NewListComponentsOptions() return &cobra.Command{ - Use: name, - Short: "List all components", - Long: "List all available component types from OpenShift's Image Builder", - Example: fmt.Sprintf(componentsExample, fullName), + Use: name, + Short: "List all components", + Long: "List all available component types from OpenShift's Image Builder", + Example: fmt.Sprintf(componentsExample, fullName), + Annotations: map[string]string{"machineoutput": "json"}, Run: func(cmd *cobra.Command, args []string) { genericclioptions.GenericRun(o, cmd, args) }, diff --git a/pkg/odo/cli/catalog/list/services.go b/pkg/odo/cli/catalog/list/services.go index e56f6f7490d..78c4b0819b7 100644 --- a/pkg/odo/cli/catalog/list/services.go +++ b/pkg/odo/cli/catalog/list/services.go @@ -67,11 +67,12 @@ func (o *ListServicesOptions) Run() (err error) { func NewCmdCatalogListServices(name, fullName string) *cobra.Command { o := NewListServicesOptions() return &cobra.Command{ - Use: name, - Short: "Lists all available services", - Long: "Lists all available services", - Example: fmt.Sprintf(servicesExample, fullName), - Args: cobra.ExactArgs(0), + Use: name, + Short: "Lists all available services", + Long: "Lists all available services", + Example: fmt.Sprintf(servicesExample, fullName), + Args: cobra.ExactArgs(0), + Annotations: map[string]string{"machineoutput": "json"}, Run: func(cmd *cobra.Command, args []string) { genericclioptions.GenericRun(o, cmd, args) }, diff --git a/pkg/odo/cli/cli.go b/pkg/odo/cli/cli.go index 47f83f9adc7..a12cbd9cf71 100644 --- a/pkg/odo/cli/cli.go +++ b/pkg/odo/cli/cli.go @@ -140,6 +140,7 @@ func NewCmdOdo(name, fullName string) *cobra.Command { rootCmd.SetUsageTemplate(rootUsageTemplate) cobra.AddTemplateFunc("CapitalizeFlagDescriptions", odoutil.CapitalizeFlagDescriptions) + cobra.AddTemplateFunc("ModifyAdditionalFlags", odoutil.ModifyAdditionalFlags) rootCmd.AddCommand( application.NewCmdApplication(application.RecommendedCommandName, util.GetFullName(fullName, application.RecommendedCommandName)), diff --git a/pkg/odo/cli/component/create.go b/pkg/odo/cli/component/create.go index c446b1e9e2b..a75f36843e5 100644 --- a/pkg/odo/cli/component/create.go +++ b/pkg/odo/cli/component/create.go @@ -546,11 +546,12 @@ func ensureAndLogProperResourceUsage(resource, resourceMin, resourceMax, resourc func NewCmdCreate(name, fullName string) *cobra.Command { co := NewCreateOptions() var componentCreateCmd = &cobra.Command{ - Use: fmt.Sprintf("%s [component_name] [flags]", name), - Short: "Create a new component", - Long: createLongDesc, - Example: fmt.Sprintf(createExample, fullName), - Args: cobra.RangeArgs(0, 2), + Use: fmt.Sprintf("%s [component_name] [flags]", name), + Short: "Create a new component", + Long: createLongDesc, + Example: fmt.Sprintf(createExample, fullName), + Args: cobra.RangeArgs(0, 2), + Annotations: map[string]string{"machineoutput": "json", "component": "component"}, Run: func(cmd *cobra.Command, args []string) { genericclioptions.GenericRun(co, cmd, args) }, @@ -568,7 +569,6 @@ func NewCmdCreate(name, fullName string) *cobra.Command { componentCreateCmd.Flags().StringSliceVarP(&co.componentPorts, "port", "p", []string{}, "Ports to be used when the component is created (ex. 8080,8100/tcp,9100/udp)") componentCreateCmd.Flags().StringSliceVar(&co.componentEnvVars, "env", []string{}, "Environmental variables for the component. For example --env VariableName=Value") // Add a defined annotation in order to appear in the help menu - componentCreateCmd.Annotations = map[string]string{"command": "component"} componentCreateCmd.SetUsageTemplate(odoutil.CmdUsageTemplate) // Adding `--now` flag diff --git a/pkg/odo/cli/component/delete.go b/pkg/odo/cli/component/delete.go index fb8a2f30802..11985d689ec 100644 --- a/pkg/odo/cli/component/delete.go +++ b/pkg/odo/cli/component/delete.go @@ -119,11 +119,12 @@ func NewCmdDelete(name, fullName string) *cobra.Command { do := NewDeleteOptions() var componentDeleteCmd = &cobra.Command{ - Use: fmt.Sprintf("%s ", name), - Short: "Delete component", - Long: "Delete component.", - Example: fmt.Sprintf(deleteExample, fullName), - Args: cobra.MaximumNArgs(1), + Use: fmt.Sprintf("%s ", name), + Short: "Delete component", + Long: "Delete component.", + Example: fmt.Sprintf(deleteExample, fullName), + Args: cobra.MaximumNArgs(1), + Annotations: map[string]string{"command": "component"}, Run: func(cmd *cobra.Command, args []string) { genericclioptions.GenericRun(do, cmd, args) }, @@ -133,7 +134,6 @@ func NewCmdDelete(name, fullName string) *cobra.Command { componentDeleteCmd.Flags().BoolVarP(&do.componentDeleteAllFlag, "all", "a", false, "Delete component and local config") // Add a defined annotation in order to appear in the help menu - componentDeleteCmd.Annotations = map[string]string{"command": "component"} componentDeleteCmd.SetUsageTemplate(odoutil.CmdUsageTemplate) completion.RegisterCommandHandler(componentDeleteCmd, completion.ComponentNameCompletionHandler) //Adding `--context` flag diff --git a/pkg/odo/cli/component/describe.go b/pkg/odo/cli/component/describe.go index 67428bb7f61..699348ea8be 100644 --- a/pkg/odo/cli/component/describe.go +++ b/pkg/odo/cli/component/describe.go @@ -88,18 +88,18 @@ func NewCmdDescribe(name, fullName string) *cobra.Command { do := NewDescribeOptions() var describeCmd = &cobra.Command{ - Use: fmt.Sprintf("%s [component_name]", name), - Short: "Describe component", - Long: `Describe component.`, - Example: fmt.Sprintf(describeExample, fullName), - Args: cobra.RangeArgs(0, 1), + Use: fmt.Sprintf("%s [component_name]", name), + Short: "Describe component", + Long: `Describe component.`, + Example: fmt.Sprintf(describeExample, fullName), + Args: cobra.RangeArgs(0, 1), + Annotations: map[string]string{"machineoutput": "json", "command": "component"}, Run: func(cmd *cobra.Command, args []string) { genericclioptions.GenericRun(do, cmd, args) }, } // Add a defined annotation in order to appear in the help menu - describeCmd.Annotations = map[string]string{"command": "component"} describeCmd.SetUsageTemplate(odoutil.CmdUsageTemplate) completion.RegisterCommandHandler(describeCmd, completion.ComponentNameCompletionHandler) // Adding --context flag diff --git a/pkg/odo/cli/component/link.go b/pkg/odo/cli/component/link.go index 6e9adfdef41..7baff0fec5a 100644 --- a/pkg/odo/cli/component/link.go +++ b/pkg/odo/cli/component/link.go @@ -119,11 +119,12 @@ func NewCmdLink(name, fullName string) *cobra.Command { o := NewLinkOptions() linkCmd := &cobra.Command{ - Use: fmt.Sprintf("%s --component [component] OR %s --component [component]", name, name), - Short: "Link component to a service or component", - Long: linkLongDesc, - Example: fmt.Sprintf(linkExample, fullName), - Args: cobra.ExactArgs(1), + Use: fmt.Sprintf("%s --component [component] OR %s --component [component]", name, name), + Short: "Link component to a service or component", + Long: linkLongDesc, + Example: fmt.Sprintf(linkExample, fullName), + Args: cobra.ExactArgs(1), + Annotations: map[string]string{"command": "component"}, Run: func(cmd *cobra.Command, args []string) { genericclioptions.GenericRun(o, cmd, args) }, @@ -134,7 +135,6 @@ func NewCmdLink(name, fullName string) *cobra.Command { linkCmd.PersistentFlags().BoolVar(&o.waitForTarget, "wait-for-target", false, "If enabled, the link command will wait for the service to be provisioned (has no effect when linking to a component)") // Add a defined annotation in order to appear in the help menu - linkCmd.Annotations = map[string]string{"command": "component"} linkCmd.SetUsageTemplate(util.CmdUsageTemplate) //Adding `--project` flag projectCmd.AddProjectFlag(linkCmd) diff --git a/pkg/odo/cli/component/list.go b/pkg/odo/cli/component/list.go index 94c39113965..75da1fd297b 100644 --- a/pkg/odo/cli/component/list.go +++ b/pkg/odo/cli/component/list.go @@ -149,17 +149,17 @@ func NewCmdList(name, fullName string) *cobra.Command { o := NewListOptions() var componentListCmd = &cobra.Command{ - Use: name, - Short: "List all components in the current application", - Long: "List all components in the current application.", - Example: fmt.Sprintf(listExample, fullName), - Args: cobra.NoArgs, + Use: name, + Short: "List all components in the current application", + Long: "List all components in the current application.", + Example: fmt.Sprintf(listExample, fullName), + Args: cobra.NoArgs, + Annotations: map[string]string{"machineoutput": "json", "command": "component"}, Run: func(cmd *cobra.Command, args []string) { genericclioptions.GenericRun(o, cmd, args) }, } // Add a defined annotation in order to appear in the help menu - componentListCmd.Annotations = map[string]string{"command": "component"} genericclioptions.AddContextFlag(componentListCmd, &o.componentContext) componentListCmd.Flags().StringVar(&o.pathFlag, "path", "", "path of the directory to scan for odo component directories") componentListCmd.Flags().BoolVar(&o.allFlag, "all", false, "lists all components") diff --git a/pkg/odo/cli/component/log.go b/pkg/odo/cli/component/log.go index 9488f5094a6..696ee2ca5d1 100644 --- a/pkg/odo/cli/component/log.go +++ b/pkg/odo/cli/component/log.go @@ -61,11 +61,12 @@ func NewCmdLog(name, fullName string) *cobra.Command { o := NewLogOptions() var logCmd = &cobra.Command{ - Use: fmt.Sprintf("%s [component_name]", name), - Short: "Retrieve the log for the given component", - Long: `Retrieve the log for the given component`, - Example: fmt.Sprintf(logExample, fullName), - Args: cobra.RangeArgs(0, 1), + Use: fmt.Sprintf("%s [component_name]", name), + Short: "Retrieve the log for the given component", + Long: `Retrieve the log for the given component`, + Example: fmt.Sprintf(logExample, fullName), + Args: cobra.RangeArgs(0, 1), + Annotations: map[string]string{"command": "component"}, Run: func(cmd *cobra.Command, args []string) { genericclioptions.GenericRun(o, cmd, args) }, @@ -74,7 +75,6 @@ func NewCmdLog(name, fullName string) *cobra.Command { logCmd.Flags().BoolVarP(&o.logFollow, "follow", "f", false, "Follow logs") // Add a defined annotation in order to appear in the help menu - logCmd.Annotations = map[string]string{"command": "component"} logCmd.SetUsageTemplate(odoutil.CmdUsageTemplate) completion.RegisterCommandHandler(logCmd, completion.ComponentNameCompletionHandler) // Adding `--context` flag diff --git a/pkg/odo/cli/component/push.go b/pkg/odo/cli/component/push.go index 45fb12436f8..51c66628517 100644 --- a/pkg/odo/cli/component/push.go +++ b/pkg/odo/cli/component/push.go @@ -109,11 +109,12 @@ func NewCmdPush(name, fullName string) *cobra.Command { po := NewPushOptions() var pushCmd = &cobra.Command{ - Use: fmt.Sprintf("%s [component name]", name), - Short: "Push source code to a component", - Long: `Push source code to a component.`, - Example: fmt.Sprintf(pushCmdExample, fullName), - Args: cobra.MaximumNArgs(1), + Use: fmt.Sprintf("%s [component name]", name), + Short: "Push source code to a component", + Long: `Push source code to a component.`, + Example: fmt.Sprintf(pushCmdExample, fullName), + Args: cobra.MaximumNArgs(1), + Annotations: map[string]string{"command": "component"}, Run: func(cmd *cobra.Command, args []string) { genericclioptions.GenericRun(po, cmd, args) }, @@ -126,7 +127,6 @@ func NewCmdPush(name, fullName string) *cobra.Command { pushCmd.Flags().BoolVarP(&po.forceBuild, "force-build", "f", false, "Use force-build flag to force building the component") // Add a defined annotation in order to appear in the help menu - pushCmd.Annotations = map[string]string{"command": "component"} pushCmd.SetUsageTemplate(odoutil.CmdUsageTemplate) completion.RegisterCommandHandler(pushCmd, completion.ComponentNameCompletionHandler) completion.RegisterCommandFlagHandler(pushCmd, "context", completion.FileCompletionHandler) diff --git a/pkg/odo/cli/component/unlink.go b/pkg/odo/cli/component/unlink.go index d0e3095dbda..2f2ccd4ddef 100644 --- a/pkg/odo/cli/component/unlink.go +++ b/pkg/odo/cli/component/unlink.go @@ -73,11 +73,12 @@ func NewCmdUnlink(name, fullName string) *cobra.Command { o := NewUnlinkOptions() unlinkCmd := &cobra.Command{ - Use: fmt.Sprintf("%s --component [component] OR %s --component [component]", name, name), - Short: "Unlink component to a service or component", - Long: unlinkLongDesc, - Example: fmt.Sprintf(unlinkExample, fullName), - Args: cobra.ExactArgs(1), + Use: fmt.Sprintf("%s --component [component] OR %s --component [component]", name, name), + Short: "Unlink component to a service or component", + Long: unlinkLongDesc, + Example: fmt.Sprintf(unlinkExample, fullName), + Args: cobra.ExactArgs(1), + Annotations: map[string]string{"command": "component"}, Run: func(cmd *cobra.Command, args []string) { genericclioptions.GenericRun(o, cmd, args) }, @@ -87,7 +88,6 @@ func NewCmdUnlink(name, fullName string) *cobra.Command { unlinkCmd.PersistentFlags().BoolVarP(&o.wait, "wait", "w", false, "If enabled the link will return only when the component is fully running after the link is deleted") // Add a defined annotation in order to appear in the help menu - unlinkCmd.Annotations = map[string]string{"command": "component"} unlinkCmd.SetUsageTemplate(util.CmdUsageTemplate) //Adding `--project` flag projectCmd.AddProjectFlag(unlinkCmd) diff --git a/pkg/odo/cli/component/update.go b/pkg/odo/cli/component/update.go index 45cd8146800..588a06344bc 100644 --- a/pkg/odo/cli/component/update.go +++ b/pkg/odo/cli/component/update.go @@ -153,11 +153,12 @@ func NewCmdUpdate(name, fullName string) *cobra.Command { uo := NewUpdateOptions() var updateCmd = &cobra.Command{ - Use: name, - Args: cobra.MaximumNArgs(1), - Short: "Update the source code path of a component", - Long: "Update the source code path of a component", - Example: fmt.Sprintf(updateCmdExample, fullName), + Use: name, + Short: "Update the source code path of a component", + Long: "Update the source code path of a component", + Example: fmt.Sprintf(updateCmdExample, fullName), + Args: cobra.MaximumNArgs(1), + Annotations: map[string]string{"command": "component"}, Run: func(cmd *cobra.Command, args []string) { genericclioptions.GenericRun(uo, cmd, args) }, @@ -167,7 +168,6 @@ func NewCmdUpdate(name, fullName string) *cobra.Command { updateCmd.Flags().StringVarP(&uo.local, "local", "l", "", "Use local directory as a source for component.") updateCmd.Flags().StringVarP(&uo.ref, "ref", "r", "", "Use a specific ref e.g. commit, branch or tag of the git repository") // Add a defined annotation in order to appear in the help menu - updateCmd.Annotations = map[string]string{"command": "component"} updateCmd.SetUsageTemplate(odoutil.CmdUsageTemplate) //Adding `--application` flag diff --git a/pkg/odo/cli/component/watch.go b/pkg/odo/cli/component/watch.go index e046daecdf6..1f3143ac244 100644 --- a/pkg/odo/cli/component/watch.go +++ b/pkg/odo/cli/component/watch.go @@ -147,11 +147,12 @@ func NewCmdWatch(name, fullName string) *cobra.Command { wo := NewWatchOptions() var watchCmd = &cobra.Command{ - Use: fmt.Sprintf("%s [component name]", name), - Short: "Watch for changes, update component on change", - Long: watchLongDesc, - Example: fmt.Sprintf(watchExample, fullName), - Args: cobra.MaximumNArgs(1), + Use: fmt.Sprintf("%s [component name]", name), + Short: "Watch for changes, update component on change", + Long: watchLongDesc, + Example: fmt.Sprintf(watchExample, fullName), + Args: cobra.MaximumNArgs(1), + Annotations: map[string]string{"command": "component"}, Run: func(cmd *cobra.Command, args []string) { genericclioptions.GenericRun(wo, cmd, args) }, @@ -162,7 +163,6 @@ func NewCmdWatch(name, fullName string) *cobra.Command { watchCmd.Flags().IntVar(&wo.delay, "delay", 1, "Time in seconds between a detection of code change and push.delay=0 means changes will be pushed as soon as they are detected which can cause performance issues") // Add a defined annotation in order to appear in the help menu - watchCmd.Annotations = map[string]string{"command": "component"} watchCmd.SetUsageTemplate(odoutil.CmdUsageTemplate) // Adding context flag genericclioptions.AddContextFlag(watchCmd, &wo.componentContext) diff --git a/pkg/odo/cli/project/create.go b/pkg/odo/cli/project/create.go index 1e2977774ce..480abd40204 100644 --- a/pkg/odo/cli/project/create.go +++ b/pkg/odo/cli/project/create.go @@ -93,11 +93,12 @@ func NewCmdProjectCreate(name, fullName string) *cobra.Command { o := NewProjectCreateOptions() projectCreateCmd := &cobra.Command{ - Use: name, - Short: createShortDesc, - Long: createLongDesc, - Example: fmt.Sprintf(createExample, fullName), - Args: cobra.ExactArgs(1), + Use: name, + Short: createShortDesc, + Long: createLongDesc, + Example: fmt.Sprintf(createExample, fullName), + Args: cobra.ExactArgs(1), + Annotations: map[string]string{"machineoutput": "json"}, Run: func(cmd *cobra.Command, args []string) { genericclioptions.GenericRun(o, cmd, args) }, diff --git a/pkg/odo/cli/project/delete.go b/pkg/odo/cli/project/delete.go index 80486c68dac..e509872ce69 100644 --- a/pkg/odo/cli/project/delete.go +++ b/pkg/odo/cli/project/delete.go @@ -98,11 +98,12 @@ func NewCmdProjectDelete(name, fullName string) *cobra.Command { o := NewProjectDeleteOptions() projectDeleteCmd := &cobra.Command{ - Use: name, - Short: deleteShortDesc, - Long: deleteLongDesc, - Example: fmt.Sprintf(deleteExample, fullName), - Args: cobra.ExactArgs(1), + Use: name, + Short: deleteShortDesc, + Long: deleteLongDesc, + Example: fmt.Sprintf(deleteExample, fullName), + Args: cobra.ExactArgs(1), + Annotations: map[string]string{"machineoutput": "json"}, Run: func(cmd *cobra.Command, args []string) { genericclioptions.GenericRun(o, cmd, args) }, diff --git a/pkg/odo/cli/project/list.go b/pkg/odo/cli/project/list.go index a163edbcb4c..15293bc1e07 100644 --- a/pkg/odo/cli/project/list.go +++ b/pkg/odo/cli/project/list.go @@ -81,11 +81,12 @@ func (plo *ProjectListOptions) Run() (err error) { func NewCmdProjectList(name, fullName string) *cobra.Command { o := NewProjectListOptions() projectListCmd := &cobra.Command{ - Use: name, - Short: listLongDesc, - Long: listLongDesc, - Example: fmt.Sprintf(listExample, fullName), - Args: cobra.ExactArgs(0), + Use: name, + Short: listLongDesc, + Long: listLongDesc, + Example: fmt.Sprintf(listExample, fullName), + Args: cobra.ExactArgs(0), + Annotations: map[string]string{"machineoutput": "json"}, Run: func(cmd *cobra.Command, args []string) { genericclioptions.GenericRun(o, cmd, args) }, diff --git a/pkg/odo/cli/service/list.go b/pkg/odo/cli/service/list.go index 7dc2a478fe7..ae83f1d7ccb 100644 --- a/pkg/odo/cli/service/list.go +++ b/pkg/odo/cli/service/list.go @@ -82,11 +82,12 @@ func (o *ServiceListOptions) Run() (err error) { func NewCmdServiceList(name, fullName string) *cobra.Command { o := NewServiceListOptions() serviceListCmd := &cobra.Command{ - Use: name, - Short: "List all services in the current application", - Long: listLongDesc, - Example: fmt.Sprintf(listExample, fullName), - Args: cobra.NoArgs, + Use: name, + Short: "List all services in the current application", + Long: listLongDesc, + Example: fmt.Sprintf(listExample, fullName), + Args: cobra.NoArgs, + Annotations: map[string]string{"machineoutput": "json"}, Run: func(cmd *cobra.Command, args []string) { genericclioptions.GenericRun(o, cmd, args) }, diff --git a/pkg/odo/cli/storage/create.go b/pkg/odo/cli/storage/create.go index ab987c87883..fd8a1527418 100644 --- a/pkg/odo/cli/storage/create.go +++ b/pkg/odo/cli/storage/create.go @@ -86,11 +86,12 @@ func (o *StorageCreateOptions) Run() (err error) { func NewCmdStorageCreate(name, fullName string) *cobra.Command { o := NewStorageCreateOptions() storageCreateCmd := &cobra.Command{ - Use: name, - Short: urlCreateShortDesc, - Long: urlCreateLongDesc, - Example: fmt.Sprintf(urlCreateExample, fullName), - Args: cobra.MaximumNArgs(1), + Use: name, + Short: urlCreateShortDesc, + Long: urlCreateLongDesc, + Example: fmt.Sprintf(urlCreateExample, fullName), + Args: cobra.MaximumNArgs(1), + Annotations: map[string]string{"machineoutput": "json"}, Run: func(cmd *cobra.Command, args []string) { genericclioptions.GenericRun(o, cmd, args) }, diff --git a/pkg/odo/cli/storage/list.go b/pkg/odo/cli/storage/list.go index f6314efd21f..9ab864b5216 100644 --- a/pkg/odo/cli/storage/list.go +++ b/pkg/odo/cli/storage/list.go @@ -102,11 +102,12 @@ func (o *StorageListOptions) Run() (err error) { func NewCmdStorageList(name, fullName string) *cobra.Command { o := NewStorageListOptions() storageListCmd := &cobra.Command{ - Use: name, - Short: storageListShortDesc, - Long: storageListLongDesc, - Example: fmt.Sprintf(storageListExample, fullName), - Args: cobra.MaximumNArgs(1), + Use: name, + Short: storageListShortDesc, + Long: storageListLongDesc, + Example: fmt.Sprintf(storageListExample, fullName), + Args: cobra.MaximumNArgs(1), + Annotations: map[string]string{"machineoutput": "json"}, Run: func(cmd *cobra.Command, args []string) { genericclioptions.GenericRun(o, cmd, args) }, diff --git a/pkg/odo/cli/url/list.go b/pkg/odo/cli/url/list.go index cfb867a221b..1b50f2a3c65 100644 --- a/pkg/odo/cli/url/list.go +++ b/pkg/odo/cli/url/list.go @@ -99,11 +99,12 @@ func (o *URLListOptions) Run() (err error) { func NewCmdURLList(name, fullName string) *cobra.Command { o := NewURLListOptions() urlListCmd := &cobra.Command{ - Use: name, - Short: urlListShortDesc, - Long: urlListLongDesc, - Example: fmt.Sprintf(urlListExample, fullName), - Args: cobra.NoArgs, + Use: name, + Short: urlListShortDesc, + Long: urlListLongDesc, + Example: fmt.Sprintf(urlListExample, fullName), + Args: cobra.NoArgs, + Annotations: map[string]string{"machineoutput": "json"}, Run: func(cmd *cobra.Command, args []string) { genericclioptions.GenericRun(o, cmd, args) }, diff --git a/pkg/odo/genericclioptions/runnable.go b/pkg/odo/genericclioptions/runnable.go index aeaa8af3969..3796589543c 100644 --- a/pkg/odo/genericclioptions/runnable.go +++ b/pkg/odo/genericclioptions/runnable.go @@ -17,10 +17,14 @@ type Runnable interface { } func GenericRun(o Runnable, cmd *cobra.Command, args []string) { + //flag.CommandLine.MarkHidden("o") + + //flag.CommandLine.Lookup("o").Hidden = true + cmd.Flag("o").Hidden = true // CheckMachineReadableOutput // fixes / checks all related machine readable output functions - CheckMachineReadableOutputCommand() + CheckMachineReadableOutputCommand(cmd) // Run completion, validation and run. util.LogErrorAndExit(o.Complete(cmd.Name(), cmd, args), "") @@ -30,15 +34,30 @@ func GenericRun(o Runnable, cmd *cobra.Command, args []string) { // CheckMachineReadableOutputCommand performs machine-readable output functions required to // have it work correctly -func CheckMachineReadableOutputCommand() { +func CheckMachineReadableOutputCommand(cmd *cobra.Command) { - // Check that the -o flag has been correctly set as json. + // Get the needed values outputFlag := pflag.Lookup("o") - if outputFlag != nil && outputFlag.Changed && outputFlag.Value.String() != "json" { + hasFlagChanged := outputFlag != nil && outputFlag.Changed + machineOutput := cmd.Annotations["machineoutput"] + + // Check the valid output + if hasFlagChanged && outputFlag.Value.String() != "json" { log.Error("Please input a valid output format for -o, available format: json") os.Exit(1) } + // Check that if -o json has been passed, that the command actually USES json.. if not, error out. + if hasFlagChanged && outputFlag.Value.String() == "json" && machineOutput == "" { + + // By default we "disable" logging, so undisable it so that the below error can be shown. + flag.Set("o", "") + + // Output the error + log.Error("Machine readable output is not yet implemented for this command") + os.Exit(1) + } + // Before running anything, we will make sure that no verbose output is made // This is a HACK to manually override `-v 4` to `-v 0` (in which we have no glog.V(0) in our code... // in order to have NO verbose output when combining both `-o json` and `-v 4` so json output diff --git a/pkg/odo/util/cmdutils.go b/pkg/odo/util/cmdutils.go index ad69fec77fa..219d9145d07 100644 --- a/pkg/odo/util/cmdutils.go +++ b/pkg/odo/util/cmdutils.go @@ -187,6 +187,28 @@ func VisitCommands(cmd *cobra.Command, f func(*cobra.Command)) { } } +// ModifyAdditionalFlags modifies the flags and updates the descriptions +// as well as changes whether or not machine readable output +// has been passed in.. +// +// Return the flag usages for the help outout +func ModifyAdditionalFlags(cmd *cobra.Command) string { + + // Hide the machine readable output if the command + // does not have the annotation. + machineOutput := cmd.Annotations["machineoutput"] + f := cmd.InheritedFlags() + + f.VisitAll(func(f *pflag.Flag) { + // Remove json flag if machineoutput has not been passed in + if f.Name == "o" && machineOutput == "" { + f.Hidden = true + } + }) + + return CapitalizeFlagDescriptions(f) +} + // CapitalizeFlagDescriptions adds capitalizations func CapitalizeFlagDescriptions(f *pflag.FlagSet) string { f.VisitAll(func(f *pflag.Flag) { @@ -214,8 +236,8 @@ Available Commands:{{range .Commands}}{{if .IsAvailableCommand}} Flags: {{CapitalizeFlagDescriptions .LocalFlags | trimRightSpace}}{{end}}{{ if .HasAvailableInheritedFlags}} -Global Flags: -{{CapitalizeFlagDescriptions .InheritedFlags | trimRightSpace}}{{end}}{{if .HasHelpSubCommands}} +Additional Flags: +{{ModifyAdditionalFlags . | trimRightSpace}}{{end}}{{if .HasHelpSubCommands}} Additional help topics:{{range .Commands}}{{if .IsHelpCommand}} {{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{ if .HasAvailableSubCommands }} diff --git a/tests/integration/component.go b/tests/integration/component.go index 7cd640c8632..5ba210ed0c5 100644 --- a/tests/integration/component.go +++ b/tests/integration/component.go @@ -39,6 +39,18 @@ func componentTests(args ...string) { os.Unsetenv("GLOBALODOCONFIG") }) + Context("Generic machine readable output tests", func() { + + It("Command should fail if json is non-existent for a command<`2`>", func() { + + // `odo version` does not have json implemented, and thus we will use this + // as a test + output := helper.CmdShouldFail("odo", "version", "-o", "json") + Expect(output).To(ContainSubstring("Machine readable output is not yet implemented for this command")) + }) + + }) + Context("Creating component", func() { JustBeforeEach(func() { project = helper.CreateRandProject()