remove duplicate profile names

pull/6441/head
Medya Gh 2020-01-30 17:11:11 -08:00
parent 5b055d7ff8
commit e7fa3e0448
1 changed files with 21 additions and 0 deletions

View File

@ -158,6 +158,7 @@ func ListProfiles(miniHome ...string) (validPs []*Profile, inValidPs []*Profile,
if err == nil { if err == nil {
pDirs = append(pDirs, cs...) pDirs = append(pDirs, cs...)
} }
pDirs = removeDups(pDirs)
for _, n := range pDirs { for _, n := range pDirs {
p, err := LoadProfile(n, miniHome...) p, err := LoadProfile(n, miniHome...)
if err != nil { if err != nil {
@ -173,6 +174,26 @@ func ListProfiles(miniHome ...string) (validPs []*Profile, inValidPs []*Profile,
return validPs, inValidPs, nil return validPs, inValidPs, nil
} }
// removeDups removes duplicates
func removeDups(profiles []string) []string {
// Use map to record duplicates as we find them.
seen := map[string]bool{}
result := []string{}
for n := range profiles {
if seen[profiles[n]] {
// Do not add duplicate.
} else {
// Record this element as an encountered element.
seen[profiles[n]] = true
// Append to result slice.
result = append(result, profiles[n])
}
}
// Return the new slice.
return result
}
// LoadProfile loads type Profile based on its name // LoadProfile loads type Profile based on its name
func LoadProfile(name string, miniHome ...string) (*Profile, error) { func LoadProfile(name string, miniHome ...string) (*Profile, error) {
cfg, err := DefaultLoader.LoadConfigFromFile(name, miniHome...) cfg, err := DefaultLoader.LoadConfigFromFile(name, miniHome...)