Skip to content

Commit b618a54

Browse files
committed
434 problems left...
* SO much
1 parent de72650 commit b618a54

File tree

104 files changed

+1142
-1061
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

+1142
-1061
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/cmd.go

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ type Cmd struct {
1818
Children []*Cmd
1919
// Use is provided in form "command [flags] [args...]".
2020
Use string
21+
22+
// Aliases is a list of alternative names for the command.
23+
Aliases []string
24+
2125
// Short is a one-line description of the command.
2226
Short string
2327
// Hidden determines whether the command should be hidden from help.
@@ -36,7 +40,7 @@ type Cmd struct {
3640
}
3741

3842
// Walk calls fn for the command and all its children.
39-
func (c *Command) Walk(fn func(*Command)) {
43+
func (c *Cmd) Walk(fn func(*Cmd)) {
4044
fn(c)
4145
for _, child := range c.Children {
4246
child.Walk(fn)
@@ -75,9 +79,10 @@ func (c *Cmd) FullUsage() string {
7579
type Invokation struct {
7680
parent *Invokation
7781

78-
ctx context.Context
79-
Command *Cmd
80-
Args []string
82+
ctx context.Context
83+
Command *Cmd
84+
parsedFlags *pflag.FlagSet
85+
Args []string
8186
// Environ is a list of environment variables. Use EnvsWithPrefix to parse
8287
// os.Environ.
8388
Environ Environ
@@ -90,6 +95,13 @@ func (i *Invokation) Context() context.Context {
9095
return i.ctx
9196
}
9297

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

102114
childrenMap := make(map[string]*Cmd)
103115
for _, child := range i.Command.Children {
104-
if _, ok := childrenMap[child.Name()]; ok {
105-
return xerrors.Errorf("duplicate command name: %s", child.Name())
116+
for _, name := range append(child.Aliases, child.Name()) {
117+
if _, ok := childrenMap[name]; ok {
118+
return xerrors.Errorf("duplicate command name: %s", name)
119+
}
120+
childrenMap[name] = child
106121
}
107-
childrenMap[child.Name()] = child
108122
}
109123

110124
if flagSet == nil {
@@ -139,6 +153,7 @@ func (i *Invokation) run(allArgs []string, flagSet *pflag.FlagSet) error {
139153
if err != nil {
140154
return xerrors.Errorf("parsing flags: %w", err)
141155
}
156+
i.parsedFlags = flagSet
142157

143158
mw := i.Command.Middleware
144159
if mw == nil {
@@ -210,9 +225,15 @@ func Chain(ms ...MiddlewareFunc) MiddlewareFunc {
210225
}
211226

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

cli/clibase/cmd_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)