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 2a44273 commit 21f0b47
Showing 1 changed file with 28 additions and 33 deletions.
61 changes: 28 additions & 33 deletions cmd/ocm/delete/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,42 +48,37 @@ var Cmd = &cobra.Command{
}

// for template format refer: https://pkg.go.dev/text/template
var (
helpTemplate = "{{with (or .Long .Short)}}{{. | trimTrailingWhitespaces}}" +
"\n" + "{{end}}{{if or .Runnable .HasSubCommands}}{{.UsageString}}{{end}}"
usageTemplate = fmt.Sprint("Usage:{{if .Runnable}}\n" +
" {{.UseLine}}{{end}}")
aliastemplate = fmt.Sprint("{{if gt (len .Aliases) 0}}Aliases:\n" +
" {{.NameAndAliases}}{{end}}")
exampletemplate = fmt.Sprint("{{if .HasExample}}Resource Alias:\n" +
" {{.Example}}{{end}}{{if .HasAvailableSubCommands}}{{$cmds := .Commands}}")
availableCommandTemplate = fmt.Sprint("{{if eq (len .Groups) 0}}Available Commands:{{range $cmds}}" +
"{{if (or .IsAvailableCommand (eq .Name \"help\"))}}\n" +
" {{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{else}}{{range $group := .Groups}}\n" +
"{{.Title}}{{range $cmds}}{{if (and (eq .GroupID $group.ID) (or .IsAvailableCommand (eq .Name \"help\")))}}\n" +
"{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}")
additionalCommandstemplate = fmt.Sprint("{{if not .AllChildCommandsHaveGroup}}Additional Commands:{{range $cmds}}" +
"{{if (and (eq .GroupID \"\") (or .IsAvailableCommand (eq .Name \"help\")))}}\n" +
"{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{end}}{{end}}")
flagsTemplate = fmt.Sprint("{{if .HasAvailableLocalFlags}}Flags:\n" +
"{{.LocalFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}")
globalFlagTemplate = fmt.Sprint("{{if .HasAvailableInheritedFlags}}Global Flags:\n" +
"{{.InheritedFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}")
additionalHelpTemplate = fmt.Sprint("{{if .HasHelpSubCommands}}Additional help topics:{{range .Commands}}" +
"{{if .IsAdditionalHelpTopicCommand}}\n" +
"{{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}")
footerTemplate = "{{if .HasAvailableSubCommands}}Use \"{{.CommandPath}} [command] --help\" " +
"for more information about a command.{{end}}"
endl = "\n\n"
usageString = fmt.Sprint(usageTemplate + aliastemplate + endl + exampletemplate + endl + availableCommandTemplate +
endl + additionalCommandstemplate + endl + flagsTemplate + endl + globalFlagTemplate + endl +
additionalHelpTemplate + endl + footerTemplate)
)
var usageTemplate = `
Usage:{{if .Runnable}}
{{.UseLine}}{{end}}{{if gt (len .Aliases) 0}}
Aliases:
{{.NameAndAliases}}{{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(usageString)
Cmd.SetUsageTemplate(usageTemplate)
Cmd.SetHelpTemplate(helpTemplate)
Cmd.Example = "account\n addon\n cluster\n role_binding\n sku_rule\n subscription"
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 Down

0 comments on commit 21f0b47

Please sign in to comment.