Skip to content

Commit 59facdd

Browse files
authored
fix: Show schedule commands in help, improve template (coder#2923)
* fix: Show schedule commands in help, improve template * chore: Remove schedule long help, fixed by listing missing commands * chore: Clean up annotation usage with template function * fix: Drive-by fix for trailing whitespace for flags Introduced in c768137.
1 parent 2d04880 commit 59facdd

File tree

2 files changed

+50
-36
lines changed

2 files changed

+50
-36
lines changed

cli/root.go

+35-12
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"os"
77
"strconv"
88
"strings"
9+
"text/template"
910
"time"
1011

1112
"golang.org/x/xerrors"
@@ -28,7 +29,7 @@ var (
2829
// Applied as annotations to workspace commands
2930
// so they display in a separated "help" section.
3031
workspaceCommand = map[string]string{
31-
"workspaces": " ",
32+
"workspaces": "",
3233
}
3334
)
3435

@@ -52,12 +53,8 @@ var (
5253
)
5354

5455
func init() {
55-
// Customizes the color of headings to make subcommands more visually
56-
// appealing.
57-
header := cliui.Styles.Placeholder
58-
cobra.AddTemplateFunc("usageHeader", func(s string) string {
59-
return header.Render(s)
60-
})
56+
// Set cobra template functions in init to avoid conflicts in tests.
57+
cobra.AddTemplateFuncs(templateFunctions)
6158
}
6259

6360
func Root() *cobra.Command {
@@ -311,6 +308,30 @@ func isTTYOut(cmd *cobra.Command) bool {
311308
return isatty.IsTerminal(file.Fd())
312309
}
313310

311+
var templateFunctions = template.FuncMap{
312+
"usageHeader": usageHeader,
313+
"isWorkspaceCommand": isWorkspaceCommand,
314+
}
315+
316+
func usageHeader(s string) string {
317+
// Customizes the color of headings to make subcommands more visually
318+
// appealing.
319+
return cliui.Styles.Placeholder.Render(s)
320+
}
321+
322+
func isWorkspaceCommand(cmd *cobra.Command) bool {
323+
if _, ok := cmd.Annotations["workspaces"]; ok {
324+
return true
325+
}
326+
var ws bool
327+
cmd.VisitParents(func(cmd *cobra.Command) {
328+
if _, ok := cmd.Annotations["workspaces"]; ok {
329+
ws = true
330+
}
331+
})
332+
return ws
333+
}
334+
314335
func usageTemplate() string {
315336
// usageHeader is defined in init().
316337
return `{{usageHeader "Usage:"}}
@@ -331,32 +352,34 @@ func usageTemplate() string {
331352
{{.Example}}
332353
{{end}}
333354
355+
{{- $isRootHelp := (not .HasParent)}}
334356
{{- if .HasAvailableSubCommands}}
335357
{{usageHeader "Commands:"}}
336358
{{- range .Commands}}
337-
{{- if (or (and .IsAvailableCommand (eq (len .Annotations) 0)) (eq .Name "help"))}}
359+
{{- $isRootWorkspaceCommand := (and $isRootHelp (isWorkspaceCommand .))}}
360+
{{- if (or (and .IsAvailableCommand (not $isRootWorkspaceCommand)) (eq .Name "help"))}}
338361
{{rpad .Name .NamePadding }} {{.Short}}
339362
{{- end}}
340363
{{- end}}
341364
{{end}}
342365
343-
{{- if and (not .HasParent) .HasAvailableSubCommands}}
366+
{{- if (and $isRootHelp .HasAvailableSubCommands)}}
344367
{{usageHeader "Workspace Commands:"}}
345368
{{- range .Commands}}
346-
{{- if (and .IsAvailableCommand (ne (index .Annotations "workspaces") ""))}}
369+
{{- if (and .IsAvailableCommand (isWorkspaceCommand .))}}
347370
{{rpad .Name .NamePadding }} {{.Short}}
348371
{{- end}}
349372
{{- end}}
350373
{{end}}
351374
352375
{{- if .HasAvailableLocalFlags}}
353376
{{usageHeader "Flags:"}}
354-
{{.LocalFlags.FlagUsagesWrapped 100}}
377+
{{.LocalFlags.FlagUsagesWrapped 100 | trimTrailingWhitespaces}}
355378
{{end}}
356379
357380
{{- if .HasAvailableInheritedFlags}}
358381
{{usageHeader "Global Flags:"}}
359-
{{.InheritedFlags.FlagUsagesWrapped 100}}
382+
{{.InheritedFlags.FlagUsagesWrapped 100 | trimTrailingWhitespaces}}
360383
{{end}}
361384
362385
{{- if .HasHelpSubCommands}}

cli/schedule.go

+15-24
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,6 @@ import (
1717
)
1818

1919
const (
20-
scheduleDescriptionLong = `Modify scheduled stop and start times for your workspace:
21-
* schedule show: show workspace schedule
22-
* schedule start: edit workspace start schedule
23-
* schedule stop: edit workspace stop schedule
24-
* schedule override-stop: edit stop time of active workspace
25-
`
2620
scheduleShowDescriptionLong = `Shows the following information for the given workspace:
2721
* The automatic start schedule
2822
* The next scheduled start time
@@ -64,24 +58,24 @@ func schedules() *cobra.Command {
6458
Annotations: workspaceCommand,
6559
Use: "schedule { show | start | stop | override } <workspace>",
6660
Short: "Modify scheduled stop and start times for your workspace",
67-
Long: scheduleDescriptionLong,
6861
}
6962

70-
scheduleCmd.AddCommand(scheduleShow())
71-
scheduleCmd.AddCommand(scheduleStart())
72-
scheduleCmd.AddCommand(scheduleStop())
73-
scheduleCmd.AddCommand(scheduleOverride())
63+
scheduleCmd.AddCommand(
64+
scheduleShow(),
65+
scheduleStart(),
66+
scheduleStop(),
67+
scheduleOverride(),
68+
)
7469

7570
return scheduleCmd
7671
}
7772

7873
func scheduleShow() *cobra.Command {
7974
showCmd := &cobra.Command{
80-
Annotations: workspaceCommand,
81-
Use: "show <workspace-name>",
82-
Short: "Show workspace schedule",
83-
Long: scheduleShowDescriptionLong,
84-
Args: cobra.ExactArgs(1),
75+
Use: "show <workspace-name>",
76+
Short: "Show workspace schedule",
77+
Long: scheduleShowDescriptionLong,
78+
Args: cobra.ExactArgs(1),
8579
RunE: func(cmd *cobra.Command, args []string) error {
8680
client, err := createClient(cmd)
8781
if err != nil {
@@ -101,8 +95,7 @@ func scheduleShow() *cobra.Command {
10195

10296
func scheduleStart() *cobra.Command {
10397
cmd := &cobra.Command{
104-
Annotations: workspaceCommand,
105-
Use: "start <workspace-name> { <start-time> [day-of-week] [location] | manual }",
98+
Use: "start <workspace-name> { <start-time> [day-of-week] [location] | manual }",
10699
Example: formatExamples(
107100
example{
108101
Description: "Set the workspace to start at 9:30am (in Dublin) from Monday to Friday",
@@ -153,9 +146,8 @@ func scheduleStart() *cobra.Command {
153146

154147
func scheduleStop() *cobra.Command {
155148
return &cobra.Command{
156-
Annotations: workspaceCommand,
157-
Args: cobra.ExactArgs(2),
158-
Use: "stop <workspace-name> { <duration> | manual }",
149+
Args: cobra.ExactArgs(2),
150+
Use: "stop <workspace-name> { <duration> | manual }",
159151
Example: formatExamples(
160152
example{
161153
Command: "coder schedule stop my-workspace 2h30m",
@@ -200,9 +192,8 @@ func scheduleStop() *cobra.Command {
200192

201193
func scheduleOverride() *cobra.Command {
202194
overrideCmd := &cobra.Command{
203-
Args: cobra.ExactArgs(2),
204-
Annotations: workspaceCommand,
205-
Use: "override-stop <workspace-name> <duration from now>",
195+
Args: cobra.ExactArgs(2),
196+
Use: "override-stop <workspace-name> <duration from now>",
206197
Example: formatExamples(
207198
example{
208199
Command: "coder schedule override-stop my-workspace 90m",

0 commit comments

Comments
 (0)