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 2, 2024
1 parent 42abaca commit 68a936a
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 0 deletions.
83 changes: 83 additions & 0 deletions cmd/incus/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package main

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

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

return volumes, cobra.ShellCompDirectiveNoFileComp
}

func isSymlinkToDir(path string, d fs.DirEntry) bool {
if d.Type()&fs.ModeSymlink == 0 {
return false
}

info, err := os.Stat(path)
if err != nil || !info.IsDir() {
return false
}

return true
}

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

instances[i] += "/"
}

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

if !includeLocalFiles {
return instances, directives
}

var files []string
sep := string(filepath.Separator)
dir, prefix := filepath.Split(toComplete)
switch prefix {
case ".":
files = append(files, dir+"."+sep)
fallthrough
case "..":
files = append(files, dir+".."+sep)
directives |= cobra.ShellCompDirectiveNoSpace
}

root, err := filepath.EvalSymlinks(filepath.Dir(dir))
if err != nil {
return append(instances, files...), directives
}

_ = filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
if err != nil || path == root {
return err
}

base := filepath.Base(path)
if strings.HasPrefix(base, prefix) {
file := dir + base
switch {
case d.IsDir():
directives |= cobra.ShellCompDirectiveNoSpace
file += sep
case isSymlinkToDir(path, d):
directives |= cobra.ShellCompDirectiveNoSpace
if base == prefix {
file += sep
}
}

files = append(files, file)
}

if d.IsDir() {
return fs.SkipDir
}

return nil
})

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 68a936a

Please sign in to comment.