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
}

0 commit comments

Comments
 (0)