diff --git a/commands.go b/commands.go index d8638b0..f3a3689 100644 --- a/commands.go +++ b/commands.go @@ -86,10 +86,16 @@ type configRoot struct { type cache struct { CacheDir string Instances []*compute.Instance - InstanceGroups []*compute.InstanceGroup + InstanceGroups []*instanceGroupExt Projects []*cloudresourcemanager.Project } +type instanceGroupExt struct { + InstanceGroup *compute.InstanceGroup + Project *cloudresourcemanager.Project + Members []*compute.Instance +} + func loadCache() (*cache, error) { cachedir, err := homedir.Expand("~/.cache/geco/") if err != nil { @@ -267,11 +273,11 @@ func doCache(cliCtx *cli.Context) { } // gcloud compute instance-groups list (in parallel) - instanceGroupNotify := make(chan []*compute.InstanceGroup) + instanceGroupNotify := make(chan []*instanceGroupExt) for _, project := range c.Projects { - go func(project *cloudresourcemanager.Project, notify chan<- []*compute.InstanceGroup) { + go func(project *cloudresourcemanager.Project, notify chan<- []*instanceGroupExt) { semaphore <- 0 - var instanceGroups []*compute.InstanceGroup + var ret []*instanceGroupExt log.Printf("loading instance groups in %s (%s)...\n", project.Name, project.ProjectId) service, err := compute.New(client) @@ -294,7 +300,12 @@ func doCache(cliCtx *cli.Context) { } for _, instanceGroupsScopedList := range res.Items { - instanceGroups = append(instanceGroups, instanceGroupsScopedList.InstanceGroups...) + for _, instanceGroup := range instanceGroupsScopedList.InstanceGroups { + ret = append(ret, &instanceGroupExt{ + InstanceGroup: instanceGroup, + Project: project, + }) + } } if res.NextPageToken != "" { @@ -306,9 +317,9 @@ func doCache(cliCtx *cli.Context) { } <-semaphore - notify <- instanceGroups + notify <- ret - log.Printf("loaded instances in %s (%s), %d instances found.\n", project.Name, project.ProjectId, len(instanceGroups)) + log.Printf("loaded instance groups in %s (%s), %d instances found.\n", project.Name, project.ProjectId, len(ret)) }(project, instanceGroupNotify) } for _ = range c.Projects { @@ -318,16 +329,13 @@ func doCache(cliCtx *cli.Context) { } } - // sort projects, instances, instance-groups + // sort projects, instances sort.Slice(c.Projects, func(i, j int) bool { return c.Projects[i].ProjectId < c.Projects[j].ProjectId }) sort.Slice(c.Instances, func(i, j int) bool { return c.Instances[i].SelfLink < c.Instances[j].SelfLink }) - sort.Slice(c.InstanceGroups, func(i, j int) bool { - return c.InstanceGroups[i].SelfLink < c.InstanceGroups[j].SelfLink - }) saveCache(c) log.Println("saved cache.")