Skip to content

Commit

Permalink
incus/alias: Stable sorting of alias names
Browse files Browse the repository at this point in the history
Closes lxc#1237

Signed-off-by: Stéphane Graber <[email protected]>
  • Loading branch information
stgraber committed Sep 25, 2024
1 parent b18c9c1 commit 7030add
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion cmd/incus/main_aliases.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"path"
"path/filepath"
"regexp"
"slices"
"strconv"
"strings"

Expand All @@ -31,7 +32,29 @@ func findAlias(aliases map[string]string, origArgs []string) ([]string, []string
aliasKey := []string{}
aliasValue := []string{}

for k, v := range aliases {
// Sort the aliases in a stable order, preferring the long multi-fields ones.
aliasNames := make([]string, 0, len(aliases))
for k := range aliases {
aliasNames = append(aliasNames, k)
}

slices.Sort(aliasNames)
slices.SortStableFunc(aliasNames, func(a, b string) int {
aFields := strings.Split(a, " ")
bFields := strings.Split(b, " ")

if len(aFields) == len(bFields) {
return 0
} else if len(aFields) < len(bFields) {
return 1
}

return -1
})

for _, k := range aliasNames {
v := aliases[k]

foundAlias = true
for i, key := range strings.Split(k, " ") {
if len(origArgs) <= i+1 || origArgs[i+1] != key {
Expand Down

0 comments on commit 7030add

Please sign in to comment.