Skip to content

Commit

Permalink
Merge pull request moby#46871 from thaJeztah/less_cobra_customisation
Browse files Browse the repository at this point in the history
cmd/dockerd: remove various cobra templating
  • Loading branch information
thaJeztah authored Dec 1, 2023
2 parents d3533ee + 61d9485 commit bb99b3f
Showing 1 changed file with 2 additions and 59 deletions.
61 changes: 2 additions & 59 deletions cmd/dockerd/cobra.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ import (
// SetupRootCommand sets default usage, help, and error handling for the
// root command.
func SetupRootCommand(rootCmd *cobra.Command) {
cobra.AddTemplateFunc("hasSubCommands", hasSubCommands)
cobra.AddTemplateFunc("hasManagementSubCommands", hasManagementSubCommands)
cobra.AddTemplateFunc("operationSubCommands", operationSubCommands)
cobra.AddTemplateFunc("managementSubCommands", managementSubCommands)
cobra.AddTemplateFunc("wrappedFlagUsages", wrappedFlagUsages)

rootCmd.SetUsageTemplate(usageTemplate)
Expand Down Expand Up @@ -42,24 +38,6 @@ func FlagErrorFunc(cmd *cobra.Command, err error) error {
}
}

func hasSubCommands(cmd *cobra.Command) bool {
return len(operationSubCommands(cmd)) > 0
}

func hasManagementSubCommands(cmd *cobra.Command) bool {
return len(managementSubCommands(cmd)) > 0
}

func operationSubCommands(cmd *cobra.Command) []*cobra.Command {
var cmds []*cobra.Command
for _, sub := range cmd.Commands() {
if sub.IsAvailableCommand() && !sub.HasSubCommands() {
cmds = append(cmds, sub)
}
}
return cmds
}

func wrappedFlagUsages(cmd *cobra.Command) string {
width := 80
if ws, err := term.GetWinsize(0); err == nil {
Expand All @@ -68,20 +46,7 @@ func wrappedFlagUsages(cmd *cobra.Command) string {
return cmd.Flags().FlagUsagesWrapped(width - 1)
}

func managementSubCommands(cmd *cobra.Command) []*cobra.Command {
var cmds []*cobra.Command
for _, sub := range cmd.Commands() {
if sub.IsAvailableCommand() && sub.HasSubCommands() {
cmds = append(cmds, sub)
}
}
return cmds
}

var usageTemplate = `Usage:
{{- if not .HasSubCommands}} {{.UseLine}}{{end}}
{{- if .HasSubCommands}} {{ .CommandPath}} COMMAND{{end}}
const usageTemplate = `Usage: {{.UseLine}}
{{ .Short | trim }}
Expand All @@ -102,30 +67,8 @@ Examples:
Options:
{{ wrappedFlagUsages . | trimRightSpace}}
{{- end}}
{{- if hasManagementSubCommands . }}
Management Commands:
{{- range managementSubCommands . }}
{{rpad .Name .NamePadding }} {{.Short}}
{{- end}}
{{- end}}
{{- if hasSubCommands .}}
Commands:
{{- range operationSubCommands . }}
{{rpad .Name .NamePadding }} {{.Short}}
{{- end}}
{{- end}}
{{- if .HasSubCommands }}
Run '{{.CommandPath}} COMMAND --help' for more information on a command.
{{- end}}
`

var helpTemplate = `
const helpTemplate = `
{{if or .Runnable .HasSubCommands}}{{.UsageString}}{{end}}`

0 comments on commit bb99b3f

Please sign in to comment.