Skip to content

Commit 2960e9d

Browse files
author
Quentin Perez
committed
cache.go: Prefix fields
1 parent 1a91a7d commit 2960e9d

File tree

2 files changed

+81
-81
lines changed

2 files changed

+81
-81
lines changed

pkg/api/cache.go

Lines changed: 71 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -19,35 +19,35 @@ import (
1919
"github.com/renstrom/fuzzysearch/fuzzy"
2020
)
2121

22-
const ( // cache fields
23-
// REGION permits to access at the region field
24-
REGION = iota
25-
// ARCH permits to access at the arch field
26-
ARCH
27-
// OWNER permits to access at the owner field
28-
OWNER
29-
// TITLE permits to access at the title field
30-
TITLE
31-
// MAXFIELD is used to determine the size of array
32-
MAXFIELD
22+
const (
23+
// CacheRegion permits to access at the region field
24+
CacheRegion = iota
25+
// CacheArch permits to access at the arch field
26+
CacheArch
27+
// CacheOwner permits to access at the owner field
28+
CacheOwner
29+
// CacheTitle permits to access at the title field
30+
CacheTitle
31+
// CacheMaxfield is used to determine the size of array
32+
CacheMaxfield
3333
)
3434

3535
// ScalewayCache is used not to query the API to resolve full identifiers
3636
type ScalewayCache struct {
3737
// Images contains names of Scaleway images indexed by identifier
38-
Images map[string][MAXFIELD]string `json:"images"`
38+
Images map[string][CacheMaxfield]string `json:"images"`
3939

4040
// Snapshots contains names of Scaleway snapshots indexed by identifier
41-
Snapshots map[string][MAXFIELD]string `json:"snapshots"`
41+
Snapshots map[string][CacheMaxfield]string `json:"snapshots"`
4242

4343
// Volumes contains names of Scaleway volumes indexed by identifier
44-
Volumes map[string][MAXFIELD]string `json:"volumes"`
44+
Volumes map[string][CacheMaxfield]string `json:"volumes"`
4545

4646
// Bootscripts contains names of Scaleway bootscripts indexed by identifier
47-
Bootscripts map[string][MAXFIELD]string `json:"bootscripts"`
47+
Bootscripts map[string][CacheMaxfield]string `json:"bootscripts"`
4848

4949
// Servers contains names of Scaleway C1 servers indexed by identifier
50-
Servers map[string][MAXFIELD]string `json:"servers"`
50+
Servers map[string][CacheMaxfield]string `json:"servers"`
5151

5252
// Path is the path to the cache file
5353
Path string `json:"-"`
@@ -166,11 +166,11 @@ func NewScalewayCache() (*ScalewayCache, error) {
166166
_, err := os.Stat(cachePath)
167167
if os.IsNotExist(err) {
168168
return &ScalewayCache{
169-
Images: make(map[string][MAXFIELD]string),
170-
Snapshots: make(map[string][MAXFIELD]string),
171-
Volumes: make(map[string][MAXFIELD]string),
172-
Bootscripts: make(map[string][MAXFIELD]string),
173-
Servers: make(map[string][MAXFIELD]string),
169+
Images: make(map[string][CacheMaxfield]string),
170+
Snapshots: make(map[string][CacheMaxfield]string),
171+
Volumes: make(map[string][CacheMaxfield]string),
172+
Bootscripts: make(map[string][CacheMaxfield]string),
173+
Servers: make(map[string][CacheMaxfield]string),
174174
Path: cachePath,
175175
}, nil
176176
} else if err != nil {
@@ -190,28 +190,28 @@ func NewScalewayCache() (*ScalewayCache, error) {
190190
return nil, err
191191
}
192192
return &ScalewayCache{
193-
Images: make(map[string][MAXFIELD]string),
194-
Snapshots: make(map[string][MAXFIELD]string),
195-
Volumes: make(map[string][MAXFIELD]string),
196-
Bootscripts: make(map[string][MAXFIELD]string),
197-
Servers: make(map[string][MAXFIELD]string),
193+
Images: make(map[string][CacheMaxfield]string),
194+
Snapshots: make(map[string][CacheMaxfield]string),
195+
Volumes: make(map[string][CacheMaxfield]string),
196+
Bootscripts: make(map[string][CacheMaxfield]string),
197+
Servers: make(map[string][CacheMaxfield]string),
198198
Path: cachePath,
199199
}, nil
200200
}
201201
if cache.Images == nil {
202-
cache.Images = make(map[string][MAXFIELD]string)
202+
cache.Images = make(map[string][CacheMaxfield]string)
203203
}
204204
if cache.Snapshots == nil {
205-
cache.Snapshots = make(map[string][MAXFIELD]string)
205+
cache.Snapshots = make(map[string][CacheMaxfield]string)
206206
}
207207
if cache.Volumes == nil {
208-
cache.Volumes = make(map[string][MAXFIELD]string)
208+
cache.Volumes = make(map[string][CacheMaxfield]string)
209209
}
210210
if cache.Servers == nil {
211-
cache.Servers = make(map[string][MAXFIELD]string)
211+
cache.Servers = make(map[string][CacheMaxfield]string)
212212
}
213213
if cache.Bootscripts == nil {
214-
cache.Bootscripts = make(map[string][MAXFIELD]string)
214+
cache.Bootscripts = make(map[string][CacheMaxfield]string)
215215
}
216216
return &cache, nil
217217
}
@@ -265,7 +265,7 @@ func (c *ScalewayCache) LookUpImages(needle string, acceptUUID bool) ScalewayRes
265265

266266
if acceptUUID && anonuuid.IsUUID(needle) == nil {
267267
if fields, ok := c.Images[needle]; ok {
268-
entry := NewScalewayResolverResult(needle, fields[TITLE], fields[ARCH], IdentifierImage)
268+
entry := NewScalewayResolverResult(needle, fields[CacheTitle], fields[CacheArch], IdentifierImage)
269269
entry.ComputeRankMatch(needle)
270270
res = append(res, entry)
271271
}
@@ -275,13 +275,13 @@ func (c *ScalewayCache) LookUpImages(needle string, acceptUUID bool) ScalewayRes
275275
// FIXME: if 'user/' is in needle, only watch for a user image
276276
nameRegex := regexp.MustCompile(`(?i)` + regexp.MustCompile(`[_-]`).ReplaceAllString(needle, ".*"))
277277
for identifier, fields := range c.Images {
278-
if fields[TITLE] == needle {
279-
entry := NewScalewayResolverResult(identifier, fields[TITLE], fields[ARCH], IdentifierImage)
278+
if fields[CacheTitle] == needle {
279+
entry := NewScalewayResolverResult(identifier, fields[CacheTitle], fields[CacheArch], IdentifierImage)
280280
entry.ComputeRankMatch(needle)
281281
exactMatches = append(exactMatches, entry)
282282
}
283-
if strings.HasPrefix(identifier, needle) || nameRegex.MatchString(fields[TITLE]) {
284-
entry := NewScalewayResolverResult(identifier, fields[TITLE], fields[ARCH], IdentifierImage)
283+
if strings.HasPrefix(identifier, needle) || nameRegex.MatchString(fields[CacheTitle]) {
284+
entry := NewScalewayResolverResult(identifier, fields[CacheTitle], fields[CacheArch], IdentifierImage)
285285
entry.ComputeRankMatch(needle)
286286
res = append(res, entry)
287287
}
@@ -304,7 +304,7 @@ func (c *ScalewayCache) LookUpSnapshots(needle string, acceptUUID bool) Scaleway
304304

305305
if acceptUUID && anonuuid.IsUUID(needle) == nil {
306306
if fields, ok := c.Snapshots[needle]; ok {
307-
entry := NewScalewayResolverResult(needle, fields[TITLE], fields[ARCH], IdentifierSnapshot)
307+
entry := NewScalewayResolverResult(needle, fields[CacheTitle], fields[CacheArch], IdentifierSnapshot)
308308
entry.ComputeRankMatch(needle)
309309
res = append(res, entry)
310310
}
@@ -313,13 +313,13 @@ func (c *ScalewayCache) LookUpSnapshots(needle string, acceptUUID bool) Scaleway
313313
needle = regexp.MustCompile(`^user/`).ReplaceAllString(needle, "")
314314
nameRegex := regexp.MustCompile(`(?i)` + regexp.MustCompile(`[_-]`).ReplaceAllString(needle, ".*"))
315315
for identifier, fields := range c.Snapshots {
316-
if fields[TITLE] == needle {
317-
entry := NewScalewayResolverResult(identifier, fields[TITLE], fields[ARCH], IdentifierSnapshot)
316+
if fields[CacheTitle] == needle {
317+
entry := NewScalewayResolverResult(identifier, fields[CacheTitle], fields[CacheArch], IdentifierSnapshot)
318318
entry.ComputeRankMatch(needle)
319319
exactMatches = append(exactMatches, entry)
320320
}
321-
if strings.HasPrefix(identifier, needle) || nameRegex.MatchString(fields[TITLE]) {
322-
entry := NewScalewayResolverResult(identifier, fields[TITLE], fields[ARCH], IdentifierSnapshot)
321+
if strings.HasPrefix(identifier, needle) || nameRegex.MatchString(fields[CacheTitle]) {
322+
entry := NewScalewayResolverResult(identifier, fields[CacheTitle], fields[CacheArch], IdentifierSnapshot)
323323
entry.ComputeRankMatch(needle)
324324
res = append(res, entry)
325325
}
@@ -342,21 +342,21 @@ func (c *ScalewayCache) LookUpVolumes(needle string, acceptUUID bool) ScalewayRe
342342

343343
if acceptUUID && anonuuid.IsUUID(needle) == nil {
344344
if fields, ok := c.Volumes[needle]; ok {
345-
entry := NewScalewayResolverResult(needle, fields[TITLE], fields[ARCH], IdentifierVolume)
345+
entry := NewScalewayResolverResult(needle, fields[CacheTitle], fields[CacheArch], IdentifierVolume)
346346
entry.ComputeRankMatch(needle)
347347
res = append(res, entry)
348348
}
349349
}
350350

351351
nameRegex := regexp.MustCompile(`(?i)` + regexp.MustCompile(`[_-]`).ReplaceAllString(needle, ".*"))
352352
for identifier, fields := range c.Volumes {
353-
if fields[TITLE] == needle {
354-
entry := NewScalewayResolverResult(needle, fields[TITLE], fields[ARCH], IdentifierVolume)
353+
if fields[CacheTitle] == needle {
354+
entry := NewScalewayResolverResult(needle, fields[CacheTitle], fields[CacheArch], IdentifierVolume)
355355
entry.ComputeRankMatch(needle)
356356
exactMatches = append(exactMatches, entry)
357357
}
358-
if strings.HasPrefix(identifier, needle) || nameRegex.MatchString(fields[TITLE]) {
359-
entry := NewScalewayResolverResult(needle, fields[TITLE], fields[ARCH], IdentifierVolume)
358+
if strings.HasPrefix(identifier, needle) || nameRegex.MatchString(fields[CacheTitle]) {
359+
entry := NewScalewayResolverResult(needle, fields[CacheTitle], fields[CacheArch], IdentifierVolume)
360360
entry.ComputeRankMatch(needle)
361361
res = append(res, entry)
362362
}
@@ -379,21 +379,21 @@ func (c *ScalewayCache) LookUpBootscripts(needle string, acceptUUID bool) Scalew
379379

380380
if acceptUUID && anonuuid.IsUUID(needle) == nil {
381381
if fields, ok := c.Bootscripts[needle]; ok {
382-
entry := NewScalewayResolverResult(needle, fields[TITLE], fields[ARCH], IdentifierBootscript)
382+
entry := NewScalewayResolverResult(needle, fields[CacheTitle], fields[CacheArch], IdentifierBootscript)
383383
entry.ComputeRankMatch(needle)
384384
res = append(res, entry)
385385
}
386386
}
387387

388388
nameRegex := regexp.MustCompile(`(?i)` + regexp.MustCompile(`[_-]`).ReplaceAllString(needle, ".*"))
389389
for identifier, fields := range c.Bootscripts {
390-
if fields[TITLE] == needle {
391-
entry := NewScalewayResolverResult(identifier, fields[TITLE], fields[ARCH], IdentifierBootscript)
390+
if fields[CacheTitle] == needle {
391+
entry := NewScalewayResolverResult(identifier, fields[CacheTitle], fields[CacheArch], IdentifierBootscript)
392392
entry.ComputeRankMatch(needle)
393393
exactMatches = append(exactMatches, entry)
394394
}
395-
if strings.HasPrefix(identifier, needle) || nameRegex.MatchString(fields[TITLE]) {
396-
entry := NewScalewayResolverResult(identifier, fields[TITLE], fields[ARCH], IdentifierBootscript)
395+
if strings.HasPrefix(identifier, needle) || nameRegex.MatchString(fields[CacheTitle]) {
396+
entry := NewScalewayResolverResult(identifier, fields[CacheTitle], fields[CacheArch], IdentifierBootscript)
397397
entry.ComputeRankMatch(needle)
398398
res = append(res, entry)
399399
}
@@ -416,21 +416,21 @@ func (c *ScalewayCache) LookUpServers(needle string, acceptUUID bool) ScalewayRe
416416

417417
if acceptUUID && anonuuid.IsUUID(needle) == nil {
418418
if fields, ok := c.Servers[needle]; ok {
419-
entry := NewScalewayResolverResult(needle, fields[TITLE], fields[ARCH], IdentifierServer)
419+
entry := NewScalewayResolverResult(needle, fields[CacheTitle], fields[CacheArch], IdentifierServer)
420420
entry.ComputeRankMatch(needle)
421421
res = append(res, entry)
422422
}
423423
}
424424

425425
nameRegex := regexp.MustCompile(`(?i)` + regexp.MustCompile(`[_-]`).ReplaceAllString(needle, ".*"))
426426
for identifier, fields := range c.Servers {
427-
if fields[TITLE] == needle {
428-
entry := NewScalewayResolverResult(identifier, fields[TITLE], fields[ARCH], IdentifierServer)
427+
if fields[CacheTitle] == needle {
428+
entry := NewScalewayResolverResult(identifier, fields[CacheTitle], fields[CacheArch], IdentifierServer)
429429
entry.ComputeRankMatch(needle)
430430
exactMatches = append(exactMatches, entry)
431431
}
432-
if strings.HasPrefix(identifier, needle) || nameRegex.MatchString(fields[TITLE]) {
433-
entry := NewScalewayResolverResult(identifier, fields[TITLE], fields[ARCH], IdentifierServer)
432+
if strings.HasPrefix(identifier, needle) || nameRegex.MatchString(fields[CacheTitle]) {
433+
entry := NewScalewayResolverResult(identifier, fields[CacheTitle], fields[CacheArch], IdentifierServer)
434434
entry.ComputeRankMatch(needle)
435435
res = append(res, entry)
436436
}
@@ -539,8 +539,8 @@ func (c *ScalewayCache) InsertServer(identifier, region, arch, owner, name strin
539539
defer c.Lock.Unlock()
540540

541541
fields, exists := c.Servers[identifier]
542-
if !exists || fields[TITLE] != name {
543-
c.Servers[identifier] = [MAXFIELD]string{region, arch, owner, name}
542+
if !exists || fields[CacheTitle] != name {
543+
c.Servers[identifier] = [CacheMaxfield]string{region, arch, owner, name}
544544
c.Modified = true
545545
}
546546
}
@@ -559,7 +559,7 @@ func (c *ScalewayCache) ClearServers() {
559559
c.Lock.Lock()
560560
defer c.Lock.Unlock()
561561

562-
c.Servers = make(map[string][MAXFIELD]string)
562+
c.Servers = make(map[string][CacheMaxfield]string)
563563
c.Modified = true
564564
}
565565

@@ -569,8 +569,8 @@ func (c *ScalewayCache) InsertImage(identifier, region, arch, owner, name string
569569
defer c.Lock.Unlock()
570570

571571
fields, exists := c.Images[identifier]
572-
if !exists || fields[TITLE] != name {
573-
c.Images[identifier] = [MAXFIELD]string{region, arch, owner, name}
572+
if !exists || fields[CacheTitle] != name {
573+
c.Images[identifier] = [CacheMaxfield]string{region, arch, owner, name}
574574
c.Modified = true
575575
}
576576
}
@@ -589,7 +589,7 @@ func (c *ScalewayCache) ClearImages() {
589589
c.Lock.Lock()
590590
defer c.Lock.Unlock()
591591

592-
c.Images = make(map[string][MAXFIELD]string)
592+
c.Images = make(map[string][CacheMaxfield]string)
593593
c.Modified = true
594594
}
595595

@@ -599,8 +599,8 @@ func (c *ScalewayCache) InsertSnapshot(identifier, region, arch, owner, name str
599599
defer c.Lock.Unlock()
600600

601601
fields, exists := c.Snapshots[identifier]
602-
if !exists || fields[TITLE] != name {
603-
c.Snapshots[identifier] = [MAXFIELD]string{region, arch, owner, name}
602+
if !exists || fields[CacheTitle] != name {
603+
c.Snapshots[identifier] = [CacheMaxfield]string{region, arch, owner, name}
604604
c.Modified = true
605605
}
606606
}
@@ -619,7 +619,7 @@ func (c *ScalewayCache) ClearSnapshots() {
619619
c.Lock.Lock()
620620
defer c.Lock.Unlock()
621621

622-
c.Snapshots = make(map[string][MAXFIELD]string)
622+
c.Snapshots = make(map[string][CacheMaxfield]string)
623623
c.Modified = true
624624
}
625625

@@ -629,8 +629,8 @@ func (c *ScalewayCache) InsertVolume(identifier, region, arch, owner, name strin
629629
defer c.Lock.Unlock()
630630

631631
fields, exists := c.Volumes[identifier]
632-
if !exists || fields[TITLE] != name {
633-
c.Volumes[identifier] = [MAXFIELD]string{region, arch, owner, name}
632+
if !exists || fields[CacheTitle] != name {
633+
c.Volumes[identifier] = [CacheMaxfield]string{region, arch, owner, name}
634634
c.Modified = true
635635
}
636636
}
@@ -649,7 +649,7 @@ func (c *ScalewayCache) ClearVolumes() {
649649
c.Lock.Lock()
650650
defer c.Lock.Unlock()
651651

652-
c.Volumes = make(map[string][MAXFIELD]string)
652+
c.Volumes = make(map[string][CacheMaxfield]string)
653653
c.Modified = true
654654
}
655655

@@ -659,8 +659,8 @@ func (c *ScalewayCache) InsertBootscript(identifier, region, arch, owner, name s
659659
defer c.Lock.Unlock()
660660

661661
fields, exists := c.Bootscripts[identifier]
662-
if !exists || fields[TITLE] != name {
663-
c.Bootscripts[identifier] = [MAXFIELD]string{region, arch, owner, name}
662+
if !exists || fields[CacheTitle] != name {
663+
c.Bootscripts[identifier] = [CacheMaxfield]string{region, arch, owner, name}
664664
c.Modified = true
665665
}
666666
}
@@ -679,7 +679,7 @@ func (c *ScalewayCache) ClearBootscripts() {
679679
c.Lock.Lock()
680680
defer c.Lock.Unlock()
681681

682-
c.Bootscripts = make(map[string][MAXFIELD]string)
682+
c.Bootscripts = make(map[string][CacheMaxfield]string)
683683
c.Modified = true
684684
}
685685

pkg/cli/x_completion.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,43 +66,43 @@ func runCompletion(cmd *Command, args []string) error {
6666
switch category {
6767
case "servers-all":
6868
for identifier, fields := range cmd.API.Cache.Servers {
69-
elements = append(elements, identifier, wordifyName(fields[api.TITLE], "server"))
69+
elements = append(elements, identifier, wordifyName(fields[api.CacheTitle], "server"))
7070
}
7171
case "servers-names":
7272
for _, fields := range cmd.API.Cache.Servers {
73-
elements = append(elements, wordifyName(fields[api.TITLE], "server"))
73+
elements = append(elements, wordifyName(fields[api.CacheTitle], "server"))
7474
}
7575
case "images-all":
7676
for identifier, fields := range cmd.API.Cache.Images {
77-
elements = append(elements, identifier, wordifyName(fields[api.TITLE], "image"))
77+
elements = append(elements, identifier, wordifyName(fields[api.CacheTitle], "image"))
7878
}
7979
case "images-names":
8080
for _, fields := range cmd.API.Cache.Images {
81-
elements = append(elements, wordifyName(fields[api.TITLE], "image"))
81+
elements = append(elements, wordifyName(fields[api.CacheTitle], "image"))
8282
}
8383
case "volumes-all":
8484
for identifier, fields := range cmd.API.Cache.Volumes {
85-
elements = append(elements, identifier, wordifyName(fields[api.TITLE], "volume"))
85+
elements = append(elements, identifier, wordifyName(fields[api.CacheTitle], "volume"))
8686
}
8787
case "volumes-names":
8888
for _, fields := range cmd.API.Cache.Volumes {
89-
elements = append(elements, wordifyName(fields[api.TITLE], "volume"))
89+
elements = append(elements, wordifyName(fields[api.CacheTitle], "volume"))
9090
}
9191
case "snapshots-all":
9292
for identifier, fields := range cmd.API.Cache.Snapshots {
93-
elements = append(elements, identifier, wordifyName(fields[api.TITLE], "snapshot"))
93+
elements = append(elements, identifier, wordifyName(fields[api.CacheTitle], "snapshot"))
9494
}
9595
case "snapshots-names":
9696
for _, fields := range cmd.API.Cache.Snapshots {
97-
elements = append(elements, wordifyName(fields[api.TITLE], "snapshot"))
97+
elements = append(elements, wordifyName(fields[api.CacheTitle], "snapshot"))
9898
}
9999
case "bootscripts-all":
100100
for identifier, fields := range cmd.API.Cache.Bootscripts {
101-
elements = append(elements, identifier, wordifyName(fields[api.TITLE], "bootscript"))
101+
elements = append(elements, identifier, wordifyName(fields[api.CacheTitle], "bootscript"))
102102
}
103103
case "bootscripts-names":
104104
for _, fields := range cmd.API.Cache.Bootscripts {
105-
elements = append(elements, wordifyName(fields[api.TITLE], "bootscript"))
105+
elements = append(elements, wordifyName(fields[api.CacheTitle], "bootscript"))
106106
}
107107
default:
108108
return fmt.Errorf("Unhandled category of completion: %s", category)

0 commit comments

Comments
 (0)