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
This repository was archived by the owner on Aug 30, 2024. It is now read-only.
/coder-v1-cliPublic archive

Commite516b8c

Browse files
authored
chore: cleanup error output for "coder sh" (#214)
1 parented09747 commite516b8c

File tree

2 files changed

+42
-13
lines changed

2 files changed

+42
-13
lines changed

‎internal/cmd/ceapi.go

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,29 +58,37 @@ func getEnvs(ctx context.Context, client *coder.Client, email string) ([]coder.E
5858
returnallEnvs,nil
5959
}
6060

61-
// findEnv returns a single environment by name (if it exists.).
62-
funcfindEnv(ctx context.Context,client*coder.Client,envName,userEmailstring) (*coder.Environment,error) {
61+
// searchForEnv searches a user's environments to find the specified envName. If none is found, the haystack of
62+
// environment names is returned.
63+
funcsearchForEnv(ctx context.Context,client*coder.Client,envName,userEmailstring) (_*coder.Environment,haystack []string,_error) {
6364
envs,err:=getEnvs(ctx,client,userEmail)
6465
iferr!=nil {
65-
returnnil,xerrors.Errorf("get environments: %w",err)
66+
returnnil,nil,xerrors.Errorf("get environments: %w",err)
6667
}
6768

6869
// NOTE: We don't know in advance where we will find the env, so we can't pre-alloc.
69-
varfound []string
7070
for_,env:=rangeenvs {
7171
ifenv.Name==envName {
72-
return&env,nil
72+
return&env,nil,nil
7373
}
7474
// Keep track of what we found for the logs.
75-
found=append(found,env.Name)
75+
haystack=append(haystack,env.Name)
7676
}
77+
returnnil,haystack,coder.ErrNotFound
78+
}
7779

78-
returnnil,clog.Fatal(
79-
"failed to find environment",
80-
fmt.Sprintf("environment %q not found in %q",envName,found),
81-
clog.BlankLine,
82-
clog.Tipf("run\"coder envs ls\" to view your environments"),
83-
)
80+
// findEnv returns a single environment by name (if it exists.).
81+
funcfindEnv(ctx context.Context,client*coder.Client,envName,userEmailstring) (*coder.Environment,error) {
82+
env,haystack,err:=searchForEnv(ctx,client,envName,userEmail)
83+
iferr!=nil {
84+
returnnil,clog.Fatal(
85+
"failed to find environment",
86+
fmt.Sprintf("environment %q not found in %q",envName,haystack),
87+
clog.BlankLine,
88+
clog.Tipf("run\"coder envs ls\" to view your environments"),
89+
)
90+
}
91+
returnenv,nil
8492
}
8593

8694
typefindImgConfstruct {

‎internal/cmd/shell.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,33 @@ func getEnvsForCompletion(user string) func(cmd *cobra.Command, args []string, t
4141
}
4242
}
4343

44+
// special handling for the common case of "coder sh" input without a positional argument.
45+
funcshValidArgs(cmd*cobra.Command,args []string)error {
46+
ctx:=cmd.Context()
47+
iferr:=cobra.MinimumNArgs(1)(cmd,args);err!=nil {
48+
client,err:=newClient(ctx)
49+
iferr!=nil {
50+
returnclog.Error("missing [environment_name] argument")
51+
}
52+
_,haystack,err:=searchForEnv(ctx,client,"",coder.Me)
53+
iferr!=nil {
54+
returnclog.Error("missing [environment_name] argument",
55+
fmt.Sprintf("specify one of %q",haystack),
56+
clog.BlankLine,
57+
clog.Tipf("run\"coder envs ls\" to view your environments"),
58+
)
59+
}
60+
returnclog.Error("missing [environment_name] argument")
61+
}
62+
returnnil
63+
}
64+
4465
funcshCmd()*cobra.Command {
4566
return&cobra.Command{
4667
Use:"sh [environment_name] [<command [args...]>]",
4768
Short:"Open a shell and execute commands in a Coder environment",
4869
Long:"Execute a remote command on the environment\\nIf no command is specified, the default shell is opened.",
49-
Args:cobra.MinimumNArgs(1),
70+
Args:shValidArgs,
5071
DisableFlagParsing:true,
5172
ValidArgsFunction:getEnvsForCompletion(coder.Me),
5273
RunE:shell,

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp