Skip to content

Commit a0a39fc

Browse files
committed
[WIP] Save/load instance groups with project, members
1 parent 24ae6e6 commit a0a39fc

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

commands.go

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,16 @@ type configRoot struct {
8686
type cache struct {
8787
CacheDir string
8888
Instances []*compute.Instance
89-
InstanceGroups []*compute.InstanceGroup
89+
InstanceGroups []*instanceGroupExt
9090
Projects []*cloudresourcemanager.Project
9191
}
9292

93+
type instanceGroupExt struct {
94+
InstanceGroup *compute.InstanceGroup
95+
Project *cloudresourcemanager.Project
96+
Members []*compute.Instance
97+
}
98+
9399
func loadCache() (*cache, error) {
94100
cachedir, err := homedir.Expand("~/.cache/geco/")
95101
if err != nil {
@@ -271,11 +277,11 @@ func doCache(cliCtx *cli.Context) {
271277
}
272278

273279
// gcloud compute instance-groups list (in parallel)
274-
instanceGroupNotify := make(chan []*compute.InstanceGroup)
280+
instanceGroupNotify := make(chan []*instanceGroupExt)
275281
for _, project := range c.Projects {
276-
go func(project *cloudresourcemanager.Project, notify chan<- []*compute.InstanceGroup) {
282+
go func(project *cloudresourcemanager.Project, notify chan<- []*instanceGroupExt) {
277283
semaphore <- 0
278-
var instanceGroups []*compute.InstanceGroup
284+
var ret []*instanceGroupExt
279285

280286
log.Printf("loading instance groups in %s (%s)...\n", project.Name, project.ProjectId)
281287
service, err := compute.New(client)
@@ -298,7 +304,12 @@ func doCache(cliCtx *cli.Context) {
298304
}
299305

300306
for _, instanceGroupsScopedList := range res.Items {
301-
instanceGroups = append(instanceGroups, instanceGroupsScopedList.InstanceGroups...)
307+
for _, instanceGroup := range instanceGroupsScopedList.InstanceGroups {
308+
ret = append(ret, &instanceGroupExt{
309+
InstanceGroup: instanceGroup,
310+
Project: project,
311+
})
312+
}
302313
}
303314

304315
if res.NextPageToken != "" {
@@ -310,9 +321,9 @@ func doCache(cliCtx *cli.Context) {
310321
}
311322

312323
<-semaphore
313-
notify <- instanceGroups
324+
notify <- ret
314325

315-
log.Printf("loaded instances in %s (%s), %d instances found.\n", project.Name, project.ProjectId, len(instanceGroups))
326+
log.Printf("loaded instance groups in %s (%s), %d instances found.\n", project.Name, project.ProjectId, len(ret))
316327
}(project, instanceGroupNotify)
317328
}
318329
for _ = range c.Projects {
@@ -322,16 +333,13 @@ func doCache(cliCtx *cli.Context) {
322333
}
323334
}
324335

325-
// sort projects, instances, instance-groups
336+
// sort projects, instances
326337
sort.Slice(c.Projects, func(i, j int) bool {
327338
return c.Projects[i].ProjectId < c.Projects[j].ProjectId
328339
})
329340
sort.Slice(c.Instances, func(i, j int) bool {
330341
return c.Instances[i].SelfLink < c.Instances[j].SelfLink
331342
})
332-
sort.Slice(c.InstanceGroups, func(i, j int) bool {
333-
return c.InstanceGroups[i].SelfLink < c.InstanceGroups[j].SelfLink
334-
})
335343

336344
saveCache(c)
337345
log.Println("saved cache.")

0 commit comments

Comments
 (0)