Skip to content

Commit c9d73d4

Browse files
committed
chore: Clean up annotation usage with template function
1 parent d25956f commit c9d73d4

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

cli/root.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ var (
2828
// Applied as annotations to workspace commands
2929
// so they display in a separated "help" section.
3030
workspaceCommand = map[string]string{
31-
"workspaces": " ",
31+
"workspaces": "",
3232
}
3333
)
3434

@@ -58,6 +58,20 @@ func init() {
5858
cobra.AddTemplateFunc("usageHeader", func(s string) string {
5959
return header.Render(s)
6060
})
61+
cobra.AddTemplateFunc("isWorkspaceCommand", isWorkspaceCommand)
62+
}
63+
64+
func isWorkspaceCommand(cmd *cobra.Command) bool {
65+
if _, ok := cmd.Annotations["workspaces"]; ok {
66+
return true
67+
}
68+
var ws bool
69+
cmd.VisitParents(func(cmd *cobra.Command) {
70+
if _, ok := cmd.Annotations["workspaces"]; ok {
71+
ws = true
72+
}
73+
})
74+
return ws
6175
}
6276

6377
func Root() *cobra.Command {
@@ -335,8 +349,8 @@ func usageTemplate() string {
335349
{{- if .HasAvailableSubCommands}}
336350
{{usageHeader "Commands:"}}
337351
{{- range .Commands}}
338-
{{- $hasRootAnnotations := (and $isRootHelp (gt (len .Annotations) 0))}}
339-
{{- if (or (and .IsAvailableCommand (not $hasRootAnnotations)) (eq .Name "help"))}}
352+
{{- $isRootWorkspaceCommand := (and $isRootHelp (isWorkspaceCommand .))}}
353+
{{- if (or (and .IsAvailableCommand (not $isRootWorkspaceCommand)) (eq .Name "help"))}}
340354
{{rpad .Name .NamePadding }} {{.Short}}
341355
{{- end}}
342356
{{- end}}
@@ -345,7 +359,7 @@ func usageTemplate() string {
345359
{{- if (and $isRootHelp .HasAvailableSubCommands)}}
346360
{{usageHeader "Workspace Commands:"}}
347361
{{- range .Commands}}
348-
{{- if (and .IsAvailableCommand (ne (index .Annotations "workspaces") ""))}}
362+
{{- if (and .IsAvailableCommand (isWorkspaceCommand .))}}
349363
{{rpad .Name .NamePadding }} {{.Short}}
350364
{{- end}}
351365
{{- end}}

0 commit comments

Comments
 (0)