Skip to content

Commit 4fd45f9

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

File tree

3 files changed

+36
-29
lines changed

3 files changed

+36
-29
lines changed

cmd/incus/main.go

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,7 @@ func aliases() []string {
9191
return aliases
9292
}
9393

94-
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-
94+
func createApp() (*cobra.Command, *cmdGlobal) {
10295
// Setup the parser
10396
app := &cobra.Command{}
10497
app.Use = "incus"
@@ -305,8 +298,16 @@ 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+
return app, &globalCmd
302+
}
303+
304+
func main() {
305+
app, globalCmd := createApp()
306+
307+
// Parse flags to deal with --all flag and --sub-commands flag and to process aliases
308+
err := app.ParseFlags(os.Args[1:])
309+
308310
// Deal with --all flag and --sub-commands flag
309-
err = app.ParseFlags(os.Args[1:])
310311
if err == nil {
311312
if globalCmd.flagHelpAll {
312313
// Show all commands
@@ -324,6 +325,13 @@ Custom commands can be defined through aliases, use "incus alias" to control tho
324325
}
325326
}
326327

328+
// Process aliases
329+
err = execIfAliases(app)
330+
if err != nil {
331+
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
332+
os.Exit(1)
333+
}
334+
327335
// Run the main command and handle errors
328336
err = app.Execute()
329337
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, args []string, 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 := args[1:slices.Index(args, fset.Arg(0))]
88+
89+
// origArgs contains everything except the flags in newArgs
90+
origArgs := append([]string{args[0]}, 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, os.Args, app)
218215
if err != nil {
219216
return err
220217
} else if !expanded {

cmd/incus/main_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,9 @@ func TestExpandAliases(t *testing.T) {
8989
conf := &config.Config{Aliases: aliases}
9090

9191
for _, tc := range testcases {
92-
result, expanded, err := expandAlias(conf, tc.input)
92+
app, _ := createApp()
93+
_ = app.ParseFlags(tc.input[1:])
94+
result, expanded, err := expandAlias(conf, tc.input, app)
9395
if tc.expectErr {
9496
assert.Error(t, err)
9597
continue

0 commit comments

Comments
 (0)