Skip to content

Commit

Permalink
fix: Load environments deterministically
Browse files Browse the repository at this point in the history
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
  • Loading branch information
braheezy committed Sep 19, 2023
1 parent cdb1db9 commit 1467861
Showing 1 changed file with 31 additions and 10 deletions.
41 changes: 31 additions & 10 deletions internal/app/ecosystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 1467861

Please sign in to comment.