Skip to content

Commit 3423d52

Browse files
committed
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 <[email protected]>
1 parent fab7ccf commit 3423d52

File tree

1 file changed

+44
-9
lines changed

1 file changed

+44
-9
lines changed

cmd/ocm/delete/cmd.go

+44-9
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,49 @@ var args struct {
4040
}
4141

4242
var Cmd = &cobra.Command{
43-
Use: "delete [flags] PATH",
43+
Use: "delete [flags] (PATH | RESOURCE_ALIAS RESOURCE_ID)",
4444
Short: "Send a DELETE request",
4545
Long: "Send a DELETE request to the given path.",
4646
RunE: run,
4747
ValidArgs: urls.Resources(),
4848
}
4949

50+
// for template format refer: https://pkg.go.dev/text/template
51+
var usageTemplate = `
52+
Usage:{{if .Runnable}}
53+
{{.UseLine}}{{end}}{{if .HasAvailableSubCommands}}
54+
{{.CommandPath}} [command]{{end}}{{if .HasExample}}
55+
56+
Resource Alias:
57+
{{.Example}}{{end}}{{if .HasAvailableSubCommands}}{{$cmds := .Commands}}{{if eq (len .Groups) 0}}
58+
59+
Available Commands:{{range $cmds}}{{if (or .IsAvailableCommand (eq .Name "help"))}}
60+
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{else}}{{range $group := .Groups}}
61+
{{.Title}}{{range $cmds}}{{if (and (eq .GroupID $group.ID) (or .IsAvailableCommand (eq .Name "help")))}}
62+
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{if not .AllChildCommandsHaveGroup}}
63+
64+
Additional Commands:{{range $cmds}}{{if (and (eq .GroupID "") (or .IsAvailableCommand (eq .Name "help")))}}
65+
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{end}}{{end}}{{if .HasAvailableLocalFlags}}
66+
67+
Flags:
68+
{{.LocalFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasAvailableInheritedFlags}}
69+
70+
Global Flags:
71+
{{.InheritedFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasHelpSubCommands}}
72+
73+
Additional help topics:{{range .Commands}}{{if .IsAdditionalHelpTopicCommand}}
74+
{{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableSubCommands}}
75+
76+
Use "{{.CommandPath}} [command] --help" for more information about a command.{{end}}
77+
`
78+
79+
var helpTemplate = `{{with (or .Long .Short)}}{{. | trimTrailingWhitespaces}}
80+
{{end}}{{if or .Runnable .HasSubCommands}}{{.UsageString}}{{end}}`
81+
5082
func init() {
83+
Cmd.SetUsageTemplate(usageTemplate)
84+
Cmd.SetHelpTemplate(helpTemplate)
85+
Cmd.Example = " account\n addon\n cluster\n role_binding\n sku_rule\n subscription"
5186
fs := Cmd.Flags()
5287
arguments.AddParameterFlag(fs, &args.parameter)
5388
arguments.AddHeaderFlag(fs, &args.header)
@@ -61,13 +96,13 @@ func init() {
6196
func run(cmd *cobra.Command, argv []string) error {
6297
path, err := urls.Expand(argv)
6398
if err != nil {
64-
return fmt.Errorf("Could not create URI: %v", err)
99+
return fmt.Errorf("could not create URI: %w", err)
65100
}
66101

67102
// Create the client for the OCM API:
68103
connection, err := ocm.NewConnection().Build()
69104
if err != nil {
70-
return fmt.Errorf("Failed to create OCM connection: %v", err)
105+
return fmt.Errorf("failed to create OCM connection: %w", err)
71106
}
72107
defer connection.Close()
73108

@@ -84,7 +119,7 @@ func run(cmd *cobra.Command, argv []string) error {
84119
// Send the request:
85120
response, err := request.Send()
86121
if err != nil {
87-
return fmt.Errorf("Can't send request: %v", err)
122+
return fmt.Errorf("can't send request: %w", err)
88123
}
89124
status := response.Status()
90125
body := response.Bytes()
@@ -94,26 +129,26 @@ func run(cmd *cobra.Command, argv []string) error {
94129
err = dump.Pretty(os.Stderr, body)
95130
}
96131
if err != nil {
97-
return fmt.Errorf("Can't print body: %v", err)
132+
return fmt.Errorf("can't print body: %w", err)
98133
}
99134

100135
// Load the configuration file:
101136
cfg, err := config.Load()
102137
if err != nil {
103-
return fmt.Errorf("Can't load config file: %v", err)
138+
return fmt.Errorf("can't load config file: %w", err)
104139
}
105140
if cfg == nil {
106-
return fmt.Errorf("Not logged in, run the 'login' command")
141+
return fmt.Errorf("not logged in, run the 'login' command")
107142
}
108143

109144
// Save the configuration:
110145
cfg.AccessToken, cfg.RefreshToken, err = connection.Tokens()
111146
if err != nil {
112-
return fmt.Errorf("Can't get tokens: %v", err)
147+
return fmt.Errorf("can't get tokens: %w", err)
113148
}
114149
err = config.Save(cfg)
115150
if err != nil {
116-
return fmt.Errorf("Can't save config file: %v", err)
151+
return fmt.Errorf("can't save config file: %w", err)
117152
}
118153

119154
// Bye:

0 commit comments

Comments
 (0)