Skip to content

Commit

Permalink
CLI: Add flag completions for --project, --profile and --mode (#…
Browse files Browse the repository at this point in the history
…14758)

Closes #14691.

This PR adds flag completions for the following flags and commands:
- `--project` for all commands;
- `--profile` for `lxc init|launch`; and
- `--mode` for `lxc move`.
  • Loading branch information
tomponline authored Jan 9, 2025
2 parents c07e768 + 4cf0782 commit 26bcbad
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
4 changes: 4 additions & 0 deletions lxc/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ lxc init ubuntu:24.04 v1 --vm -c limits.cpu=2 -c limits.memory=8GiB -d root,size
return c.global.cmpImages(toComplete)
}

_ = cmd.RegisterFlagCompletionFunc("profile", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return c.global.cmpProfiles(toComplete, true)
})

return cmd
}

Expand Down
9 changes: 9 additions & 0 deletions lxc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,15 @@ For help with any of those, simply call them with --help.`))
app.PersistentFlags().BoolVarP(&globalCmd.flagQuiet, "quiet", "q", false, i18n.G("Don't show progress information"))
app.PersistentFlags().BoolVar(&globalCmd.flagSubCmds, "sub-commands", false, i18n.G("Use with help or --help to view sub-commands"))

_ = app.RegisterFlagCompletionFunc("project", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
projects, directives := globalCmd.cmpProjects(toComplete)
if projects != nil {
return projects, directives
}

return nil, cobra.ShellCompDirectiveError
})

// Wrappers
app.PersistentPreRunE = globalCmd.PreRun
app.PersistentPostRunE = globalCmd.PostRun
Expand Down
7 changes: 6 additions & 1 deletion lxc/move.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ lxc move <instance>/<old snapshot name> <instance>/<new snapshot name>

cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
return c.global.cmpInstances(toComplete)
instances, directives := c.global.cmpInstances(toComplete)
return instances, directives | cobra.ShellCompDirectiveNoSpace
}

if len(args) == 1 {
Expand All @@ -80,6 +81,10 @@ lxc move <instance>/<old snapshot name> <instance>/<new snapshot name>
return nil, cobra.ShellCompDirectiveNoFileComp
}

_ = cmd.RegisterFlagCompletionFunc("mode", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return []string{"pull", "push", "relay"}, cobra.ShellCompDirectiveNoFileComp
})

return cmd
}

Expand Down

0 comments on commit 26bcbad

Please sign in to comment.