Skip to content

Commit 8363c66

Browse files
committed
incus: Improve completion for file push and file pull
Signed-off-by: montag451 <[email protected]>
1 parent 42abaca commit 8363c66

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

cmd/incus/completion.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ package main
22

33
import (
44
"fmt"
5+
"io/fs"
6+
"os"
7+
"path/filepath"
58
"regexp"
69
"strings"
710

@@ -1222,3 +1225,84 @@ func (g *cmdGlobal) cmpStoragePoolVolumes(poolName string) ([]string, cobra.Shel
12221225

12231226
return volumes, cobra.ShellCompDirectiveNoFileComp
12241227
}
1228+
1229+
func isSymlinkToDir(path string, d fs.DirEntry) bool {
1230+
if d.Type()&fs.ModeSymlink == 0 {
1231+
return false
1232+
}
1233+
1234+
info, err := os.Stat(path)
1235+
if err != nil || !info.IsDir() {
1236+
return false
1237+
}
1238+
1239+
return true
1240+
}
1241+
1242+
func (g *cmdGlobal) cmpFiles(toComplete string, includeLocalFiles bool) ([]string, cobra.ShellCompDirective) {
1243+
instances, directives := g.cmpInstances(toComplete)
1244+
for i := range instances {
1245+
if strings.HasSuffix(instances[i], ":") {
1246+
continue
1247+
}
1248+
1249+
instances[i] += "/"
1250+
}
1251+
1252+
if len(instances) > 0 {
1253+
directives |= cobra.ShellCompDirectiveNoSpace
1254+
}
1255+
1256+
if !includeLocalFiles {
1257+
return instances, directives
1258+
}
1259+
1260+
var files []string
1261+
sep := string(filepath.Separator)
1262+
dir, prefix := filepath.Split(toComplete)
1263+
switch prefix {
1264+
case ".":
1265+
files = append(files, dir + "." + sep)
1266+
fallthrough
1267+
case "..":
1268+
files = append(files, dir + ".." + sep)
1269+
directives |= cobra.ShellCompDirectiveNoSpace
1270+
}
1271+
1272+
root, err := filepath.EvalSymlinks(filepath.Dir(dir))
1273+
if err != nil {
1274+
return append(instances, files...), directives
1275+
}
1276+
1277+
_ = filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
1278+
if err != nil || path == root {
1279+
return err
1280+
}
1281+
1282+
base := filepath.Base(path)
1283+
if strings.HasPrefix(base, prefix) {
1284+
file := dir + base
1285+
switch {
1286+
case isSymlinkToDir(path, d):
1287+
directives |= cobra.ShellCompDirectiveNoSpace
1288+
if base == prefix {
1289+
file += sep
1290+
}
1291+
1292+
case d.IsDir():
1293+
directives |= cobra.ShellCompDirectiveNoSpace
1294+
file += sep
1295+
}
1296+
1297+
files = append(files, file)
1298+
}
1299+
1300+
if d.IsDir() {
1301+
return fs.SkipDir
1302+
}
1303+
1304+
return nil
1305+
})
1306+
1307+
return append(instances, files...), directives
1308+
}

cmd/incus/file.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,8 +475,17 @@ func (c *cmdFilePull) Command() *cobra.Command {
475475

476476
cmd.Flags().BoolVarP(&c.file.flagMkdir, "create-dirs", "p", false, i18n.G("Create any directories necessary"))
477477
cmd.Flags().BoolVarP(&c.file.flagRecursive, "recursive", "r", false, i18n.G("Recursively transfer files"))
478+
478479
cmd.RunE = c.Run
479480

481+
cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
482+
if len(args) == 0 {
483+
return c.global.cmpFiles(toComplete, false)
484+
}
485+
486+
return c.global.cmpFiles(toComplete, true)
487+
}
488+
480489
return cmd
481490
}
482491

@@ -697,8 +706,17 @@ func (c *cmdFilePush) Command() *cobra.Command {
697706
cmd.Flags().IntVar(&c.file.flagUID, "uid", -1, i18n.G("Set the file's uid on push")+"``")
698707
cmd.Flags().IntVar(&c.file.flagGID, "gid", -1, i18n.G("Set the file's gid on push")+"``")
699708
cmd.Flags().StringVar(&c.file.flagMode, "mode", "", i18n.G("Set the file's perms on push")+"``")
709+
700710
cmd.RunE = c.Run
701711

712+
cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
713+
if len(args) == 0 {
714+
return nil, cobra.ShellCompDirectiveDefault
715+
}
716+
717+
return c.global.cmpFiles(toComplete, true)
718+
}
719+
702720
return cmd
703721
}
704722

0 commit comments

Comments
 (0)