Skip to content

Commit

Permalink
OCM-1888: Add docs for ocm delete account subcommands and arguments
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
cgiradkar committed Mar 14, 2024
1 parent fab7ccf commit 3423d52
Showing 1 changed file with 44 additions and 9 deletions.
53 changes: 44 additions & 9 deletions cmd/ocm/delete/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()

Expand All @@ -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()
Expand All @@ -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:
Expand Down

0 comments on commit 3423d52

Please sign in to comment.