From c798e201e194daa47938aae270127bced71a16c0 Mon Sep 17 00:00:00 2001 From: Mathias Fredriksson Date: Tue, 12 Jul 2022 16:17:56 +0300 Subject: [PATCH 1/6] fix: Show schedule commands in help, improve template --- cli/root.go | 6 ++++-- cli/schedule.go | 32 +++++++++++++++----------------- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/cli/root.go b/cli/root.go index 97e5e5620f674..dd227b6643b99 100644 --- a/cli/root.go +++ b/cli/root.go @@ -331,16 +331,18 @@ func usageTemplate() string { {{.Example}} {{end}} +{{- $isRootHelp := (not .HasParent)}} {{- if .HasAvailableSubCommands}} {{usageHeader "Commands:"}} {{- range .Commands}} - {{- if (or (and .IsAvailableCommand (eq (len .Annotations) 0)) (eq .Name "help"))}} + {{- $hasRootAnnotations := (and $isRootHelp (eq (len .Annotations) 0))}} + {{- if (or (and .IsAvailableCommand (not $hasRootAnnotations)) (eq .Name "help"))}} {{rpad .Name .NamePadding }} {{.Short}} {{- end}} {{- end}} {{end}} -{{- if and (not .HasParent) .HasAvailableSubCommands}} +{{- if (and $isRootHelp .HasAvailableSubCommands)}} {{usageHeader "Workspace Commands:"}} {{- range .Commands}} {{- if (and .IsAvailableCommand (ne (index .Annotations "workspaces") ""))}} diff --git a/cli/schedule.go b/cli/schedule.go index 1cd7cc65edca9..f02c13d30d902 100644 --- a/cli/schedule.go +++ b/cli/schedule.go @@ -67,21 +67,22 @@ func schedules() *cobra.Command { Long: scheduleDescriptionLong, } - scheduleCmd.AddCommand(scheduleShow()) - scheduleCmd.AddCommand(scheduleStart()) - scheduleCmd.AddCommand(scheduleStop()) - scheduleCmd.AddCommand(scheduleOverride()) + scheduleCmd.AddCommand( + scheduleShow(), + scheduleStart(), + scheduleStop(), + scheduleOverride(), + ) return scheduleCmd } func scheduleShow() *cobra.Command { showCmd := &cobra.Command{ - Annotations: workspaceCommand, - Use: "show ", - Short: "Show workspace schedule", - Long: scheduleShowDescriptionLong, - Args: cobra.ExactArgs(1), + Use: "show ", + Short: "Show workspace schedule", + Long: scheduleShowDescriptionLong, + Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { client, err := createClient(cmd) if err != nil { @@ -101,8 +102,7 @@ func scheduleShow() *cobra.Command { func scheduleStart() *cobra.Command { cmd := &cobra.Command{ - Annotations: workspaceCommand, - Use: "start { [day-of-week] [location] | manual }", + Use: "start { [day-of-week] [location] | manual }", Example: formatExamples( example{ Description: "Set the workspace to start at 9:30am (in Dublin) from Monday to Friday", @@ -153,9 +153,8 @@ func scheduleStart() *cobra.Command { func scheduleStop() *cobra.Command { return &cobra.Command{ - Annotations: workspaceCommand, - Args: cobra.ExactArgs(2), - Use: "stop { | manual }", + Args: cobra.ExactArgs(2), + Use: "stop { | manual }", Example: formatExamples( example{ Command: "coder schedule stop my-workspace 2h30m", @@ -200,9 +199,8 @@ func scheduleStop() *cobra.Command { func scheduleOverride() *cobra.Command { overrideCmd := &cobra.Command{ - Args: cobra.ExactArgs(2), - Annotations: workspaceCommand, - Use: "override-stop ", + Args: cobra.ExactArgs(2), + Use: "override-stop ", Example: formatExamples( example{ Command: "coder schedule override-stop my-workspace 90m", From 89895311e349c0e7e3476697388fb0cfa34bae7e Mon Sep 17 00:00:00 2001 From: Mathias Fredriksson Date: Tue, 12 Jul 2022 16:21:20 +0300 Subject: [PATCH 2/6] fix: eq -> gt --- cli/root.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/root.go b/cli/root.go index dd227b6643b99..8de7df1a47a00 100644 --- a/cli/root.go +++ b/cli/root.go @@ -335,7 +335,7 @@ func usageTemplate() string { {{- if .HasAvailableSubCommands}} {{usageHeader "Commands:"}} {{- range .Commands}} - {{- $hasRootAnnotations := (and $isRootHelp (eq (len .Annotations) 0))}} + {{- $hasRootAnnotations := (and $isRootHelp (gt (len .Annotations) 0))}} {{- if (or (and .IsAvailableCommand (not $hasRootAnnotations)) (eq .Name "help"))}} {{rpad .Name .NamePadding }} {{.Short}} {{- end}} From d25956f1a51750668925ffde14d475a11682ef42 Mon Sep 17 00:00:00 2001 From: Mathias Fredriksson Date: Tue, 12 Jul 2022 16:25:44 +0300 Subject: [PATCH 3/6] chore: Remove schedule long help, fixed by listing missing commands --- cli/schedule.go | 7 ------- 1 file changed, 7 deletions(-) diff --git a/cli/schedule.go b/cli/schedule.go index f02c13d30d902..b583915baf7f7 100644 --- a/cli/schedule.go +++ b/cli/schedule.go @@ -17,12 +17,6 @@ import ( ) const ( - scheduleDescriptionLong = `Modify scheduled stop and start times for your workspace: - * schedule show: show workspace schedule - * schedule start: edit workspace start schedule - * schedule stop: edit workspace stop schedule - * schedule override-stop: edit stop time of active workspace -` scheduleShowDescriptionLong = `Shows the following information for the given workspace: * The automatic start schedule * The next scheduled start time @@ -64,7 +58,6 @@ func schedules() *cobra.Command { Annotations: workspaceCommand, Use: "schedule { show | start | stop | override } ", Short: "Modify scheduled stop and start times for your workspace", - Long: scheduleDescriptionLong, } scheduleCmd.AddCommand( From c9d73d4b178692ba6f043c3bdfbf28dfc7683312 Mon Sep 17 00:00:00 2001 From: Mathias Fredriksson Date: Tue, 12 Jul 2022 16:42:44 +0300 Subject: [PATCH 4/6] chore: Clean up annotation usage with template function --- cli/root.go | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/cli/root.go b/cli/root.go index 8de7df1a47a00..b2959b4ecd57f 100644 --- a/cli/root.go +++ b/cli/root.go @@ -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": "", } ) @@ -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 { @@ -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}} @@ -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}} From 32951b34930f070db72f006683362e4f5009def9 Mon Sep 17 00:00:00 2001 From: Mathias Fredriksson Date: Tue, 12 Jul 2022 16:58:55 +0300 Subject: [PATCH 5/6] fix: Drive-by fix for trailing whitespace for flags Introduced in c7681370b5b863b9e07a4f6c20b8edb10f5c360a. --- cli/root.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cli/root.go b/cli/root.go index b2959b4ecd57f..21c8446de068f 100644 --- a/cli/root.go +++ b/cli/root.go @@ -367,12 +367,12 @@ func usageTemplate() string { {{- if .HasAvailableLocalFlags}} {{usageHeader "Flags:"}} -{{.LocalFlags.FlagUsagesWrapped 100}} +{{.LocalFlags.FlagUsagesWrapped 100 | trimTrailingWhitespaces}} {{end}} {{- if .HasAvailableInheritedFlags}} {{usageHeader "Global Flags:"}} -{{.InheritedFlags.FlagUsagesWrapped 100}} +{{.InheritedFlags.FlagUsagesWrapped 100 | trimTrailingWhitespaces}} {{end}} {{- if .HasHelpSubCommands}} From aaeb67e677c2ea5f5ab7c9346c56797eb93027bf Mon Sep 17 00:00:00 2001 From: Mathias Fredriksson Date: Tue, 12 Jul 2022 17:23:23 +0300 Subject: [PATCH 6/6] chore: Move template functions closer to usage --- cli/root.go | 47 +++++++++++++++++++++++++++-------------------- 1 file changed, 27 insertions(+), 20 deletions(-) diff --git a/cli/root.go b/cli/root.go index 21c8446de068f..a7c1b90ad751f 100644 --- a/cli/root.go +++ b/cli/root.go @@ -6,6 +6,7 @@ import ( "os" "strconv" "strings" + "text/template" "time" "golang.org/x/xerrors" @@ -52,26 +53,8 @@ var ( ) func init() { - // Customizes the color of headings to make subcommands more visually - // appealing. - header := cliui.Styles.Placeholder - 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 + // Set cobra template functions in init to avoid conflicts in tests. + cobra.AddTemplateFuncs(templateFunctions) } func Root() *cobra.Command { @@ -325,6 +308,30 @@ func isTTYOut(cmd *cobra.Command) bool { return isatty.IsTerminal(file.Fd()) } +var templateFunctions = template.FuncMap{ + "usageHeader": usageHeader, + "isWorkspaceCommand": isWorkspaceCommand, +} + +func usageHeader(s string) string { + // Customizes the color of headings to make subcommands more visually + // appealing. + return cliui.Styles.Placeholder.Render(s) +} + +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 usageTemplate() string { // usageHeader is defined in init(). return `{{usageHeader "Usage:"}}