Skip to content

Commit 9b057df

Browse files
committed
Don't throw error if there is an exact match (Fixes #55)
1 parent 20c404f commit 9b057df

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

cache.go

+40
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ func (c *ScalewayCache) LookUpImages(needle string, acceptUUID bool) []string {
138138
defer c.Lock.Unlock()
139139

140140
var res []string
141+
var exactMatches []string
141142

142143
if acceptUUID && uuid.Parse(needle) != nil {
143144
res = append(res, needle)
@@ -147,11 +148,18 @@ func (c *ScalewayCache) LookUpImages(needle string, acceptUUID bool) []string {
147148
// FIXME: if 'user/' is in needle, only watch for a user image
148149
nameRegex := regexp.MustCompile(`(?i)` + regexp.MustCompile(`[_-]`).ReplaceAllString(needle, ".*"))
149150
for identifier, name := range c.Images {
151+
if name == needle {
152+
exactMatches = append(exactMatches, identifier)
153+
}
150154
if strings.HasPrefix(identifier, needle) || nameRegex.MatchString(name) {
151155
res = append(res, identifier)
152156
}
153157
}
154158

159+
if len(exactMatches) == 1 {
160+
return exactMatches
161+
}
162+
155163
return RemoveDuplicates(res)
156164
}
157165

@@ -161,6 +169,7 @@ func (c *ScalewayCache) LookUpSnapshots(needle string, acceptUUID bool) []string
161169
defer c.Lock.Unlock()
162170

163171
var res []string
172+
var exactMatches []string
164173

165174
if acceptUUID && uuid.Parse(needle) != nil {
166175
res = append(res, needle)
@@ -169,11 +178,18 @@ func (c *ScalewayCache) LookUpSnapshots(needle string, acceptUUID bool) []string
169178
needle = regexp.MustCompile(`^user/`).ReplaceAllString(needle, "")
170179
nameRegex := regexp.MustCompile(`(?i)` + regexp.MustCompile(`[_-]`).ReplaceAllString(needle, ".*"))
171180
for identifier, name := range c.Snapshots {
181+
if name == needle {
182+
exactMatches = append(exactMatches, identifier)
183+
}
172184
if strings.HasPrefix(identifier, needle) || nameRegex.MatchString(name) {
173185
res = append(res, identifier)
174186
}
175187
}
176188

189+
if len(exactMatches) == 1 {
190+
return exactMatches
191+
}
192+
177193
return RemoveDuplicates(res)
178194
}
179195

@@ -183,18 +199,26 @@ func (c *ScalewayCache) LookUpVolumes(needle string, acceptUUID bool) []string {
183199
defer c.Lock.Unlock()
184200

185201
var res []string
202+
var exactMatches []string
186203

187204
if acceptUUID && uuid.Parse(needle) != nil {
188205
res = append(res, needle)
189206
}
190207

191208
nameRegex := regexp.MustCompile(`(?i)` + regexp.MustCompile(`[_-]`).ReplaceAllString(needle, ".*"))
192209
for identifier, name := range c.Volumes {
210+
if name == needle {
211+
exactMatches = append(exactMatches, identifier)
212+
}
193213
if strings.HasPrefix(identifier, needle) || nameRegex.MatchString(name) {
194214
res = append(res, identifier)
195215
}
196216
}
197217

218+
if len(exactMatches) == 1 {
219+
return exactMatches
220+
}
221+
198222
return RemoveDuplicates(res)
199223
}
200224

@@ -204,18 +228,26 @@ func (c *ScalewayCache) LookUpBootscripts(needle string, acceptUUID bool) []stri
204228
defer c.Lock.Unlock()
205229

206230
var res []string
231+
var exactMatches []string
207232

208233
if acceptUUID && uuid.Parse(needle) != nil {
209234
res = append(res, needle)
210235
}
211236

212237
nameRegex := regexp.MustCompile(`(?i)` + regexp.MustCompile(`[_-]`).ReplaceAllString(needle, ".*"))
213238
for identifier, name := range c.Bootscripts {
239+
if name == needle {
240+
exactMatches = append(exactMatches, identifier)
241+
}
214242
if strings.HasPrefix(identifier, needle) || nameRegex.MatchString(name) {
215243
res = append(res, identifier)
216244
}
217245
}
218246

247+
if len(exactMatches) == 1 {
248+
return exactMatches
249+
}
250+
219251
return RemoveDuplicates(res)
220252
}
221253

@@ -225,18 +257,26 @@ func (c *ScalewayCache) LookUpServers(needle string, acceptUUID bool) []string {
225257
defer c.Lock.Unlock()
226258

227259
var res []string
260+
var exactMatches []string
228261

229262
if acceptUUID && uuid.Parse(needle) != nil {
230263
res = append(res, needle)
231264
}
232265

233266
nameRegex := regexp.MustCompile(`(?i)` + regexp.MustCompile(`[_-]`).ReplaceAllString(needle, ".*"))
234267
for identifier, name := range c.Servers {
268+
if name == needle {
269+
exactMatches = append(exactMatches, identifier)
270+
}
235271
if strings.HasPrefix(identifier, needle) || nameRegex.MatchString(name) {
236272
res = append(res, identifier)
237273
}
238274
}
239275

276+
if len(exactMatches) == 1 {
277+
return exactMatches
278+
}
279+
240280
return RemoveDuplicates(res)
241281
}
242282

0 commit comments

Comments
 (0)