Skip to content

Commit 1fdf0d0

Browse files
committed
434 problems left...
* SO much
1 parent 0dbbb0c commit 1fdf0d0

File tree

104 files changed

+1141
-1060
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

104 files changed

+1141
-1060
lines changed

cli/agent.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func workspaceAgent() *clibase.Command {
4343
ctx, cancel := context.WithCancel(inv.Context())
4444
defer cancel()
4545

46-
rawURL, err := cmd.Flags().GetString(varAgentURL)
46+
rawURL, err := inv.ParsedFlags().GetString(varAgentURL)
4747
if err != nil {
4848
return xerrors.Errorf("CODER_AGENT_URL must be set: %w", err)
4949
}
@@ -129,7 +129,7 @@ func workspaceAgent() *clibase.Command {
129129
var exchangeToken func(context.Context) (agentsdk.AuthenticateResponse, error)
130130
switch auth {
131131
case "token":
132-
token, err := cmd.Flags().GetString(varAgentToken)
132+
token, err := inv.ParsedFlags().GetString(varAgentToken)
133133
if err != nil {
134134
return xerrors.Errorf("CODER_AGENT_TOKEN must be set for token auth: %w", err)
135135
}

cli/bigcli/env_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"github.com/coder/coder/cli/clibase"
88
)
99

10-
func TestFilterNamePrefix(t *testing.T) {
10+
func TestParseEnviron(t *testing.T) {
1111
t.Parallel()
1212
type args struct {
1313
environ []string
@@ -16,7 +16,7 @@ func TestFilterNamePrefix(t *testing.T) {
1616
tests := []struct {
1717
name string
1818
args args
19-
want []clibase.EnvVar
19+
want clibase.Environ
2020
}{
2121
{"empty", args{[]string{}, "SHIRE"}, nil},
2222
{
@@ -37,7 +37,7 @@ func TestFilterNamePrefix(t *testing.T) {
3737
t.Run(tt.name, func(t *testing.T) {
3838
t.Parallel()
3939
if got := clibase.ParseEnviron(tt.args.environ, tt.args.prefix); !reflect.DeepEqual(got, tt.want) {
40-
t.Errorf("EnvsWithPrefix() = %v, want %v", got, tt.want)
40+
t.Errorf("ParseEnviron() = %v, want %v", got, tt.want)
4141
}
4242
})
4343
}

cli/clibase/clibasetest/invokation.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,11 @@ func FakeIO(i *clibase.Invokation) *IO {
2525
i.Stdin = io.Stdin
2626
return io
2727
}
28+
29+
// Invoke creates a fake invokation and IO.
30+
func Invoke(cmd *clibase.Command, args ...string) (*clibase.Invokation, *IO) {
31+
i := clibase.Invokation{
32+
Args: args,
33+
}
34+
return &i, FakeIO(&i)
35+
}

cli/clibase/command.go

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ type Command struct {
1919

2020
// Use is provided in form "command [flags] [args...]".
2121
Use string
22+
23+
// Aliases is a list of alternative names for the command.
24+
Aliases []string
25+
2226
// Short is a one-line description of the command.
2327
Short string
2428
// Hidden determines whether the command should be hidden from help.
@@ -76,9 +80,10 @@ func (c *Command) FullUsage() string {
7680
type Invokation struct {
7781
parent *Invokation
7882

79-
ctx context.Context
80-
Command *Command
81-
Args []string
83+
ctx context.Context
84+
Command *Command
85+
parsedFlags *pflag.FlagSet
86+
Args []string
8287
// Environ is a list of environment variables. Use EnvsWithPrefix to parse
8388
// os.Environ.
8489
Environ Environ
@@ -91,6 +96,13 @@ func (i *Invokation) Context() context.Context {
9196
return i.ctx
9297
}
9398

99+
func (i *Invokation) ParsedFlags() *pflag.FlagSet {
100+
if i.parsedFlags == nil {
101+
panic("flags not parsed, has Run() been called?")
102+
}
103+
return i.parsedFlags
104+
}
105+
94106
// run recursively executes the command and its children.
95107
// allArgs is wired through the stack so that global flags can be accepted
96108
// anywhere in the command invokation.
@@ -102,10 +114,12 @@ func (i *Invokation) run(allArgs []string, flagSet *pflag.FlagSet) error {
102114

103115
childrenMap := make(map[string]*Command)
104116
for _, child := range i.Command.Children {
105-
if _, ok := childrenMap[child.Name()]; ok {
106-
return xerrors.Errorf("duplicate command name: %s", child.Name())
117+
for _, name := range append(child.Aliases, child.Name()) {
118+
if _, ok := childrenMap[name]; ok {
119+
return xerrors.Errorf("duplicate command name: %s", name)
120+
}
121+
childrenMap[name] = child
107122
}
108-
childrenMap[child.Name()] = child
109123
}
110124

111125
if flagSet == nil {
@@ -140,6 +154,7 @@ func (i *Invokation) run(allArgs []string, flagSet *pflag.FlagSet) error {
140154
if err != nil {
141155
return xerrors.Errorf("parsing flags: %w", err)
142156
}
157+
i.parsedFlags = flagSet
143158

144159
mw := i.Command.Middleware
145160
if mw == nil {
@@ -211,9 +226,15 @@ func Chain(ms ...MiddlewareFunc) MiddlewareFunc {
211226
}
212227

213228
func RequireNArgs(want int) MiddlewareFunc {
229+
if want < 0 {
230+
panic("want must be >= 0")
231+
}
214232
return func(next HandlerFunc) HandlerFunc {
215233
return func(i *Invokation) error {
216234
if len(i.Args) != want {
235+
if want == 0 {
236+
return xerrors.Errorf("wanted no args but got %v", len(i.Args))
237+
}
217238
return fmt.Errorf(
218239
"wanted %v args but got %v",
219240
want,

cli/clibase/command_test.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ func TestCommand_ToUpper(t *testing.T) {
3434
Middleware: clibase.Chain(
3535
clibase.RequireNArgs(1),
3636
),
37+
Aliases: []string{"up"},
3738
Options: clibase.OptionSet{
3839
clibase.Option{
3940
Name: "lower",
@@ -63,7 +64,7 @@ func TestCommand_ToUpper(t *testing.T) {
6364
}
6465
}
6566

66-
t.Run("OK", func(t *testing.T) {
67+
t.Run("SimpleOK", func(t *testing.T) {
6768
t.Parallel()
6869
i := &clibase.Invokation{
6970
Args: []string{"root", "toupper", "hello"},
@@ -74,6 +75,17 @@ func TestCommand_ToUpper(t *testing.T) {
7475
require.Equal(t, "HELLO", io.Stdout.String())
7576
})
7677

78+
t.Run("Alias", func(t *testing.T) {
79+
t.Parallel()
80+
i := &clibase.Invokation{
81+
Args: []string{"root", "up", "hello"},
82+
Command: cmd(),
83+
}
84+
io := clibasetest.FakeIO(i)
85+
i.Run()
86+
require.Equal(t, "HELLO", io.Stdout.String())
87+
})
88+
7789
t.Run("NoSubcommand", func(t *testing.T) {
7890
t.Parallel()
7991
i := &clibase.Invokation{
@@ -130,6 +142,20 @@ func TestCommand_ToUpper(t *testing.T) {
130142
require.NoError(t, i.Run())
131143
require.Equal(t, "hello!!!", io.Stdout.String())
132144
})
145+
146+
t.Run("ParsedFlags", func(t *testing.T) {
147+
t.Parallel()
148+
i := &clibase.Invokation{
149+
Args: []string{"root", "toupper", "--verbose", "hello", "--lower"},
150+
Command: cmd(),
151+
}
152+
_ = clibasetest.FakeIO(i)
153+
require.NoError(t, i.Run())
154+
require.Equal(t,
155+
"true",
156+
i.ParsedFlags().Lookup("verbose").Value.String(),
157+
)
158+
})
133159
}
134160

135161
func TestCommand_MiddlewareOrder(t *testing.T) {

cli/cliflag/cliflag.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,14 @@ import (
1818

1919
"github.com/spf13/pflag"
2020

21-
"github.com/coder/coder/cli/clibase"
2221
"github.com/coder/coder/cli/cliui"
2322
)
2423

2524
// IsSetBool returns the value of the boolean flag if it is set.
2625
// It returns false if the flag isn't set or if any error occurs attempting
2726
// to parse the value of the flag.
28-
func IsSetBool(cmd *clibase.Command, name string) bool {
29-
val, ok := IsSet(cmd, name)
27+
func IsSetBool(fs *pflag.FlagSet, name string) bool {
28+
val, ok := IsSet(fs, name)
3029
if !ok {
3130
return false
3231
}
@@ -36,12 +35,11 @@ func IsSetBool(cmd *clibase.Command, name string) bool {
3635
}
3736

3837
// IsSet returns the string value of the flag and whether it was set.
39-
func IsSet(cmd *clibase.Command, name string) (string, bool) {
40-
flag := cmd.Flag(name)
38+
func IsSet(fs *pflag.FlagSet, name string) (string, bool) {
39+
flag := fs.Lookup(name)
4140
if flag == nil {
4241
return "", false
4342
}
44-
4543
return flag.Value.String(), flag.Changed
4644
}
4745

0 commit comments

Comments
 (0)