Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita58d15e

Browse files
committed
434 problems left...
* SO much
1 parent7884f32 commita58d15e

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
defercancel()
4545

46-
rawURL,err:=cmd.Flags().GetString(varAgentURL)
46+
rawURL,err:=inv.ParsedFlags().GetString(varAgentURL)
4747
iferr!=nil {
4848
returnxerrors.Errorf("CODER_AGENT_URL must be set: %w",err)
4949
}
@@ -129,7 +129,7 @@ func workspaceAgent() *clibase.Command {
129129
varexchangeTokenfunc(context.Context) (agentsdk.AuthenticateResponse,error)
130130
switchauth {
131131
case"token":
132-
token,err:=cmd.Flags().GetString(varAgentToken)
132+
token,err:=inv.ParsedFlags().GetString(varAgentToken)
133133
iferr!=nil {
134134
returnxerrors.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-
funcTestFilterNamePrefix(t*testing.T) {
10+
funcTestParseEnviron(t*testing.T) {
1111
t.Parallel()
1212
typeargsstruct {
1313
environ []string
@@ -16,7 +16,7 @@ func TestFilterNamePrefix(t *testing.T) {
1616
tests:= []struct {
1717
namestring
1818
argsargs
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
ifgot:=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
returnio
2727
}
28+
29+
// Invoke creates a fake invokation and IO.
30+
funcInvoke(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
Usestring
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
Shortstring
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(fnfunc(*Command)) {
43+
func (c*Cmd)Walk(fnfunc(*Cmd)) {
4044
fn(c)
4145
for_,child:=rangec.Children {
4246
child.Walk(fn)
@@ -75,9 +79,10 @@ func (c *Cmd) FullUsage() string {
7579
typeInvokationstruct {
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
EnvironEnviron
@@ -90,6 +95,13 @@ func (i *Invokation) Context() context.Context {
9095
returni.ctx
9196
}
9297

98+
func (i*Invokation)ParsedFlags()*pflag.FlagSet {
99+
ifi.parsedFlags==nil {
100+
panic("flags not parsed, has Run() been called?")
101+
}
102+
returni.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:=rangei.Command.Children {
104-
if_,ok:=childrenMap[child.Name()];ok {
105-
returnxerrors.Errorf("duplicate command name: %s",child.Name())
116+
for_,name:=rangeappend(child.Aliases,child.Name()) {
117+
if_,ok:=childrenMap[name];ok {
118+
returnxerrors.Errorf("duplicate command name: %s",name)
119+
}
120+
childrenMap[name]=child
106121
}
107-
childrenMap[child.Name()]=child
108122
}
109123

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

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

212227
funcRequireNArgs(wantint)MiddlewareFunc {
228+
ifwant<0 {
229+
panic("want must be >= 0")
230+
}
213231
returnfunc(nextHandlerFunc)HandlerFunc {
214232
returnfunc(i*Invokation)error {
215233
iflen(i.Args)!=want {
234+
ifwant==0 {
235+
returnxerrors.Errorf("wanted no args but got %v",len(i.Args))
236+
}
216237
returnfmt.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
funcTestCommand_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-
funcIsSetBool(cmd*clibase.Command,namestring)bool {
29-
val,ok:=IsSet(cmd,name)
27+
funcIsSetBool(fs*pflag.FlagSet,namestring)bool {
28+
val,ok:=IsSet(fs,name)
3029
if!ok {
3130
returnfalse
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-
funcIsSet(cmd*clibase.Command,namestring) (string,bool) {
40-
flag:=cmd.Flag(name)
38+
funcIsSet(fs*pflag.FlagSet,namestring) (string,bool) {
39+
flag:=fs.Lookup(name)
4140
ifflag==nil {
4241
return"",false
4342
}
44-
4543
returnflag.Value.String(),flag.Changed
4644
}
4745

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp