fix: sort mapped locations to solve the folder/subfolder issue

This commit is contained in:
lnu 2020-11-20 19:39:07 +01:00 committed by Jan De Dobbeleer
parent 2a54724a75
commit c0fd060c89

View file

@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"path/filepath" "path/filepath"
"sort"
"strings" "strings"
) )
@ -78,9 +79,20 @@ func (pt *path) getShortPath() string {
mappedLocations[key] = val mappedLocations[key] = val
} }
for location, value := range mappedLocations { // sort map keys in reverse order
if strings.HasPrefix(pwd, location) { // fixes case when a subfoder and its parent are mapped
return strings.Replace(pwd, location, value, 1) // ex /users/test and /users/test/dev
keys := make([]string, len(mappedLocations))
i := 0
for k := range mappedLocations {
keys[i] = k
i++
}
sort.Sort(sort.Reverse(sort.StringSlice(keys)))
for _, value := range keys {
if strings.HasPrefix(pwd, value) {
return strings.Replace(pwd, value, mappedLocations[value], 1)
} }
} }
return pwd return pwd