Skip to content

feat(cli): organize flags #6269

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

Closed
wants to merge 14 commits into from
4 changes: 4 additions & 0 deletions cli/deployment/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,7 @@ func newConfig() *codersdk.DeploymentConfig {
Usage: "Disables all rate limits. This is not recommended in production.",
Flag: "dangerous-disable-rate-limits",
Default: false,
Hidden: true,
},
API: &codersdk.DeploymentConfigField[int]{
Name: "API Rate Limit",
Expand All @@ -476,6 +477,7 @@ func newConfig() *codersdk.DeploymentConfig {
EnvOverride: "CODER_API_RATE_LIMIT",
Flag: "api-rate-limit",
Default: 512,
Hidden: true,
},
},
// DEPRECATED: use Experiments instead.
Expand Down Expand Up @@ -538,12 +540,14 @@ func newConfig() *codersdk.DeploymentConfig {
Usage: "Allow workspace apps that are not served from subdomains to be shared. Path-based app sharing is DISABLED by default for security purposes. Path-based apps can make requests to the Coder API and pose a security risk when the workspace serves malicious JavaScript. Path-based apps can be disabled entirely with --disable-path-apps for further security.",
Flag: "dangerous-allow-path-app-sharing",
Default: false,
Hidden: true,
},
AllowPathAppSiteOwnerAccess: &codersdk.DeploymentConfigField[bool]{
Name: "DANGEROUS: Allow Site Owners to Access Path Apps",
Usage: "Allow site-owners to access workspace apps from workspaces they do not own. Owners cannot access path-based apps they do not own by default. Path-based apps can make requests to the Coder API and pose a security risk when the workspace serves malicious JavaScript. Path-based apps can be disabled entirely with --disable-path-apps for further security.",
Flag: "dangerous-allow-path-app-site-owner-access",
Default: false,
Hidden: true,
},
},
DisablePathApps: &codersdk.DeploymentConfigField[bool]{
Expand Down
117 changes: 0 additions & 117 deletions cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"runtime"
"strings"
"syscall"
"text/template"
"time"

"golang.org/x/xerrors"
Expand Down Expand Up @@ -440,122 +439,6 @@ func isTTYWriter(cmd *cobra.Command, writer func() io.Writer) 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:"}}
{{- if .Runnable}}
{{.UseLine}}
{{end}}
{{- if .HasAvailableSubCommands}}
{{.CommandPath}} [command]
{{end}}

{{- if gt (len .Aliases) 0}}
{{usageHeader "Aliases:"}}
{{.NameAndAliases}}
{{end}}

{{- if .HasExample}}
{{usageHeader "Get Started:"}}
{{.Example}}
{{end}}

{{- $isRootHelp := (not .HasParent)}}
{{- if .HasAvailableSubCommands}}
{{usageHeader "Commands:"}}
{{- range .Commands}}
{{- $isRootWorkspaceCommand := (and $isRootHelp (isWorkspaceCommand .))}}
{{- if (or (and .IsAvailableCommand (not $isRootWorkspaceCommand)) (eq .Name "help"))}}
{{rpad .Name .NamePadding }} {{.Short}}
{{- end}}
{{- end}}
{{end}}

{{- if (and $isRootHelp .HasAvailableSubCommands)}}
{{usageHeader "Workspace Commands:"}}
{{- range .Commands}}
{{- if (and .IsAvailableCommand (isWorkspaceCommand .))}}
{{rpad .Name .NamePadding }} {{.Short}}
{{- end}}
{{- end}}
{{end}}

{{- if .HasAvailableLocalFlags}}
{{usageHeader "Flags:"}}
{{.LocalFlags.FlagUsagesWrapped 100 | trimTrailingWhitespaces}}
{{end}}

{{- if .HasAvailableInheritedFlags}}
{{usageHeader "Global Flags:"}}
{{.InheritedFlags.FlagUsagesWrapped 100 | trimTrailingWhitespaces}}
{{end}}

{{- if .HasHelpSubCommands}}
{{usageHeader "Additional help topics:"}}
{{- range .Commands}}
{{- if .IsAdditionalHelpTopicCommand}}
{{rpad .CommandPath .CommandPathPadding}} {{.Short}}
{{- end}}
{{- end}}
{{end}}

{{- if .HasAvailableSubCommands}}
Use "{{.CommandPath}} [command] --help" for more information about a command.
{{end}}`
}

// example represents a standard example for command usage, to be used
// with formatExamples.
type example struct {
Description string
Command string
}

// formatExamples formats the examples as width wrapped bulletpoint
// descriptions with the command underneath.
func formatExamples(examples ...example) string {
wrap := cliui.Styles.Wrap.Copy()
wrap.PaddingLeft(4)
var sb strings.Builder
for i, e := range examples {
if len(e.Description) > 0 {
_, _ = sb.WriteString(" - " + wrap.Render(e.Description + ":")[4:] + "\n\n ")
}
// We add 1 space here because `cliui.Styles.Code` adds an extra
// space. This makes the code block align at an even 2 or 6
// spaces for symmetry.
_, _ = sb.WriteString(" " + cliui.Styles.Code.Render(fmt.Sprintf("$ %s", e.Command)))
if i < len(examples)-1 {
_, _ = sb.WriteString("\n\n")
}
}
return sb.String()
}

// FormatCobraError colorizes and adds "--help" docs to cobra commands.
func FormatCobraError(err error, cmd *cobra.Command) string {
helpErrMsg := fmt.Sprintf("Run '%s --help' for usage.", cmd.CommandPath())
Expand Down
10 changes: 5 additions & 5 deletions cli/testdata/coder_--help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,19 @@ Workspace Commands:
stop Stop a workspace
update Update a workspace

Flags:
Auth Flags:
--token string Specify an authentication token. For security reasons setting
CODER_SESSION_TOKEN is preferred.
Consumes $CODER_SESSION_TOKEN
Other Flags:
--global-config coder Path to the global coder config directory.
Consumes $CODER_CONFIG_DIR (default "~/.config/coderv2")
--header stringArray HTTP headers added to all requests. Provide as "Key=Value".
Consumes $CODER_HEADER
-h, --help help for coder
--no-feature-warning Suppress warnings about unlicensed features.
Consumes $CODER_NO_FEATURE_WARNING
--no-version-warning Suppress warning when client and server versions do not match.
Consumes $CODER_NO_VERSION_WARNING
--token string Specify an authentication token. For security reasons setting
CODER_SESSION_TOKEN is preferred.
Consumes $CODER_SESSION_TOKEN
--url string URL to a deployment.
Consumes $CODER_URL
-v, --verbose Enable verbose output.
Expand Down
7 changes: 4 additions & 3 deletions cli/testdata/coder_agent_--help.golden
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
Usage:
coder agent [flags]

Flags:
Auth Flags:
--auth string Specify the authentication type to use for the agent.
Consumes $CODER_AGENT_AUTH (default "token")
-h, --help help for agent
Operability Flags:
--log-dir string Specify the location for the agent log files.
Consumes $CODER_AGENT_LOG_DIR (default "/tmp")
--no-reap Do not start a process reaper.
--pprof-address string The address to serve pprof.
Consumes $CODER_AGENT_PPROF_ADDRESS (default "127.0.0.1:6060")
Other Flags:
--no-reap Do not start a process reaper.

Global Flags:
--global-config coder Path to the global coder config directory.
Expand Down
1 change: 0 additions & 1 deletion cli/testdata/coder_config-ssh_--help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ Get Started:
Flags:
-n, --dry-run Perform a trial run with no changes made, showing a diff at
the end.
-h, --help help for config-ssh
--ssh-config-file string Specifies the path to an SSH config.
Consumes $CODER_SSH_CONFIG_FILE (default "~/.ssh/config")
-o, --ssh-option stringArray Specifies additional SSH options to embed in each host stanza.
Expand Down
1 change: 0 additions & 1 deletion cli/testdata/coder_create_--help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ Usage:
coder create [name] [flags]

Flags:
-h, --help help for create
--parameter-file string Specify a file path with parameter values.
Consumes $CODER_PARAMETER_FILE
--rich-parameter-file string Specify a file path with values for rich
Expand Down
1 change: 0 additions & 1 deletion cli/testdata/coder_delete_--help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ Aliases:
delete, rm

Flags:
-h, --help help for delete
--orphan Delete a workspace without deleting its resources. This can delete a
workspace in a broken state, but may also lead to unaccounted cloud resources.
-y, --yes Bypass prompts
Expand Down
1 change: 0 additions & 1 deletion cli/testdata/coder_dotfiles_--help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ Get Started:
$ coder dotfiles --yes git@github.com:example/dotfiles.git

Flags:
-h, --help help for dotfiles
--symlink-dir string Specifies the directory for the dotfiles symlink destinations. If
empty will use $HOME.
Consumes $CODER_SYMLINK_DIR
Expand Down
1 change: 0 additions & 1 deletion cli/testdata/coder_list_--help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ Flags:
template, status, last built, outdated, starts at, stops after
(default [workspace,template,status,last built,outdated,starts
at,stops after])
-h, --help help for list
-o, --output string Output format. Available formats: table, json (default "table")
--search string Search for a workspace with a query. (default "owner:me")

Expand Down
1 change: 0 additions & 1 deletion cli/testdata/coder_login_--help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ Flags:
--first-user-username string Specifies a username to use if creating the first user
for the deployment.
Consumes $CODER_FIRST_USER_USERNAME
-h, --help help for login

Global Flags:
--global-config coder Path to the global coder config directory.
Expand Down
1 change: 0 additions & 1 deletion cli/testdata/coder_logout_--help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ Usage:
coder logout [flags]

Flags:
-h, --help help for logout
-y, --yes Bypass prompts

Global Flags:
Expand Down
1 change: 0 additions & 1 deletion cli/testdata/coder_ping_--help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ Usage:
coder ping <workspace> [flags]

Flags:
-h, --help help for ping
-n, --num int Specifies the number of pings to perform. (default 10)
-t, --timeout duration Specifies how long to wait for a ping to complete. (default 5s)
-v, --verbose Enables verbose logging.
Expand Down
1 change: 0 additions & 1 deletion cli/testdata/coder_port-forward_--help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ Get Started:
$ coder port-forward <workspace> --tcp 8080,9000:3000,9090-9092,10000-10002:10010-10012

Flags:
-h, --help help for port-forward
-p, --tcp stringArray Forward TCP port(s) from the workspace to the local machine.
Consumes $CODER_PORT_FORWARD_TCP
--udp stringArray Forward UDP port(s) from the workspace to the local machine. The UDP
Expand Down
1 change: 0 additions & 1 deletion cli/testdata/coder_publickey_--help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ Aliases:
publickey, pubkey

Flags:
-h, --help help for publickey
--reset Regenerate your public key. This will require updating the key on any services
it's registered with.
-y, --yes Bypass prompts
Expand Down
1 change: 0 additions & 1 deletion cli/testdata/coder_rename_--help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ Usage:
coder rename <workspace> <new name> [flags]

Flags:
-h, --help help for rename
-y, --yes Bypass prompts

Global Flags:
Expand Down
1 change: 0 additions & 1 deletion cli/testdata/coder_reset-password_--help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ Usage:
coder reset-password <username> [flags]

Flags:
-h, --help help for reset-password
--postgres-url string URL of a PostgreSQL database to connect to.
Consumes $CODER_PG_CONNECTION_URL

Expand Down
1 change: 0 additions & 1 deletion cli/testdata/coder_restart_--help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ Usage:
coder restart <workspace> [flags]

Flags:
-h, --help help for restart
-y, --yes Bypass prompts

Global Flags:
Expand Down
3 changes: 1 addition & 2 deletions cli/testdata/coder_scaletest_--help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ Commands:
cleanup Cleanup any orphaned scaletest resources
create-workspaces Creates many workspaces and waits for them to be ready

Flags:
-h, --help help for scaletest


Global Flags:
--global-config coder Path to the global coder config directory.
Expand Down
1 change: 0 additions & 1 deletion cli/testdata/coder_scaletest_cleanup_--help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ Flags:
Consumes $CODER_LOADTEST_CLEANUP_JOB_TIMEOUT (default 5m0s)
--cleanup-timeout duration Timeout for the entire cleanup run. 0 means unlimited.
Consumes $CODER_LOADTEST_CLEANUP_TIMEOUT (default 30m0s)
-h, --help help for cleanup

Global Flags:
--global-config coder Path to the global coder config directory.
Expand Down
40 changes: 20 additions & 20 deletions cli/testdata/coder_scaletest_create-workspaces_--help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,26 @@ It is recommended that all rate limits are disabled on the server before running
Usage:
coder scaletest create-workspaces [flags]

Flags:
Operability Flags:
--trace Whether application tracing data is collected. It
exports to a backend configured by environment
variables. See:
https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/protocol/exporter.md.
Consumes $CODER_LOADTEST_TRACE
--trace-coder Whether opentelemetry traces are sent to Coder.
We recommend keeping this disabled unless we
advise you to enable it.
Consumes $CODER_LOADTEST_TRACE_CODER
--trace-honeycomb-api-key string Enables trace exporting to Honeycomb.io using the
provided API key.
Consumes $CODER_LOADTEST_TRACE_HONEYCOMB_API_KEY
--trace-propagate Enables trace propagation to the Coder backend,
which will be used to correlate server-side spans
with client-side spans. Only enable this if the
server is configured with the exact same tracing
configuration as the client.
Consumes $CODER_LOADTEST_TRACE_PROPAGATE
Other Flags:
--cleanup-concurrency int Number of concurrent cleanup jobs to run. 0 means
unlimited.
Consumes $CODER_LOADTEST_CLEANUP_CONCURRENCY
Expand Down Expand Up @@ -38,7 +57,6 @@ Flags:
Consumes $CODER_LOADTEST_CONNECT_URL
-c, --count int Required: Number of workspaces to create.
Consumes $CODER_LOADTEST_COUNT (default 1)
-h, --help help for create-workspaces
--job-timeout duration Timeout per job. Jobs may take longer to complete
under higher concurrency limits.
Consumes $CODER_LOADTEST_JOB_TIMEOUT (default 5m0s)
Expand Down Expand Up @@ -94,24 +112,6 @@ Flags:
--timeout duration Timeout for the entire test run. 0 means
unlimited.
Consumes $CODER_LOADTEST_TIMEOUT (default 30m0s)
--trace Whether application tracing data is collected. It
exports to a backend configured by environment
variables. See:
https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/protocol/exporter.md.
Consumes $CODER_LOADTEST_TRACE
--trace-coder Whether opentelemetry traces are sent to Coder.
We recommend keeping this disabled unless we
advise you to enable it.
Consumes $CODER_LOADTEST_TRACE_CODER
--trace-honeycomb-api-key string Enables trace exporting to Honeycomb.io using the
provided API key.
Consumes $CODER_LOADTEST_TRACE_HONEYCOMB_API_KEY
--trace-propagate Enables trace propagation to the Coder backend,
which will be used to correlate server-side spans
with client-side spans. Only enable this if the
server is configured with the exact same tracing
configuration as the client.
Consumes $CODER_LOADTEST_TRACE_PROPAGATE

Global Flags:
--global-config coder Path to the global coder config directory.
Expand Down
3 changes: 1 addition & 2 deletions cli/testdata/coder_schedule_--help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ Commands:
start Edit workspace start schedule
stop Edit workspace stop schedule

Flags:
-h, --help help for schedule


Global Flags:
--global-config coder Path to the global coder config directory.
Expand Down
Loading