mirror of
https://github.com/coder/coder.git
synced 2025-07-13 21:36:50 +00:00
fix: Show schedule commands in help, improve template (#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 c7681370b5
.
This commit is contained in:
committed by
GitHub
parent
2d048803c8
commit
59facdd8dc
47
cli/root.go
47
cli/root.go
@ -6,6 +6,7 @@ import (
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"text/template"
|
||||
"time"
|
||||
|
||||
"golang.org/x/xerrors"
|
||||
@ -28,7 +29,7 @@ var (
|
||||
// Applied as annotations to workspace commands
|
||||
// so they display in a separated "help" section.
|
||||
workspaceCommand = map[string]string{
|
||||
"workspaces": " ",
|
||||
"workspaces": "",
|
||||
}
|
||||
)
|
||||
|
||||
@ -52,12 +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)
|
||||
})
|
||||
// Set cobra template functions in init to avoid conflicts in tests.
|
||||
cobra.AddTemplateFuncs(templateFunctions)
|
||||
}
|
||||
|
||||
func Root() *cobra.Command {
|
||||
@ -311,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:"}}
|
||||
@ -331,19 +352,21 @@ 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"))}}
|
||||
{{- $isRootWorkspaceCommand := (and $isRootHelp (isWorkspaceCommand .))}}
|
||||
{{- if (or (and .IsAvailableCommand (not $isRootWorkspaceCommand)) (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") ""))}}
|
||||
{{- if (and .IsAvailableCommand (isWorkspaceCommand .))}}
|
||||
{{rpad .Name .NamePadding }} {{.Short}}
|
||||
{{- end}}
|
||||
{{- end}}
|
||||
@ -351,12 +374,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}}
|
||||
|
@ -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,20 +58,20 @@ func schedules() *cobra.Command {
|
||||
Annotations: workspaceCommand,
|
||||
Use: "schedule { show | start | stop | override } <workspace>",
|
||||
Short: "Modify scheduled stop and start times for your workspace",
|
||||
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 <workspace-name>",
|
||||
Short: "Show workspace schedule",
|
||||
Long: scheduleShowDescriptionLong,
|
||||
@ -101,7 +95,6 @@ func scheduleShow() *cobra.Command {
|
||||
|
||||
func scheduleStart() *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Annotations: workspaceCommand,
|
||||
Use: "start <workspace-name> { <start-time> [day-of-week] [location] | manual }",
|
||||
Example: formatExamples(
|
||||
example{
|
||||
@ -153,7 +146,6 @@ func scheduleStart() *cobra.Command {
|
||||
|
||||
func scheduleStop() *cobra.Command {
|
||||
return &cobra.Command{
|
||||
Annotations: workspaceCommand,
|
||||
Args: cobra.ExactArgs(2),
|
||||
Use: "stop <workspace-name> { <duration> | manual }",
|
||||
Example: formatExamples(
|
||||
@ -201,7 +193,6 @@ func scheduleStop() *cobra.Command {
|
||||
func scheduleOverride() *cobra.Command {
|
||||
overrideCmd := &cobra.Command{
|
||||
Args: cobra.ExactArgs(2),
|
||||
Annotations: workspaceCommand,
|
||||
Use: "override-stop <workspace-name> <duration from now>",
|
||||
Example: formatExamples(
|
||||
example{
|
||||
|
Reference in New Issue
Block a user