Skip to content

Commit

Permalink
Merge pull request #552 from adamcstephens/cmp/20240227
Browse files Browse the repository at this point in the history
incus: add completions for remotes and projects
  • Loading branch information
stgraber authored Feb 28, 2024
2 parents 27cc7c7 + 707a249 commit 78886cc
Show file tree
Hide file tree
Showing 3 changed files with 198 additions and 1 deletion.
66 changes: 66 additions & 0 deletions cmd/incus/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,62 @@ func (g *cmdGlobal) cmpProfiles(toComplete string, includeRemotes bool) ([]strin
return results, cobra.ShellCompDirectiveNoFileComp
}

func (g *cmdGlobal) cmpProjectConfigs(projectName string) ([]string, cobra.ShellCompDirective) {
resources, err := g.ParseServers(projectName)
if err != nil || len(resources) == 0 {
return nil, cobra.ShellCompDirectiveError
}

resource := resources[0]
client := resource.server

project, _, err := client.GetProject(resource.name)
if err != nil {
return nil, cobra.ShellCompDirectiveError
}

var configs []string
for c := range project.Config {
configs = append(configs, c)
}

return configs, cobra.ShellCompDirectiveNoFileComp
}

func (g *cmdGlobal) cmpProjects(toComplete string) ([]string, cobra.ShellCompDirective) {
results := []string{}

resources, _ := g.ParseServers(toComplete)

if len(resources) > 0 {
resource := resources[0]

projects, err := resource.server.GetProjectNames()
if err != nil {
return nil, cobra.ShellCompDirectiveError
}

for _, project := range projects {
var name string

if resource.remote == g.conf.DefaultRemote && !strings.Contains(toComplete, g.conf.DefaultRemote) {
name = project
} else {
name = fmt.Sprintf("%s:%s", resource.remote, project)
}

results = append(results, name)
}
}

if !strings.Contains(toComplete, ":") {
remotes, _ := g.cmpRemotes(false)
results = append(results, remotes...)
}

return results, cobra.ShellCompDirectiveNoFileComp
}

func (g *cmdGlobal) cmpRemotes(includeAll bool) ([]string, cobra.ShellCompDirective) {
results := []string{}

Expand All @@ -283,3 +339,13 @@ func (g *cmdGlobal) cmpRemotes(includeAll bool) ([]string, cobra.ShellCompDirect

return results, cobra.ShellCompDirectiveNoSpace
}

func (g *cmdGlobal) cmpRemoteNames() ([]string, cobra.ShellCompDirective) {
results := []string{}

for remoteName := range g.conf.Remotes {
results = append(results, remoteName)
}

return results, cobra.ShellCompDirectiveNoFileComp
}
101 changes: 100 additions & 1 deletion cmd/incus/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@ func (c *cmdProjectCreate) Command() *cobra.Command {

cmd.RunE = c.Run

cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
return c.global.cmpRemotes(false)
}

return nil, cobra.ShellCompDirectiveNoFileComp
}

return cmd
}

Expand Down Expand Up @@ -161,6 +169,14 @@ func (c *cmdProjectDelete) Command() *cobra.Command {

cmd.RunE = c.Run

cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
return c.global.cmpProjects(toComplete)
}

return nil, cobra.ShellCompDirectiveNoFileComp
}

return cmd
}

Expand Down Expand Up @@ -227,6 +243,14 @@ func (c *cmdProjectEdit) Command() *cobra.Command {

cmd.RunE = c.Run

cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
return c.global.cmpProjects(toComplete)
}

return nil, cobra.ShellCompDirectiveNoFileComp
}

return cmd
}

Expand Down Expand Up @@ -352,6 +376,19 @@ func (c *cmdProjectGet) Command() *cobra.Command {

cmd.RunE = c.Run
cmd.Flags().BoolVarP(&c.flagIsProperty, "property", "p", false, i18n.G("Get the key as a project property"))

cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
return c.global.cmpProjects(toComplete)
}

if len(args) == 1 {
return c.global.cmpProjectConfigs(args[0])
}

return nil, cobra.ShellCompDirectiveNoFileComp
}

return cmd
}

Expand Down Expand Up @@ -414,6 +451,14 @@ func (c *cmdProjectList) Command() *cobra.Command {

cmd.RunE = c.Run

cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
return c.global.cmpRemotes(false)
}

return nil, cobra.ShellCompDirectiveNoFileComp
}

return cmd
}

Expand Down Expand Up @@ -525,6 +570,14 @@ func (c *cmdProjectRename) Command() *cobra.Command {

cmd.RunE = c.Run

cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
return c.global.cmpProjects(toComplete)
}

return nil, cobra.ShellCompDirectiveNoFileComp
}

return cmd
}

Expand Down Expand Up @@ -585,6 +638,15 @@ For backward compatibility, a single configuration key may still be set with:

cmd.RunE = c.Run
cmd.Flags().BoolVarP(&c.flagIsProperty, "property", "p", false, i18n.G("Set the key as a project property"))

cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
return c.global.cmpProjects(toComplete)
}

return nil, cobra.ShellCompDirectiveNoFileComp
}

return cmd
}

Expand Down Expand Up @@ -661,6 +723,19 @@ func (c *cmdProjectUnset) Command() *cobra.Command {

cmd.RunE = c.Run
cmd.Flags().BoolVarP(&c.flagIsProperty, "property", "p", false, i18n.G("Unset the key as a project property"))

cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
return c.global.cmpProjects(toComplete)
}

if len(args) == 1 {
return c.global.cmpProjectConfigs(args[0])
}

return nil, cobra.ShellCompDirectiveNoFileComp
}

return cmd
}

Expand Down Expand Up @@ -692,6 +767,14 @@ func (c *cmdProjectShow) Command() *cobra.Command {

cmd.RunE = c.Run

cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
return c.global.cmpProjects(toComplete)
}

return nil, cobra.ShellCompDirectiveNoFileComp
}

return cmd
}

Expand Down Expand Up @@ -745,6 +828,14 @@ func (c *cmdProjectSwitch) Command() *cobra.Command {

cmd.RunE = c.Run

cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
return c.global.cmpProjects(toComplete)
}

return nil, cobra.ShellCompDirectiveNoFileComp
}

return cmd
}

Expand Down Expand Up @@ -797,14 +888,22 @@ type cmdProjectInfo struct {

func (c *cmdProjectInfo) Command() *cobra.Command {
cmd := &cobra.Command{}
cmd.Use = usage("info", i18n.G("[<remote>:]<project> <key>"))
cmd.Use = usage("info", i18n.G("[<remote>:]<project>"))
cmd.Short = i18n.G("Get a summary of resource allocations")
cmd.Long = cli.FormatSection(i18n.G("Description"), i18n.G(
`Get a summary of resource allocations`))
cmd.Flags().StringVarP(&c.flagFormat, "format", "f", "table", i18n.G("Format (csv|json|table|yaml|compact)")+"``")

cmd.RunE = c.Run

cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
return c.global.cmpProjects(toComplete)
}

return nil, cobra.ShellCompDirectiveNoFileComp
}

return cmd
}

Expand Down
32 changes: 32 additions & 0 deletions cmd/incus/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,14 @@ func (c *cmdRemoteRename) Command() *cobra.Command {

cmd.RunE = c.Run

cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
return c.global.cmpRemoteNames()
}

return nil, cobra.ShellCompDirectiveNoFileComp
}

return cmd
}

Expand Down Expand Up @@ -818,6 +826,14 @@ func (c *cmdRemoteRemove) Command() *cobra.Command {

cmd.RunE = c.Run

cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
return c.global.cmpRemoteNames()
}

return nil, cobra.ShellCompDirectiveNoFileComp
}

return cmd
}

Expand Down Expand Up @@ -873,6 +889,14 @@ func (c *cmdRemoteSwitch) Command() *cobra.Command {

cmd.RunE = c.Run

cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
return c.global.cmpRemoteNames()
}

return nil, cobra.ShellCompDirectiveNoFileComp
}

return cmd
}

Expand Down Expand Up @@ -911,6 +935,14 @@ func (c *cmdRemoteSetURL) Command() *cobra.Command {

cmd.RunE = c.Run

cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
return c.global.cmpRemoteNames()
}

return nil, cobra.ShellCompDirectiveNoFileComp
}

return cmd
}

Expand Down

0 comments on commit 78886cc

Please sign in to comment.