From 3423d526881349d2ca6ca3c5d5d9463891a74fd4 Mon Sep 17 00:00:00 2001 From: Chetan Giradkar Date: Thu, 1 Feb 2024 17:57:51 +0000 Subject: [PATCH] OCM-1888: Add docs for ocm delete account subcommands and arguments Prevously when using the help in OCM-CLI it did not list all available sub commands. This was due to how we currently alias urls in the CLI. Now added documentation about using the urls(path) or provide a resource_id for the resource to be deleted. Another effort has been started to understand and standardize the help format, which in turn will help in the task OCM-5179. The template format has been used from https://pkg.go.dev/text/template. Signed-off-by: Chetan Giradkar --- cmd/ocm/delete/cmd.go | 53 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 44 insertions(+), 9 deletions(-) diff --git a/cmd/ocm/delete/cmd.go b/cmd/ocm/delete/cmd.go index b4391ff0..79b6ca1c 100644 --- a/cmd/ocm/delete/cmd.go +++ b/cmd/ocm/delete/cmd.go @@ -40,14 +40,49 @@ var args struct { } var Cmd = &cobra.Command{ - Use: "delete [flags] PATH", + Use: "delete [flags] (PATH | RESOURCE_ALIAS RESOURCE_ID)", Short: "Send a DELETE request", Long: "Send a DELETE request to the given path.", RunE: run, ValidArgs: urls.Resources(), } +// for template format refer: https://pkg.go.dev/text/template +var usageTemplate = ` +Usage:{{if .Runnable}} + {{.UseLine}}{{end}}{{if .HasAvailableSubCommands}} + {{.CommandPath}} [command]{{end}}{{if .HasExample}} + +Resource Alias: +{{.Example}}{{end}}{{if .HasAvailableSubCommands}}{{$cmds := .Commands}}{{if eq (len .Groups) 0}} + +Available Commands:{{range $cmds}}{{if (or .IsAvailableCommand (eq .Name "help"))}} + {{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{else}}{{range $group := .Groups}} +{{.Title}}{{range $cmds}}{{if (and (eq .GroupID $group.ID) (or .IsAvailableCommand (eq .Name "help")))}} +{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{if not .AllChildCommandsHaveGroup}} + +Additional Commands:{{range $cmds}}{{if (and (eq .GroupID "") (or .IsAvailableCommand (eq .Name "help")))}} +{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{end}}{{end}}{{if .HasAvailableLocalFlags}} + +Flags: +{{.LocalFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasAvailableInheritedFlags}} + +Global Flags: +{{.InheritedFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasHelpSubCommands}} + +Additional help topics:{{range .Commands}}{{if .IsAdditionalHelpTopicCommand}} +{{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableSubCommands}} + +Use "{{.CommandPath}} [command] --help" for more information about a command.{{end}} +` + +var helpTemplate = `{{with (or .Long .Short)}}{{. | trimTrailingWhitespaces}} +{{end}}{{if or .Runnable .HasSubCommands}}{{.UsageString}}{{end}}` + func init() { + Cmd.SetUsageTemplate(usageTemplate) + Cmd.SetHelpTemplate(helpTemplate) + Cmd.Example = " account\n addon\n cluster\n role_binding\n sku_rule\n subscription" fs := Cmd.Flags() arguments.AddParameterFlag(fs, &args.parameter) arguments.AddHeaderFlag(fs, &args.header) @@ -61,13 +96,13 @@ func init() { func run(cmd *cobra.Command, argv []string) error { path, err := urls.Expand(argv) if err != nil { - return fmt.Errorf("Could not create URI: %v", err) + return fmt.Errorf("could not create URI: %w", err) } // Create the client for the OCM API: connection, err := ocm.NewConnection().Build() if err != nil { - return fmt.Errorf("Failed to create OCM connection: %v", err) + return fmt.Errorf("failed to create OCM connection: %w", err) } defer connection.Close() @@ -84,7 +119,7 @@ func run(cmd *cobra.Command, argv []string) error { // Send the request: response, err := request.Send() if err != nil { - return fmt.Errorf("Can't send request: %v", err) + return fmt.Errorf("can't send request: %w", err) } status := response.Status() body := response.Bytes() @@ -94,26 +129,26 @@ func run(cmd *cobra.Command, argv []string) error { err = dump.Pretty(os.Stderr, body) } if err != nil { - return fmt.Errorf("Can't print body: %v", err) + return fmt.Errorf("can't print body: %w", err) } // Load the configuration file: cfg, err := config.Load() if err != nil { - return fmt.Errorf("Can't load config file: %v", err) + return fmt.Errorf("can't load config file: %w", err) } if cfg == nil { - return fmt.Errorf("Not logged in, run the 'login' command") + return fmt.Errorf("not logged in, run the 'login' command") } // Save the configuration: cfg.AccessToken, cfg.RefreshToken, err = connection.Tokens() if err != nil { - return fmt.Errorf("Can't get tokens: %v", err) + return fmt.Errorf("can't get tokens: %w", err) } err = config.Save(cfg) if err != nil { - return fmt.Errorf("Can't save config file: %v", err) + return fmt.Errorf("can't save config file: %w", err) } // Bye: