Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Commit e516b8c

Browse files
authored
chore: cleanup error output for "coder sh" (#214)
1 parent ed09747 commit e516b8c

File tree

2 files changed

+42
-13
lines changed

2 files changed

+42
-13
lines changed

Diff for: internal/cmd/ceapi.go

+20-12
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
return allEnvs, nil
5959
}
6060

61-
// findEnv returns a single environment by name (if it exists.).
62-
func findEnv(ctx context.Context, client *coder.Client, envName, userEmail string) (*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+
func searchForEnv(ctx context.Context, client *coder.Client, envName, userEmail string) (_ *coder.Environment, haystack []string, _ error) {
6364
envs, err := getEnvs(ctx, client, userEmail)
6465
if err != nil {
65-
return nil, xerrors.Errorf("get environments: %w", err)
66+
return nil, 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-
var found []string
7070
for _, env := range envs {
7171
if env.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+
return nil, haystack, coder.ErrNotFound
78+
}
7779

78-
return nil, 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+
func findEnv(ctx context.Context, client *coder.Client, envName, userEmail string) (*coder.Environment, error) {
82+
env, haystack, err := searchForEnv(ctx, client, envName, userEmail)
83+
if err != nil {
84+
return nil, 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+
return env, nil
8492
}
8593

8694
type findImgConf struct {

Diff for: internal/cmd/shell.go

+22-1
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+
func shValidArgs(cmd *cobra.Command, args []string) error {
46+
ctx := cmd.Context()
47+
if err := cobra.MinimumNArgs(1)(cmd, args); err != nil {
48+
client, err := newClient(ctx)
49+
if err != nil {
50+
return clog.Error("missing [environment_name] argument")
51+
}
52+
_, haystack, err := searchForEnv(ctx, client, "", coder.Me)
53+
if err != nil {
54+
return clog.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+
return clog.Error("missing [environment_name] argument")
61+
}
62+
return nil
63+
}
64+
4465
func shCmd() *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)