Skip to content

Commit c567c36

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

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

cmd/incus/completion.go

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

33
import (
44
"fmt"
5+
"os"
6+
"path/filepath"
57
"regexp"
68
"strings"
79

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

12231225
return volumes, cobra.ShellCompDirectiveNoFileComp
12241226
}
1227+
1228+
func (g *cmdGlobal) cmpFiles(toComplete string, includeLocalFiles bool) ([]string, cobra.ShellCompDirective) {
1229+
instances, directives := g.cmpInstances(toComplete)
1230+
for i := range instances {
1231+
inst := instances[i]
1232+
if strings.HasSuffix(inst, ":") {
1233+
continue
1234+
}
1235+
1236+
instances[i] = inst + "/"
1237+
}
1238+
1239+
if len(instances) > 0 {
1240+
directives |= cobra.ShellCompDirectiveNoSpace
1241+
}
1242+
1243+
if !includeLocalFiles {
1244+
return instances, directives
1245+
}
1246+
1247+
files, _ := filepath.Glob(toComplete + "*")
1248+
for i := range files {
1249+
file := files[i]
1250+
info, err := os.Stat(file)
1251+
if err != nil || !info.IsDir() {
1252+
continue
1253+
}
1254+
1255+
files[i] = file + string(filepath.Separator)
1256+
directives |= cobra.ShellCompDirectiveNoSpace
1257+
}
1258+
1259+
return append(instances, files...), directives
1260+
}

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)