From 14678618cf2999a990526da035836dbb36ccf950 Mon Sep 17 00:00:00 2001 From: braheezy Date: Mon, 18 Sep 2023 18:45:02 -0700 Subject: [PATCH] fix: Load environments deterministically The original implemenation collected environment information using a map and this unorderedness caused the indeterministic ordering of env tabs. This refactor uses an array instead --- internal/app/ecosystem.go | 41 +++++++++++++++++++++++++++++---------- 1 file changed, 31 insertions(+), 10 deletions(-) diff --git a/internal/app/ecosystem.go b/internal/app/ecosystem.go index 8c6a288..85513ac 100644 --- a/internal/app/ecosystem.go +++ b/internal/app/ecosystem.go @@ -57,20 +57,41 @@ func createEcosystem(client *vagrant.VagrantClient) (Ecosystem, error) { machines = append(machines, machine) } // Create different envs by grouping machines based on machine-home - envGroups := make(map[string][]Machine) + type EnvironmentGroup struct { + Name string + Machines []Machine + } + var envGroups []EnvironmentGroup for _, machine := range machines { - // TODO: Bug if two different paths have the same folder name e.g. /foo/env1 and /bar/env1 will incorrectly be treated the same - envGroups[path.Base(machine.home)] = append(envGroups[path.Base(machine.home)], machine) + found := false + for i, env := range envGroups { + // TODO: Bug if two different paths have the same folder name e.g. /foo/env1 and /bar/env1 will incorrectly be treated the same + if env.Name == path.Base(machine.home) { + envGroups[i].Machines = append(envGroups[i].Machines, machine) + found = true + break + } + } + if !found { + env := EnvironmentGroup{ + Name: path.Base(machine.home), + Machines: []Machine{machine}, + } + envGroups = append(envGroups, env) + } } + var environments []Environment - for envName, machines := range envGroups { - env := Environment{ - name: envName, - machines: machines, - home: envGroups[envName][0].home, - hasFocus: true, + for _, envGroup := range envGroups { + if len(envGroup.Machines) > 0 { + env := Environment{ + name: envGroup.Name, + machines: envGroup.Machines, + home: envGroup.Machines[0].home, + hasFocus: true, + } + environments = append(environments, env) } - environments = append(environments, env) } return Ecosystem{ environments: environments,