Skip to content

Commit

Permalink
incus: Improve completion for file push and file pull
Browse files Browse the repository at this point in the history
Signed-off-by: montag451 <[email protected]>
  • Loading branch information
montag451 committed Dec 1, 2024
1 parent 42abaca commit 6e51f3d
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
36 changes: 36 additions & 0 deletions cmd/incus/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package main

import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"

Expand Down Expand Up @@ -1222,3 +1224,37 @@ func (g *cmdGlobal) cmpStoragePoolVolumes(poolName string) ([]string, cobra.Shel

return volumes, cobra.ShellCompDirectiveNoFileComp
}

func (g *cmdGlobal) cmpFiles(toComplete string, includeLocalFiles bool) ([]string, cobra.ShellCompDirective) {
instances, directives := g.cmpInstances(toComplete)
for i := range instances {
inst := instances[i]
if strings.HasSuffix(inst, ":") {
continue
}

instances[i] = inst + "/"
}

if len(instances) > 0 {
directives |= cobra.ShellCompDirectiveNoSpace
}

if !includeLocalFiles {
return instances, directives
}

files, _ := filepath.Glob(toComplete + "*")
for i := range files {
file := files[i]
info, err := os.Stat(file)
if err != nil || !info.IsDir() {
continue
}

files[i] = file + string(filepath.Separator)
directives |= cobra.ShellCompDirectiveNoSpace
}

return append(instances, files...), directives
}
18 changes: 18 additions & 0 deletions cmd/incus/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -475,8 +475,17 @@ func (c *cmdFilePull) Command() *cobra.Command {

cmd.Flags().BoolVarP(&c.file.flagMkdir, "create-dirs", "p", false, i18n.G("Create any directories necessary"))
cmd.Flags().BoolVarP(&c.file.flagRecursive, "recursive", "r", false, i18n.G("Recursively transfer files"))

cmd.RunE = c.Run

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

return c.global.cmpFiles(toComplete, true)
}

return cmd
}

Expand Down Expand Up @@ -697,8 +706,17 @@ func (c *cmdFilePush) Command() *cobra.Command {
cmd.Flags().IntVar(&c.file.flagUID, "uid", -1, i18n.G("Set the file's uid on push")+"``")
cmd.Flags().IntVar(&c.file.flagGID, "gid", -1, i18n.G("Set the file's gid on push")+"``")
cmd.Flags().StringVar(&c.file.flagMode, "mode", "", i18n.G("Set the file's perms on push")+"``")

cmd.RunE = c.Run

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

return c.global.cmpFiles(toComplete, true)
}

return cmd
}

Expand Down

0 comments on commit 6e51f3d

Please sign in to comment.