Skip to content

Commit 799ace7

Browse files
committed
425 problems remaining...
* Everything, everywhere is broken
1 parent e8959cf commit 799ace7

40 files changed

+211
-159
lines changed

cli/clibase/cmd.go

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -225,37 +225,53 @@ func Chain(ms ...MiddlewareFunc) MiddlewareFunc {
225225
}
226226

227227
func RequireNArgs(want int) MiddlewareFunc {
228-
if want < 0 {
229-
panic("want must be >= 0")
230-
}
231-
return func(next HandlerFunc) HandlerFunc {
232-
return func(i *Invokation) error {
233-
if len(i.Args) != want {
234-
if want == 0 {
235-
return xerrors.Errorf("wanted no args but got %v", len(i.Args))
236-
}
237-
return fmt.Errorf(
238-
"wanted %v args but got %v",
239-
want,
240-
len(i.Args),
241-
)
242-
}
243-
return next(i)
244-
}
245-
}
228+
return RequireRangeArgs(want, want)
246229
}
247230

231+
// RequireRangeArgs returns a Middleware that requires the number of arguments
232+
// to be between start and end (inclusive). If end is -1, then the number of
233+
// arguments must be at least start.
248234
func RequireRangeArgs(start, end int) MiddlewareFunc {
235+
if start < 0 {
236+
panic("start must be >= 0")
237+
}
249238
return func(next HandlerFunc) HandlerFunc {
250239
return func(i *Invokation) error {
251-
if len(i.Args) < start || len(i.Args) > end {
240+
got := len(i.Args)
241+
switch {
242+
case start == end && got != start:
243+
switch start {
244+
case 0:
245+
return xerrors.Errorf("wanted no args but got %v", got)
246+
default:
247+
return fmt.Errorf(
248+
"wanted %v args but got %v",
249+
start,
250+
got,
251+
)
252+
}
253+
case start > 0 && end == -1:
254+
switch {
255+
case got < start:
256+
return fmt.Errorf(
257+
"wanted at least %v args but got %v",
258+
start,
259+
got,
260+
)
261+
default:
262+
return next(i)
263+
}
264+
case start > end:
265+
panic("start must be <= end")
266+
case got < start || got > end:
252267
return fmt.Errorf(
253268
"wanted between %v and %v args but got %v",
254269
start, end,
255-
len(i.Args),
270+
got,
256271
)
272+
default:
273+
return next(i)
257274
}
258-
return next(i)
259275
}
260276
}
261277
}

cli/cliui/log.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ func Warn(wtr io.Writer, header string, lines ...string) {
4444
}.String())
4545
}
4646

47+
// Warn writes a formatted log to the writer provided.
48+
func Warnf(wtr io.Writer, fmtStr string, args ...interface{}) {
49+
Warn(wtr, fmt.Sprintf(fmtStr, args...))
50+
}
51+
4752
// Info writes a log to the writer provided.
4853
func Info(wtr io.Writer, header string, lines ...string) {
4954
_, _ = fmt.Fprint(wtr, cliMessage{
@@ -52,6 +57,11 @@ func Info(wtr io.Writer, header string, lines ...string) {
5257
}.String())
5358
}
5459

60+
// Infof writes a formatted log to the writer provided.
61+
func Infof(wtr io.Writer, fmtStr string, args ...interface{}) {
62+
Info(wtr, fmt.Sprintf(fmtStr, args...))
63+
}
64+
5565
// Error writes a log to the writer provided.
5666
func Error(wtr io.Writer, header string, lines ...string) {
5767
_, _ = fmt.Fprint(wtr, cliMessage{
@@ -61,3 +71,8 @@ func Error(wtr io.Writer, header string, lines ...string) {
6171
Lines: lines,
6272
}.String())
6373
}
74+
75+
// Errorf writes a formatted log to the writer provided.
76+
func Errorf(wtr io.Writer, fmtStr string, args ...interface{}) {
77+
Error(wtr, fmt.Sprintf(fmtStr, args...))
78+
}

cli/cliui/prompt.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ type PromptOptions struct {
2727

2828
const skipPromptFlag = "yes"
2929

30-
// AllowSkipPrompt adds a "yes" flag to the cmd that can be used to skip
30+
// SkipPromptOption adds a "yes" flag to the cmd that can be used to skip
3131
// prompts.
32-
func AllowSkipPrompt() clibase.Option {
32+
func SkipPromptOption() clibase.Option {
3333
return clibase.Option{
3434
Name: skipPromptFlag,
3535
Flag: skipPromptFlag,

cli/cliui/prompt_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func TestPrompt(t *testing.T) {
7979
Text: "ShouldNotSeeThis",
8080
IsConfirm: true,
8181
}, func(inv *clibase.Invokation) {
82-
inv.Command.Options = append(inv.Command.Options, cliui.AllowSkipPrompt())
82+
inv.Command.Options = append(inv.Command.Options, cliui.SkipPromptOption())
8383
inv.Args = []string{"-y"}
8484
})
8585
assert.NoError(t, err)

cli/configssh.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ func (r *RootCmd) configSSH() *clibase.Cmd {
325325
if dryRun {
326326
_, _ = fmt.Fprintf(out, "Dry run, the following changes would be made to your SSH configuration:\n\n * %s\n\n", strings.Join(changes, "\n * "))
327327

328-
color := isTTYOut(cmd)
328+
color := isTTYOut(inv.Stdout)
329329
diff, err := diffBytes(sshConfigFile, configRaw, configModified, color)
330330
if err != nil {
331331
return xerrors.Errorf("diff failed: %w", err)
@@ -409,7 +409,7 @@ func (r *RootCmd) configSSH() *clibase.Cmd {
409409
Description: "Specifies whether or not to keep options from previous run of config-ssh.",
410410
Value: clibase.BoolOf(&usePreviousOpts),
411411
},
412-
cliui.AllowSkipPrompt(),
412+
cliui.SkipPromptOption(),
413413
}
414414

415415
return cmd

cli/create.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ func (r *RootCmd) create() *clibase.Cmd {
196196
Description: "Specify a duration after which the workspace should shut down (e.g. 8h).",
197197
Value: clibase.DurationOf(&stopAfter),
198198
},
199-
cliui.AllowSkipPrompt(),
199+
cliui.SkipPromptOption(),
200200
)
201201

202202
return cmd

cli/delete.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,6 @@ func (r *RootCmd) deleteWorkspace() *clibase.Cmd {
6868
`Delete a workspace without deleting its resources. This can delete a
6969
workspace in a broken state, but may also lead to unaccounted cloud resources.`,
7070
)
71-
cliui.AllowSkipPrompt(inv)
71+
cliui.SkipPromptOption(inv)
7272
return cmd
7373
}

cli/dotfiles.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ func (r *RootCmd) dotfiles() *clibase.Cmd {
232232
return nil
233233
},
234234
}
235-
cliui.AllowSkipPrompt(inv)
235+
cliui.SkipPromptOption(inv)
236236
cliflag.StringVarP(cmd.Flags(), &symlinkDir, "symlink-dir", "", "CODER_SYMLINK_DIR", "", "Specifies the directory for the dotfiles symlink destinations. If empty will use $HOME.")
237237

238238
return cmd

cli/gitaskpass.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,24 +47,24 @@ func (r *RootCmd) gitAskpass() *clibase.Cmd {
4747
if errors.As(err, &apiError) && apiError.StatusCode() == http.StatusNotFound {
4848
// This prevents the "Run 'coder --help' for usage"
4949
// message from occurring.
50-
cmd.Printf("%s\n", apiError.Message)
50+
cliui.Errorf(inv.Stderr, "%s\n", apiError.Message))
5151
return cliui.Canceled
5252
}
5353
return xerrors.Errorf("get git token: %w", err)
5454
}
5555
if token.URL != "" {
5656
if err := openURL(cmd, token.URL); err == nil {
57-
cmd.Printf("Your browser has been opened to authenticate with Git:\n\n\t%s\n\n", token.URL)
57+
cliui.Infof(inv.Stdout, "Your browser has been opened to authenticate with Git:\n\n\t%s\n\n", token.URL))
5858
} else {
59-
cmd.Printf("Open the following URL to authenticate with Git:\n\n\t%s\n\n", token.URL)
59+
cliui.Infof(inv.Stdout, "Open the following URL to authenticate with Git:\n\n\t%s\n\n", token.URL))
6060
}
6161

6262
for r := retry.New(250*time.Millisecond, 10*time.Second); r.Wait(ctx); {
6363
token, err = client.GitAuth(ctx, host, true)
6464
if err != nil {
6565
continue
6666
}
67-
cmd.Printf("You've been authenticated with Git!\n")
67+
cliui.Infof(inv.Stdout, "You've been authenticated with Git!\n"))
6868
break
6969
}
7070
}

cli/logout.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,6 @@ func (r *RootCmd) logout() *clibase.Cmd {
6868
},
6969
}
7070

71-
cliui.AllowSkipPrompt(inv)
71+
cliui.SkipPromptOption(inv)
7272
return cmd
7373
}

cli/parameter.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,20 @@ func createParameterMapFromFile(parameterFile string) (map[string]string, error)
3636

3737
// Returns a parameter value from a given map, if the map does not exist or does not contain the item, it takes input from the user.
3838
// Throws an error if there are any errors with the users input.
39-
func getParameterValueFromMapOrInput(cmd *clibase.Cmd, parameterMap map[string]string, parameterSchema codersdk.ParameterSchema) (string, error) {
39+
func getParameterValueFromMapOrInput(inv *clibase.Invokation, parameterMap map[string]string, parameterSchema codersdk.ParameterSchema) (string, error) {
4040
var parameterValue string
4141
var err error
4242
if parameterMap != nil {
4343
var ok bool
4444
parameterValue, ok = parameterMap[parameterSchema.Name]
4545
if !ok {
46-
parameterValue, err = cliui.ParameterSchema(cmd, parameterSchema)
46+
parameterValue, err = cliui.ParameterSchema(inv, parameterSchema)
4747
if err != nil {
4848
return "", err
4949
}
5050
}
5151
} else {
52-
parameterValue, err = cliui.ParameterSchema(cmd, parameterSchema)
52+
parameterValue, err = cliui.ParameterSchema(inv, parameterSchema)
5353
if err != nil {
5454
return "", err
5555
}

cli/parameters.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package cli
22

33
import (
44
"github.com/coder/coder/cli/clibase"
5-
"gvisor.dev/gvisor/runsc/cmd"
65
)
76

87
func (r *RootCmd) parameters() *clibase.Cmd {
@@ -22,7 +21,7 @@ func (r *RootCmd) parameters() *clibase.Cmd {
2221
Hidden: true,
2322
Aliases: []string{"params"},
2423
Handler: func(inv *clibase.Invokation) error {
25-
return cmd.Help()
24+
return inv.Command.HelpHandler(inv)
2625
},
2726
}
2827
cmd.AddCommand(

cli/portforward.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313

1414
"github.com/pion/udp"
1515
"golang.org/x/xerrors"
16-
"gvisor.dev/gvisor/runsc/cmd"
1716

1817
"github.com/coder/coder/agent"
1918
"github.com/coder/coder/cli/clibase"
@@ -63,7 +62,7 @@ func (r *RootCmd) portForward() *clibase.Cmd {
6362
return xerrors.Errorf("parse port-forward specs: %w", err)
6463
}
6564
if len(specs) == 0 {
66-
err = cmd.Help()
65+
err = inv.Command.HelpHandler(inv)
6766
if err != nil {
6867
return xerrors.Errorf("generate help output: %w", err)
6968
}

cli/publickey.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,17 @@ func (r *RootCmd) publickey() *clibase.Cmd {
4848
"Coder. All clones with SSH will be authenticated automatically 🪄.",
4949
))
5050
cmd.Println()
51-
cmd.Println(cliui.Styles.Code.Render(strings.TrimSpace(key.PublicKey)))
51+
cliui.Infof(inv.Stdout, cliui.Styles.Code.Render(strings.TrimSpace(key.PublicKey))+"\n")
5252
cmd.Println()
53-
cmd.Println("Add to GitHub and GitLab:")
54-
cmd.Println(cliui.Styles.Prompt.String() + "https://github.com/settings/ssh/new")
55-
cmd.Println(cliui.Styles.Prompt.String() + "https://gitlab.com/-/profile/keys")
53+
cliui.Infof(inv.Stdout, "Add to GitHub and GitLab:"+"\n")
54+
cliui.Infof(inv.Stdout, cliui.Styles.Prompt.String()+"https://github.com/settings/ssh/new"+"\n")
55+
cliui.Infof(inv.Stdout, cliui.Styles.Prompt.String()+"https://gitlab.com/-/profile/keys"+"\n")
5656

5757
return nil
5858
},
5959
}
6060
cmd.Flags().BoolVar(&reset, "reset", false, "Regenerate your public key. This will require updating the key on any services it's registered with.")
61-
cliui.AllowSkipPrompt(inv)
61+
cliui.SkipPromptOption(inv)
6262

6363
return cmd
6464
}

cli/rename.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func (r *RootCmd) rename() *clibase.Cmd {
5050
},
5151
}
5252

53-
cliui.AllowSkipPrompt(inv)
53+
cliui.SkipPromptOption(inv)
5454

5555
return cmd
5656
}

cli/restart.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ func (r *RootCmd) restart() *clibase.Cmd {
1919
clibase.RequireNArgs(1),
2020
r.useClient(client),
2121
),
22+
Options: []clibase.Option{
23+
cliui.SkipPromptOption(),
24+
},
2225
Handler: func(inv *clibase.Invokation) error {
2326
ctx := inv.Context()
2427
out := inv.Stdout
@@ -62,6 +65,5 @@ func (r *RootCmd) restart() *clibase.Cmd {
6265
return nil
6366
},
6467
}
65-
cliui.AllowSkipPrompt(inv)
6668
return cmd
6769
}

cli/scaletest.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func (r *RootCmd) scaletest() *clibase.Cmd {
3939
Short: "Run a scale test against the Coder API",
4040
Long: "Perform scale tests against the Coder server.",
4141
Handler: func(inv *clibase.Invokation) error {
42-
return cmd.Help()
42+
return inv.Command.HelpHandler(inv)
4343
},
4444
}
4545

@@ -368,7 +368,7 @@ func (r *RootCmd) scaletestCleanup() *clibase.Cmd {
368368

369369
cmd.PrintErrf("Found %d scaletest workspaces\n", len(workspaces))
370370
if len(workspaces) != 0 {
371-
cmd.Println("Deleting scaletest workspaces...")
371+
cliui.Infof(inv.Stdout, "Deleting scaletest workspaces..."+"\n")
372372
harness := harness.NewTestHarness(cleanupStrategy.toStrategy(), harness.ConcurrentExecutionStrategy{})
373373

374374
for i, w := range workspaces {
@@ -384,7 +384,7 @@ func (r *RootCmd) scaletestCleanup() *clibase.Cmd {
384384
return xerrors.Errorf("run test harness to delete workspaces (harness failure, not a test failure): %w", err)
385385
}
386386

387-
cmd.Println("Done deleting scaletest workspaces:")
387+
cliui.Infof(inv.Stdout, "Done deleting scaletest workspaces:"+"\n")
388388
res := harness.Results()
389389
res.PrintText(inv.Stderr)
390390

@@ -425,7 +425,7 @@ func (r *RootCmd) scaletestCleanup() *clibase.Cmd {
425425

426426
cmd.PrintErrf("Found %d scaletest users\n", len(users))
427427
if len(workspaces) != 0 {
428-
cmd.Println("Deleting scaletest users...")
428+
cliui.Infof(inv.Stdout, "Deleting scaletest users..."+"\n")
429429
harness := harness.NewTestHarness(cleanupStrategy.toStrategy(), harness.ConcurrentExecutionStrategy{})
430430

431431
for i, u := range users {
@@ -444,7 +444,7 @@ func (r *RootCmd) scaletestCleanup() *clibase.Cmd {
444444
return xerrors.Errorf("run test harness to delete users (harness failure, not a test failure): %w", err)
445445
}
446446

447-
cmd.Println("Done deleting scaletest users:")
447+
cliui.Infof(inv.Stdout, "Done deleting scaletest users:"+"\n")
448448
res := harness.Results()
449449
res.PrintText(inv.Stderr)
450450

cli/schedule.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77

88
"github.com/jedib0t/go-pretty/v6/table"
99
"golang.org/x/xerrors"
10-
"gvisor.dev/gvisor/runsc/cmd"
1110

1211
"github.com/coder/coder/cli/clibase"
1312
"github.com/coder/coder/cli/cliui"
@@ -60,7 +59,7 @@ func (r *RootCmd) schedules() *clibase.Cmd {
6059
Use: "schedule { show | start | stop | override } <workspace>",
6160
Short: "Schedule automated start and stop times for workspaces",
6261
Handler: func(inv *clibase.Invokation) error {
63-
return cmd.Help()
62+
return inv.Command.HelpHandler(inv)
6463
},
6564
}
6665

0 commit comments

Comments
 (0)