@@ -58,29 +58,37 @@ func getEnvs(ctx context.Context, client *coder.Client, email string) ([]coder.E
58
58
return allEnvs , nil
59
59
}
60
60
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 ) {
63
64
envs , err := getEnvs (ctx , client , userEmail )
64
65
if err != nil {
65
- return nil , xerrors .Errorf ("get environments: %w" , err )
66
+ return nil , nil , xerrors .Errorf ("get environments: %w" , err )
66
67
}
67
68
68
69
// NOTE: We don't know in advance where we will find the env, so we can't pre-alloc.
69
- var found []string
70
70
for _ , env := range envs {
71
71
if env .Name == envName {
72
- return & env , nil
72
+ return & env , nil , nil
73
73
}
74
74
// Keep track of what we found for the logs.
75
- found = append (found , env .Name )
75
+ haystack = append (haystack , env .Name )
76
76
}
77
+ return nil , haystack , coder .ErrNotFound
78
+ }
77
79
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
84
92
}
85
93
86
94
type findImgConf struct {
0 commit comments