Skip to content

chore: expose formatExamples to enterprise commands #13304

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

Merged
merged 1 commit into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions cli/configssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,12 +230,12 @@ func (r *RootCmd) configSSH() *serpent.Command {
Annotations: workspaceCommand,
Use: "config-ssh",
Short: "Add an SSH Host entry for your workspaces \"ssh coder.workspace\"",
Long: formatExamples(
example{
Long: FormatExamples(
Example{
Description: "You can use -o (or --ssh-option) so set SSH options to be used for all your workspaces",
Command: "coder config-ssh -o ForwardAgent=yes",
},
example{
Example{
Description: "You can use --dry-run (or -n) to see the changes that would be made",
Command: "coder config-ssh --dry-run",
},
Expand Down
4 changes: 2 additions & 2 deletions cli/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ func (r *RootCmd) create() *serpent.Command {
Annotations: workspaceCommand,
Use: "create [name]",
Short: "Create a workspace",
Long: formatExamples(
example{
Long: FormatExamples(
Example{
Description: "Create a workspace for another user (if you have permission)",
Command: "coder create <username>/<workspace_name>",
},
Expand Down
4 changes: 2 additions & 2 deletions cli/dotfiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ func (r *RootCmd) dotfiles() *serpent.Command {
Use: "dotfiles <git_repo_url>",
Middleware: serpent.RequireNArgs(1),
Short: "Personalize your workspace by applying a canonical dotfiles repository",
Long: formatExamples(
example{
Long: FormatExamples(
Example{
Description: "Check out and install a dotfiles repository without prompts",
Command: "coder dotfiles --yes git@github.com:example/dotfiles.git",
},
Expand Down
6 changes: 3 additions & 3 deletions cli/externalauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ func (r *RootCmd) externalAuthAccessToken() *serpent.Command {
Short: "Print auth for an external provider",
Long: "Print an access-token for an external auth provider. " +
"The access-token will be validated and sent to stdout with exit code 0. " +
"If a valid access-token cannot be obtained, the URL to authenticate will be sent to stdout with exit code 1\n" + formatExamples(
example{
"If a valid access-token cannot be obtained, the URL to authenticate will be sent to stdout with exit code 1\n" + FormatExamples(
Example{
Description: "Ensure that the user is authenticated with GitHub before cloning.",
Command: `#!/usr/bin/env sh

Expand All @@ -49,7 +49,7 @@ else
fi
`,
},
example{
Example{
Description: "Obtain an extra property of an access token for additional metadata.",
Command: "coder external-auth access-token slack --extra \"authed_user.id\"",
},
Expand Down
6 changes: 3 additions & 3 deletions cli/organization.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ func (r *RootCmd) switchOrganization() *serpent.Command {
cmd := &serpent.Command{
Use: "set <organization name | ID>",
Short: "set the organization used by the CLI. Pass an empty string to reset to the default organization.",
Long: "set the organization used by the CLI. Pass an empty string to reset to the default organization.\n" + formatExamples(
example{
Long: "set the organization used by the CLI. Pass an empty string to reset to the default organization.\n" + FormatExamples(
Example{
Description: "Remove the current organization and defer to the default.",
Command: "coder organizations set ''",
},
example{
Example{
Description: "Switch to a custom organization.",
Command: "coder organizations set my-org",
},
Expand Down
12 changes: 6 additions & 6 deletions cli/portforward.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,24 @@ func (r *RootCmd) portForward() *serpent.Command {
Use: "port-forward <workspace>",
Short: `Forward ports from a workspace to the local machine. For reverse port forwarding, use "coder ssh -R".`,
Aliases: []string{"tunnel"},
Long: formatExamples(
example{
Long: FormatExamples(
Example{
Description: "Port forward a single TCP port from 1234 in the workspace to port 5678 on your local machine",
Command: "coder port-forward <workspace> --tcp 5678:1234",
},
example{
Example{
Description: "Port forward a single UDP port from port 9000 to port 9000 on your local machine",
Command: "coder port-forward <workspace> --udp 9000",
},
example{
Example{
Description: "Port forward multiple TCP ports and a UDP port",
Command: "coder port-forward <workspace> --tcp 8080:8080 --tcp 9000:3000 --udp 5353:53",
},
example{
Example{
Description: "Port forward multiple ports (TCP or UDP) in condensed syntax",
Command: "coder port-forward <workspace> --tcp 8080,9000:3000,9090-9092,10000-10002:10010-10012",
},
example{
Example{
Description: "Port forward specifying the local address to bind to",
Command: "coder port-forward <workspace> --tcp 1.2.3.4:8080:8080",
},
Expand Down
16 changes: 8 additions & 8 deletions cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,12 +181,12 @@ func (r *RootCmd) Command(subcommands []*serpent.Command) (*serpent.Command, err
`
cmd := &serpent.Command{
Use: "coder [global-flags] <subcommand>",
Long: fmt.Sprintf(fmtLong, buildinfo.Version()) + formatExamples(
example{
Long: fmt.Sprintf(fmtLong, buildinfo.Version()) + FormatExamples(
Example{
Description: "Start a Coder server",
Command: "coder server",
},
example{
Example{
Description: "Get started by creating a template from an example",
Command: "coder templates init",
},
Expand Down Expand Up @@ -753,16 +753,16 @@ func isTTYWriter(inv *serpent.Invocation, writer io.Writer) bool {
return isatty.IsTerminal(file.Fd())
}

// example represents a standard example for command usage, to be used
// with formatExamples.
type example struct {
// 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
// FormatExamples formats the examples as width wrapped bulletpoint
// descriptions with the command underneath.
func formatExamples(examples ...example) string {
func FormatExamples(examples ...Example) string {
var sb strings.Builder

padStyle := cliui.DefaultStyles.Wrap.With(pretty.XPad(4, 0))
Expand Down
8 changes: 4 additions & 4 deletions cli/root_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func Test_formatExamples(t *testing.T) {

tests := []struct {
name string
examples []example
examples []Example
wantMatches []string
}{
{
Expand All @@ -55,7 +55,7 @@ func Test_formatExamples(t *testing.T) {
},
{
name: "Output examples",
examples: []example{
examples: []Example{
{
Description: "Hello world.",
Command: "echo hello",
Expand All @@ -72,7 +72,7 @@ func Test_formatExamples(t *testing.T) {
},
{
name: "No description outputs commands",
examples: []example{
examples: []Example{
{
Command: "echo hello",
},
Expand All @@ -87,7 +87,7 @@ func Test_formatExamples(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

got := formatExamples(tt.examples...)
got := FormatExamples(tt.examples...)
if len(tt.wantMatches) == 0 {
require.Empty(t, got)
} else {
Expand Down
12 changes: 6 additions & 6 deletions cli/schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ func (r *RootCmd) scheduleStart() *serpent.Command {
client := new(codersdk.Client)
cmd := &serpent.Command{
Use: "start <workspace-name> { <start-time> [day-of-week] [location] | manual }",
Long: scheduleStartDescriptionLong + "\n" + formatExamples(
example{
Long: scheduleStartDescriptionLong + "\n" + FormatExamples(
Example{
Description: "Set the workspace to start at 9:30am (in Dublin) from Monday to Friday",
Command: "coder schedule start my-workspace 9:30AM Mon-Fri Europe/Dublin",
},
Expand Down Expand Up @@ -189,8 +189,8 @@ func (r *RootCmd) scheduleStop() *serpent.Command {
client := new(codersdk.Client)
return &serpent.Command{
Use: "stop <workspace-name> { <duration> | manual }",
Long: scheduleStopDescriptionLong + "\n" + formatExamples(
example{
Long: scheduleStopDescriptionLong + "\n" + FormatExamples(
Example{
Command: "coder schedule stop my-workspace 2h30m",
},
),
Expand Down Expand Up @@ -234,8 +234,8 @@ func (r *RootCmd) scheduleOverride() *serpent.Command {
overrideCmd := &serpent.Command{
Use: "override-stop <workspace-name> <duration from now>",
Short: "Override the stop time of a currently running workspace instance.",
Long: scheduleOverrideDescriptionLong + "\n" + formatExamples(
example{
Long: scheduleOverrideDescriptionLong + "\n" + FormatExamples(
Example{
Command: "coder schedule override-stop my-workspace 90m",
},
),
Expand Down
6 changes: 3 additions & 3 deletions cli/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ func (r *RootCmd) templates() *serpent.Command {
cmd := &serpent.Command{
Use: "templates",
Short: "Manage templates",
Long: "Templates are written in standard Terraform and describe the infrastructure for workspaces\n" + formatExamples(
example{
Long: "Templates are written in standard Terraform and describe the infrastructure for workspaces\n" + FormatExamples(
Example{
Description: "Make changes to your template, and plan the changes",
Command: "coder templates plan my-template",
},
example{
Example{
Description: "Create or push an update to the template. Your developers can update their workspaces",
Command: "coder templates push my-template",
},
Expand Down
4 changes: 2 additions & 2 deletions cli/templateversions.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ func (r *RootCmd) templateVersions() *serpent.Command {
Use: "versions",
Short: "Manage different versions of the specified template",
Aliases: []string{"version"},
Long: formatExamples(
example{
Long: FormatExamples(
Example{
Description: "List versions of a specific template",
Command: "coder templates versions list my-template",
},
Expand Down
8 changes: 4 additions & 4 deletions cli/tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ func (r *RootCmd) tokens() *serpent.Command {
cmd := &serpent.Command{
Use: "tokens",
Short: "Manage personal access tokens",
Long: "Tokens are used to authenticate automated clients to Coder.\n" + formatExamples(
example{
Long: "Tokens are used to authenticate automated clients to Coder.\n" + FormatExamples(
Example{
Description: "Create a token for automation",
Command: "coder tokens create",
},
example{
Example{
Description: "List your tokens",
Command: "coder tokens ls",
},
example{
Example{
Description: "Remove a token by ID",
Command: "coder tokens rm WuoWs4ZsMX",
},
Expand Down
4 changes: 2 additions & 2 deletions cli/userlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ func (r *RootCmd) userSingle() *serpent.Command {
cmd := &serpent.Command{
Use: "show <username|user_id|'me'>",
Short: "Show a single user. Use 'me' to indicate the currently authenticated user.",
Long: formatExamples(
example{
Long: FormatExamples(
Example{
Command: "coder users show me",
},
),
Expand Down
4 changes: 2 additions & 2 deletions cli/userstatus.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ func (r *RootCmd) createUserStatusCommand(sdkStatus codersdk.UserStatus) *serpen
Use: fmt.Sprintf("%s <username|user_id>", verb),
Short: short,
Aliases: aliases,
Long: formatExamples(
example{
Long: FormatExamples(
Example{
Command: fmt.Sprintf("coder users %s example_user", verb),
},
),
Expand Down
Loading