Skip to content

fix: Show schedule commands in help, improve template #2923

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 12, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
chore: Clean up annotation usage with template function
  • Loading branch information
mafredri committed Jul 12, 2022
commit c9d73d4b178692ba6f043c3bdfbf28dfc7683312
22 changes: 18 additions & 4 deletions cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ var (
// Applied as annotations to workspace commands
// so they display in a separated "help" section.
workspaceCommand = map[string]string{
"workspaces": " ",
"workspaces": "",
}
)

Expand Down Expand Up @@ -58,6 +58,20 @@ func init() {
cobra.AddTemplateFunc("usageHeader", func(s string) string {
return header.Render(s)
})
cobra.AddTemplateFunc("isWorkspaceCommand", isWorkspaceCommand)
}

func isWorkspaceCommand(cmd *cobra.Command) bool {
if _, ok := cmd.Annotations["workspaces"]; ok {
return true
}
var ws bool
cmd.VisitParents(func(cmd *cobra.Command) {
if _, ok := cmd.Annotations["workspaces"]; ok {
ws = true
}
})
return ws
}

func Root() *cobra.Command {
Expand Down Expand Up @@ -335,8 +349,8 @@ func usageTemplate() string {
{{- if .HasAvailableSubCommands}}
{{usageHeader "Commands:"}}
{{- range .Commands}}
{{- $hasRootAnnotations := (and $isRootHelp (gt (len .Annotations) 0))}}
{{- if (or (and .IsAvailableCommand (not $hasRootAnnotations)) (eq .Name "help"))}}
{{- $isRootWorkspaceCommand := (and $isRootHelp (isWorkspaceCommand .))}}
{{- if (or (and .IsAvailableCommand (not $isRootWorkspaceCommand)) (eq .Name "help"))}}
{{rpad .Name .NamePadding }} {{.Short}}
{{- end}}
{{- end}}
Expand All @@ -345,7 +359,7 @@ func usageTemplate() string {
{{- if (and $isRootHelp .HasAvailableSubCommands)}}
{{usageHeader "Workspace Commands:"}}
{{- range .Commands}}
{{- if (and .IsAvailableCommand (ne (index .Annotations "workspaces") ""))}}
{{- if (and .IsAvailableCommand (isWorkspaceCommand .))}}
{{rpad .Name .NamePadding }} {{.Short}}
{{- end}}
{{- end}}
Expand Down