Skip to content

Commit

Permalink
lxc/aliases: fix completion regression
Browse files Browse the repository at this point in the history
Signed-off-by: Adam Stephens <[email protected]>
(cherry picked from commit 06949bf3468105e8500fabfc839662a9d29059dd)
Signed-off-by: Kadin Sayani <[email protected]>
License: Apache-2.0
  • Loading branch information
adamcstephens authored and kadinsayani committed Sep 4, 2024
1 parent 732bd1e commit 8c6dfed
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion lxc/main_aliases.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ func findAlias(aliases map[string]string, origArgs []string) ([]string, []string
}

func expandAlias(conf *config.Config, args []string) ([]string, bool, error) {
var completion = false
var completionFragment string
var newArgs []string
var origArgs []string

Expand All @@ -62,6 +64,13 @@ func expandAlias(conf *config.Config, args []string) ([]string, bool, error) {

origArgs = append([]string{args[0]}, args[len(newArgs)+1:]...)

// strip out completion subcommand and fragment from end
if len(origArgs) >= 3 && origArgs[1] == "__complete" {
completion = true
completionFragment = origArgs[len(origArgs)-1]
origArgs = append(origArgs[:1], origArgs[2:len(origArgs)-1]...)
}

aliasKey, aliasValue, foundAlias := findAlias(conf.Aliases, origArgs)
if !foundAlias {
aliasKey, aliasValue, foundAlias = findAlias(defaultAliases, origArgs)
Expand Down Expand Up @@ -116,7 +125,13 @@ func expandAlias(conf *config.Config, args []string) ([]string, bool, error) {
for _, aliasArg := range aliasValue {
// Only replace all @ARGS@ when it is not part of another string
if aliasArg == "@ARGS@" {
newArgs = append(newArgs, atArgs...)
// if completing we want to stop on @ARGS@ and append the completion below
if completion {
break
} else {
newArgs = append(newArgs, atArgs...)
}

hasReplacedArgsVar = true
continue
}
Expand All @@ -143,6 +158,12 @@ func expandAlias(conf *config.Config, args []string) ([]string, bool, error) {
newArgs = append(newArgs, aliasArg)
}

// add back in completion if it was stripped before
if completion {
newArgs = append([]string{newArgs[0], "__complete"}, newArgs[1:]...)
newArgs = append(newArgs, completionFragment)
}

// Add the rest of the arguments only if @ARGS@ wasn't used.
if !hasReplacedArgsVar {
newArgs = append(newArgs, atArgs...)
Expand Down

0 comments on commit 8c6dfed

Please sign in to comment.