@@ -84,33 +84,50 @@ func findEnv(ctx context.Context, client *coder.Client, envName, userEmail strin
84
84
}
85
85
86
86
func findImg (ctx context.Context , client * coder.Client , email , imgName , orgName string ) (* coder.Image , error ) {
87
- userImgs , err := lookupUserImgs (ctx , client , email , imgName , orgName )
87
+ switch {
88
+ case email == "" :
89
+ return nil , xerrors .New ("user email unset" )
90
+ case imgName == "" :
91
+ return nil , xerrors .New ("image name unset" )
92
+ }
93
+
94
+ imgs , err := getImgs (ctx , client , email , orgName )
88
95
if err != nil {
89
96
return nil , err
90
97
}
91
98
92
- numImgs := len ( userImgs )
99
+ var possibleMatches []coder. Image
93
100
94
- if numImgs == 0 {
95
- return nil , xerrors .New ("image not found - did you forget to import this image?" )
101
+ // The user may provide an image thats not an exact match
102
+ // to one of their imported images but they may be close.
103
+ // We can assist the user by collecting images that contain
104
+ // the user provided image flag value as a substring.
105
+ for _ , img := range imgs {
106
+ // If it's an exact match we can just return and exit.
107
+ if img .Repository == imgName {
108
+ return & img , nil
109
+ }
110
+ if strings .Contains (img .Repository , imgName ) {
111
+ possibleMatches = append (possibleMatches , img )
112
+ }
96
113
}
97
114
98
- if numImgs > 1 {
99
- lines := []string {clog .Tipf ("Did you mean?" )}
100
-
101
- for _ , img := range userImgs {
102
- lines = append (lines , img .Repository )
103
- }
115
+ if len (possibleMatches ) == 0 {
116
+ return nil , xerrors .New ("image not found" )
117
+ }
104
118
119
+ lines := []string {clog .Tipf ("Did you mean?" )}
105
120
106
- clog .Fatal (
107
- fmt .Sprintf ("Found %d possible matches for %q." , numImgs , imgName ),
108
- lines ... ,
109
- )
121
+ for _ , img := range possibleMatches {
122
+ lines = append (lines , img .Repository )
110
123
}
111
- return & userImgs [0 ], nil
124
+ return nil , clog .Fatal (
125
+ fmt .Sprintf ("Found %d possible matches for %q." , len (possibleMatches ), imgName ),
126
+ lines ... ,
127
+ )
112
128
}
113
129
130
+
114
131
func getImgs (ctx context.Context , client * coder.Client , email , orgName string ) ([]coder.Image , error ) {
115
132
u , err := client .UserByEmail (ctx , email )
116
133
if err != nil {
@@ -136,40 +153,6 @@ func getImgs(ctx context.Context, client *coder.Client, email, orgName string) (
136
153
return nil , xerrors .Errorf ("org name %q not found" , orgName )
137
154
}
138
155
139
- // returns all images that contain imgName as a substring and have been imported by the user.
140
- func lookupUserImgs (ctx context.Context , client * coder.Client , email , imgName , orgName string ) ([]coder.Image , error ) {
141
- switch {
142
- case email == "" :
143
- return nil , xerrors .New ("user email unset" )
144
- case imgName == "" :
145
- return nil , xerrors .New ("image name unset" )
146
- }
147
-
148
- imgs , err := getImgs (ctx , client , email , orgName )
149
- if err != nil {
150
- return nil , err
151
- }
152
-
153
- var userImgs []coder.Image
154
-
155
- // The user may provide an image thats not an exact match
156
- // to one of their imported images but they may be close.
157
- // We can assist the user by collecting images that contain
158
- // the user provided image flag value as a substring.
159
- for _ , img := range imgs {
160
- if strings .Contains (img .Repository , imgName ) {
161
- userImgs = append (userImgs , img )
162
- }
163
- // If it's an exact match we can overwrite the slice and exit the loop
164
- // since we won't need the fuzzy matched images anymore.
165
- if img .Repository == imgName {
166
- userImgs = []coder.Image {img }
167
- break
168
- }
169
- }
170
- return userImgs , nil
171
- }
172
-
173
156
func isMultiOrgMember (email string ) (* bool , error ) {
174
157
ctx , cancel := context .WithCancel (context .Background ())
175
158
defer cancel ()
0 commit comments