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

Commit1fdf0d0

Browse files
committed
434 problems left...
* SO much
1 parent0dbbb0c commit1fdf0d0

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
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/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
Usestring
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
Shortstring
2428
// Hidden determines whether the command should be hidden from help.
@@ -76,9 +80,10 @@ func (c *Command) FullUsage() string {
7680
typeInvokationstruct {
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
EnvironEnviron
@@ -91,6 +96,13 @@ func (i *Invokation) Context() context.Context {
9196
returni.ctx
9297
}
9398

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

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

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

213228
funcRequireNArgs(wantint)MiddlewareFunc {
229+
ifwant<0 {
230+
panic("want must be >= 0")
231+
}
214232
returnfunc(nextHandlerFunc)HandlerFunc {
215233
returnfunc(i*Invokation)error {
216234
iflen(i.Args)!=want {
235+
ifwant==0 {
236+
returnxerrors.Errorf("wanted no args but got %v",len(i.Args))
237+
}
217238
returnfmt.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
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