Skip to content

Commit 7925eae

Browse files
committed
incus: Fix alias arguments handling
Remove the limitations stated in 50373fe Signed-off-by: montag451 <[email protected]>
1 parent 315cb17 commit 7925eae

File tree

2 files changed

+26
-27
lines changed

2 files changed

+26
-27
lines changed

cmd/incus/main.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,6 @@ func aliases() []string {
9292
}
9393

9494
func main() {
95-
// Process aliases
96-
err := execIfAliases()
97-
if err != nil {
98-
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
99-
os.Exit(1)
100-
}
101-
10295
// Setup the parser
10396
app := &cobra.Command{}
10497
app.Use = "incus"
@@ -305,8 +298,10 @@ Custom commands can be defined through aliases, use "incus alias" to control tho
305298
app.Flags().BoolVar(&globalCmd.flagHelpAll, "all", false, i18n.G("Show less common commands"))
306299
help.Flags().BoolVar(&globalCmd.flagHelpAll, "all", false, i18n.G("Show less common commands"))
307300

301+
// Parse flags to deal with --all flag and --sub-commands flag and to process aliases
302+
err := app.ParseFlags(os.Args[1:])
303+
308304
// Deal with --all flag and --sub-commands flag
309-
err = app.ParseFlags(os.Args[1:])
310305
if err == nil {
311306
if globalCmd.flagHelpAll {
312307
// Show all commands
@@ -324,6 +319,13 @@ Custom commands can be defined through aliases, use "incus alias" to control tho
324319
}
325320
}
326321

322+
// Process aliases
323+
err = execIfAliases(app)
324+
if err != nil {
325+
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
326+
os.Exit(1)
327+
}
328+
327329
// Run the main command and handle errors
328330
err = app.Execute()
329331
if err != nil {

cmd/incus/main_aliases.go

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"strings"
1111

1212
"github.com/kballard/go-shellquote"
13+
"github.com/spf13/cobra"
1314

1415
"github.com/lxc/incus/v6/internal/i18n"
1516
config "github.com/lxc/incus/v6/shared/cliconfig"
@@ -76,26 +77,24 @@ func findAlias(aliases map[string]string, origArgs []string) ([]string, []string
7677
return aliasKey, aliasValue, foundAlias
7778
}
7879

79-
func expandAlias(conf *config.Config, args []string) ([]string, bool, error) {
80-
var completion = false
81-
var completionFrament string
82-
var newArgs []string
83-
var origArgs []string
84-
85-
for _, arg := range args[1:] {
86-
if !strings.HasPrefix(arg, "-") {
87-
break
88-
}
89-
90-
newArgs = append(newArgs, arg)
80+
func expandAlias(conf *config.Config, app *cobra.Command) ([]string, bool, error) {
81+
fset := app.Flags()
82+
if fset.NArg() == 0 {
83+
return nil, false, nil
9184
}
9285

93-
origArgs = append([]string{args[0]}, args[len(newArgs)+1:]...)
86+
// newArgs contains all the flags before the first positional argument
87+
newArgs := os.Args[1:slices.Index(os.Args, fset.Arg(0))]
88+
89+
// origArgs contains everything except the flags in newArgs
90+
origArgs := append([]string{os.Args[0]}, os.Args[len(newArgs)+1:]...)
9491

9592
// strip out completion subcommand and fragment from end
93+
completion := false
94+
completionFragment := ""
9695
if len(origArgs) >= 3 && origArgs[1] == "__complete" {
9796
completion = true
98-
completionFrament = origArgs[len(origArgs)-1]
97+
completionFragment = origArgs[len(origArgs)-1]
9998
origArgs = append(origArgs[:1], origArgs[2:len(origArgs)-1]...)
10099
}
101100

@@ -189,7 +188,7 @@ func expandAlias(conf *config.Config, args []string) ([]string, bool, error) {
189188
// add back in completion if it was stripped before
190189
if completion {
191190
newArgs = append([]string{newArgs[0], "__complete"}, newArgs[1:]...)
192-
newArgs = append(newArgs, completionFrament)
191+
newArgs = append(newArgs, completionFragment)
193192
}
194193

195194
// Add the rest of the arguments only if @ARGS@ wasn't used.
@@ -200,9 +199,7 @@ func expandAlias(conf *config.Config, args []string) ([]string, bool, error) {
200199
return newArgs, true, nil
201200
}
202201

203-
func execIfAliases() error {
204-
args := os.Args
205-
202+
func execIfAliases(app *cobra.Command) error {
206203
// Avoid loops
207204
if os.Getenv("INCUS_ALIASES") == "1" {
208205
return nil
@@ -214,7 +211,7 @@ func execIfAliases() error {
214211
}
215212

216213
// Expand the aliases
217-
newArgs, expanded, err := expandAlias(conf, args)
214+
newArgs, expanded, err := expandAlias(conf, app)
218215
if err != nil {
219216
return err
220217
} else if !expanded {

0 commit comments

Comments
 (0)