From 17ac06d7133dc304139451669e40ffa7001e3166 Mon Sep 17 00:00:00 2001 From: Charlie Moog Date: Tue, 12 Jan 2021 12:30:21 -0600 Subject: [PATCH 1/2] chore: cleanup error output for "coder sh" --- internal/cmd/ceapi.go | 32 ++++++++++++++++++++------------ internal/cmd/shell.go | 23 ++++++++++++++++++++++- 2 files changed, 42 insertions(+), 13 deletions(-) diff --git a/internal/cmd/ceapi.go b/internal/cmd/ceapi.go index 8d155de1..32cd1f5d 100644 --- a/internal/cmd/ceapi.go +++ b/internal/cmd/ceapi.go @@ -58,29 +58,37 @@ func getEnvs(ctx context.Context, client *coder.Client, email string) ([]coder.E return allEnvs, nil } -// findEnv returns a single environment by name (if it exists.). -func findEnv(ctx context.Context, client *coder.Client, envName, userEmail string) (*coder.Environment, error) { +// searchForEnv searches a user's environments to find the specified envName. If none is found, the haystack of +// environment names is returned. +func searchForEnv(ctx context.Context, client *coder.Client, envName, userEmail string) (_ *coder.Environment, haystack []string, _ error) { envs, err := getEnvs(ctx, client, userEmail) if err != nil { - return nil, xerrors.Errorf("get environments: %w", err) + return nil, nil, xerrors.Errorf("get environments: %w", err) } // NOTE: We don't know in advance where we will find the env, so we can't pre-alloc. - var found []string for _, env := range envs { if env.Name == envName { - return &env, nil + return &env, nil, nil } // Keep track of what we found for the logs. - found = append(found, env.Name) + haystack = append(haystack, env.Name) } + return nil, haystack, coder.ErrNotFound +} - return nil, clog.Fatal( - "failed to find environment", - fmt.Sprintf("environment %q not found in %q", envName, found), - clog.BlankLine, - clog.Tipf("run \"coder envs ls\" to view your environments"), - ) +// findEnv returns a single environment by name (if it exists.). +func findEnv(ctx context.Context, client *coder.Client, envName, userEmail string) (*coder.Environment, error) { + env, haystack, err := searchForEnv(ctx, client, envName, userEmail) + if err != nil { + return nil, clog.Fatal( + "failed to find environment", + fmt.Sprintf("environment %q not found in %q", envName, haystack), + clog.BlankLine, + clog.Tipf("run \"coder envs ls\" to view your environments"), + ) + } + return env, nil } type findImgConf struct { diff --git a/internal/cmd/shell.go b/internal/cmd/shell.go index 1e448358..00cc5679 100644 --- a/internal/cmd/shell.go +++ b/internal/cmd/shell.go @@ -41,12 +41,33 @@ func getEnvsForCompletion(user string) func(cmd *cobra.Command, args []string, t } } +// special handling for he common case of "coder sh" input without a positional argument. +func shValidArgs(cmd *cobra.Command, args []string) error { + ctx := cmd.Context() + if err := cobra.MinimumNArgs(1)(cmd, args); err != nil { + client, err := newClient(ctx) + if err != nil { + return clog.Error("missing [environment_name] argument") + } + _, haystack, err := searchForEnv(ctx, client, "", coder.Me) + if err != nil { + return clog.Error("missing [environment_name] argument", + fmt.Sprintf("specify one of %q", haystack), + clog.BlankLine, + clog.Tipf("run \"coder envs ls\" to view your environments"), + ) + } + return clog.Error("missing [environment_name] argument") + } + return nil +} + func shCmd() *cobra.Command { return &cobra.Command{ Use: "sh [environment_name] []", Short: "Open a shell and execute commands in a Coder environment", Long: "Execute a remote command on the environment\\nIf no command is specified, the default shell is opened.", - Args: cobra.MinimumNArgs(1), + Args: shValidArgs, DisableFlagParsing: true, ValidArgsFunction: getEnvsForCompletion(coder.Me), RunE: shell, From 957b9c43bba771d05d025c3d0fd25bd66b815719 Mon Sep 17 00:00:00 2001 From: Charles Moog Date: Wed, 13 Jan 2021 11:33:34 -0600 Subject: [PATCH 2/2] Update internal/cmd/shell.go Co-authored-by: Faris Huskovic --- internal/cmd/shell.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/cmd/shell.go b/internal/cmd/shell.go index 00cc5679..26902be9 100644 --- a/internal/cmd/shell.go +++ b/internal/cmd/shell.go @@ -41,7 +41,7 @@ func getEnvsForCompletion(user string) func(cmd *cobra.Command, args []string, t } } -// special handling for he common case of "coder sh" input without a positional argument. +// special handling for the common case of "coder sh" input without a positional argument. func shValidArgs(cmd *cobra.Command, args []string) error { ctx := cmd.Context() if err := cobra.MinimumNArgs(1)(cmd, args); err != nil {