Skip to content

Commit a6fbf57

Browse files
committed
incus: Improve instance and remote names completion
The completion functions for instance and remote names now only return names prefixed by the string to be completed. This allows the caller of these functions to improve their decisions about their own completion behaviour. Signed-off-by: montag451 <[email protected]>
1 parent c2f4950 commit a6fbf57

File tree

14 files changed

+47
-35
lines changed

14 files changed

+47
-35
lines changed

cmd/incus/cluster.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ func (c *cmdClusterList) Command() *cobra.Command {
155155

156156
cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
157157
if len(args) == 0 {
158-
return c.global.cmpRemotes(false)
158+
return c.global.cmpRemotes(toComplete, false)
159159
}
160160

161161
return nil, cobra.ShellCompDirectiveNoFileComp
@@ -791,7 +791,7 @@ func (c *cmdClusterEnable) Command() *cobra.Command {
791791

792792
cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
793793
if len(args) == 0 {
794-
return c.global.cmpRemotes(false)
794+
return c.global.cmpRemotes(toComplete, false)
795795
}
796796

797797
return nil, cobra.ShellCompDirectiveNoFileComp
@@ -1095,7 +1095,7 @@ Pre-defined column shorthand chars:
10951095

10961096
cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
10971097
if len(args) == 0 {
1098-
return c.global.cmpRemotes(false)
1098+
return c.global.cmpRemotes(toComplete, false)
10991099
}
11001100

11011101
return nil, cobra.ShellCompDirectiveNoFileComp

cmd/incus/cluster_group.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ incus cluster group create g1 < config.yaml
195195

196196
cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
197197
if len(args) == 0 {
198-
return c.global.cmpRemotes(false)
198+
return c.global.cmpRemotes(toComplete, false)
199199
}
200200

201201
return nil, cobra.ShellCompDirectiveNoFileComp
@@ -481,7 +481,7 @@ Pre-defined column shorthand chars:
481481

482482
cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
483483
if len(args) == 0 {
484-
return c.global.cmpRemotes(false)
484+
return c.global.cmpRemotes(toComplete, false)
485485
}
486486

487487
return nil, cobra.ShellCompDirectiveNoFileComp

cmd/incus/completion.go

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func (g *cmdGlobal) cmpClusterGroups(toComplete string) ([]string, cobra.ShellCo
7171
}
7272

7373
if !strings.Contains(toComplete, ":") {
74-
remotes, directives := g.cmpRemotes(false)
74+
remotes, directives := g.cmpRemotes(toComplete, false)
7575
results = append(results, remotes...)
7676
cmpDirectives |= directives
7777
}
@@ -192,7 +192,7 @@ func (g *cmdGlobal) cmpClusterMembers(toComplete string) ([]string, cobra.ShellC
192192
}
193193

194194
if !strings.Contains(toComplete, ":") {
195-
remotes, directives := g.cmpRemotes(false)
195+
remotes, directives := g.cmpRemotes(toComplete, false)
196196
results = append(results, remotes...)
197197
cmpDirectives |= directives
198198
}
@@ -230,7 +230,7 @@ func (g *cmdGlobal) cmpImages(toComplete string) ([]string, cobra.ShellCompDirec
230230
}
231231

232232
if !strings.Contains(toComplete, ":") {
233-
remotes, directives := g.cmpRemotes(true)
233+
remotes, directives := g.cmpRemotes(toComplete, true)
234234
results = append(results, remotes...)
235235
cmpDirectives |= directives
236236
}
@@ -330,12 +330,16 @@ func (g *cmdGlobal) cmpInstances(toComplete string) ([]string, cobra.ShellCompDi
330330
name = fmt.Sprintf("%s:%s", resource.remote, instName)
331331
}
332332

333+
if !strings.HasPrefix(name, toComplete) {
334+
continue
335+
}
336+
333337
results = append(results, name)
334338
}
335339
}
336340

337341
if !strings.Contains(toComplete, ":") {
338-
remotes, directives := g.cmpRemotes(false)
342+
remotes, directives := g.cmpRemotes(toComplete, false)
339343
results = append(results, remotes...)
340344
cmpDirectives |= directives
341345
}
@@ -375,7 +379,7 @@ func (g *cmdGlobal) cmpInstancesAndSnapshots(toComplete string) ([]string, cobra
375379
}
376380

377381
if !strings.Contains(toComplete, ":") {
378-
remotes, directives := g.cmpRemotes(false)
382+
remotes, directives := g.cmpRemotes(toComplete, false)
379383
results = append(results, remotes...)
380384
cmpDirectives |= directives
381385
}
@@ -453,7 +457,7 @@ func (g *cmdGlobal) cmpNetworkACLs(toComplete string) ([]string, cobra.ShellComp
453457
}
454458

455459
if !strings.Contains(toComplete, ":") {
456-
remotes, directives := g.cmpRemotes(false)
460+
remotes, directives := g.cmpRemotes(toComplete, false)
457461
results = append(results, remotes...)
458462
cmpDirectives |= directives
459463
}
@@ -607,7 +611,7 @@ func (g *cmdGlobal) cmpNetworks(toComplete string) ([]string, cobra.ShellCompDir
607611
}
608612

609613
if !strings.Contains(toComplete, ":") {
610-
remotes, directives := g.cmpRemotes(false)
614+
remotes, directives := g.cmpRemotes(toComplete, false)
611615
results = append(results, remotes...)
612616
cmpDirectives |= directives
613617
}
@@ -789,7 +793,7 @@ func (g *cmdGlobal) cmpNetworkZones(toComplete string) ([]string, cobra.ShellCom
789793
}
790794

791795
if !strings.Contains(toComplete, ":") {
792-
remotes, directives := g.cmpRemotes(false)
796+
remotes, directives := g.cmpRemotes(toComplete, false)
793797
results = append(results, remotes...)
794798
cmpDirectives |= directives
795799
}
@@ -882,7 +886,7 @@ func (g *cmdGlobal) cmpProfiles(toComplete string, includeRemotes bool) ([]strin
882886
}
883887

884888
if includeRemotes && !strings.Contains(toComplete, ":") {
885-
remotes, directives := g.cmpRemotes(false)
889+
remotes, directives := g.cmpRemotes(toComplete, false)
886890
results = append(results, remotes...)
887891
cmpDirectives |= directives
888892
}
@@ -940,26 +944,34 @@ func (g *cmdGlobal) cmpProjects(toComplete string) ([]string, cobra.ShellCompDir
940944
}
941945

942946
if !strings.Contains(toComplete, ":") {
943-
remotes, directives := g.cmpRemotes(false)
947+
remotes, directives := g.cmpRemotes(toComplete, false)
944948
results = append(results, remotes...)
945949
cmpDirectives |= directives
946950
}
947951

948952
return results, cmpDirectives
949953
}
950954

951-
func (g *cmdGlobal) cmpRemotes(includeAll bool) ([]string, cobra.ShellCompDirective) {
955+
func (g *cmdGlobal) cmpRemotes(toComplete string, includeAll bool) ([]string, cobra.ShellCompDirective) {
952956
results := []string{}
953957

954958
for remoteName, rc := range g.conf.Remotes {
955959
if !includeAll && rc.Protocol != "incus" && rc.Protocol != "" {
956960
continue
957961
}
958962

963+
if !strings.HasPrefix(remoteName, toComplete) {
964+
continue
965+
}
966+
959967
results = append(results, fmt.Sprintf("%s:", remoteName))
960968
}
961969

962-
return results, cobra.ShellCompDirectiveNoSpace
970+
if len(results) > 0 {
971+
return results, cobra.ShellCompDirectiveNoSpace
972+
} else {
973+
return results, cobra.ShellCompDirectiveNoFileComp
974+
}
963975
}
964976

965977
func (g *cmdGlobal) cmpRemoteNames() ([]string, cobra.ShellCompDirective) {
@@ -1056,7 +1068,7 @@ func (g *cmdGlobal) cmpStoragePools(toComplete string) ([]string, cobra.ShellCom
10561068
}
10571069

10581070
if !strings.Contains(toComplete, ":") {
1059-
remotes, _ := g.cmpRemotes(false)
1071+
remotes, _ := g.cmpRemotes(toComplete, false)
10601072
results = append(results, remotes...)
10611073
}
10621074

cmd/incus/copy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ The pull transfer mode is the default as it is compatible with all server versio
7171
}
7272

7373
if len(args) == 1 {
74-
return c.global.cmpRemotes(false)
74+
return c.global.cmpRemotes(toComplete, false)
7575
}
7676

7777
return nil, cobra.ShellCompDirectiveNoFileComp

cmd/incus/image.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ It requires the source to be an alias and for it to be public.`))
167167
}
168168

169169
if len(args) == 1 {
170-
return c.global.cmpRemotes(false)
170+
return c.global.cmpRemotes(toComplete, false)
171171
}
172172

173173
return nil, cobra.ShellCompDirectiveNoFileComp
@@ -680,7 +680,7 @@ Directory import is only available on Linux and must be performed as root.`))
680680
}
681681

682682
if len(args) == 1 {
683-
return c.global.cmpRemotes(false)
683+
return c.global.cmpRemotes(toComplete, false)
684684
}
685685

686686
return nil, cobra.ShellCompDirectiveNoFileComp

cmd/incus/list.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ incus list -c ns,user.comment:comment
138138

139139
cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
140140
if len(args) == 0 {
141-
return c.global.cmpRemotes(false)
141+
return c.global.cmpRemotes(toComplete, false)
142142
}
143143

144144
return nil, cobra.ShellCompDirectiveNoFileComp

cmd/incus/move.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ incus move <instance>/<old snapshot name> <instance>/<new snapshot name>
7171
}
7272

7373
if len(args) == 1 {
74-
return c.global.cmpRemotes(false)
74+
return c.global.cmpRemotes(toComplete, false)
7575
}
7676

7777
return nil, cobra.ShellCompDirectiveNoFileComp

cmd/incus/network.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ incus network create bar network=baz --type ovn
354354
return nil, cobra.ShellCompDirectiveNoFileComp
355355
}
356356

357-
return c.global.cmpRemotes(false)
357+
return c.global.cmpRemotes(toComplete, false)
358358
}
359359

360360
return cmd
@@ -1079,7 +1079,7 @@ u - Used by (count)`))
10791079
return nil, cobra.ShellCompDirectiveNoFileComp
10801080
}
10811081

1082-
return c.global.cmpRemotes(false)
1082+
return c.global.cmpRemotes(toComplete, false)
10831083
}
10841084

10851085
return cmd

cmd/incus/network_acl.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func (c *cmdNetworkACLList) Command() *cobra.Command {
9999

100100
cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
101101
if len(args) == 0 {
102-
return c.global.cmpRemotes(false)
102+
return c.global.cmpRemotes(toComplete, false)
103103
}
104104

105105
return nil, cobra.ShellCompDirectiveNoFileComp

cmd/incus/network_zone.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ Pre-defined column shorthand chars:
116116

117117
cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
118118
if len(args) == 0 {
119-
return c.global.cmpRemotes(false)
119+
return c.global.cmpRemotes(toComplete, false)
120120
}
121121

122122
return nil, cobra.ShellCompDirectiveNoFileComp

cmd/incus/profile.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ func (c *cmdProfileCopy) Command() *cobra.Command {
285285
}
286286

287287
if len(args) == 1 {
288-
return c.global.cmpRemotes(false)
288+
return c.global.cmpRemotes(toComplete, false)
289289
}
290290

291291
return nil, cobra.ShellCompDirectiveNoFileComp
@@ -366,7 +366,7 @@ incus profile create p1 < config.yaml
366366

367367
cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
368368
if len(args) == 0 {
369-
return c.global.cmpRemotes(false)
369+
return c.global.cmpRemotes(toComplete, false)
370370
}
371371

372372
return nil, cobra.ShellCompDirectiveNoFileComp
@@ -728,7 +728,7 @@ u - Used By`))
728728

729729
cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
730730
if len(args) == 0 {
731-
return c.global.cmpRemotes(false)
731+
return c.global.cmpRemotes(toComplete, false)
732732
}
733733

734734
return nil, cobra.ShellCompDirectiveNoFileComp

cmd/incus/project.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ incus project create p1 < config.yaml
115115

116116
cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
117117
if len(args) == 0 {
118-
return c.global.cmpRemotes(false)
118+
return c.global.cmpRemotes(toComplete, false)
119119
}
120120

121121
return nil, cobra.ShellCompDirectiveNoFileComp
@@ -536,7 +536,7 @@ u - Used By`))
536536

537537
cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
538538
if len(args) == 0 {
539-
return c.global.cmpRemotes(false)
539+
return c.global.cmpRemotes(toComplete, false)
540540
}
541541

542542
return nil, cobra.ShellCompDirectiveNoFileComp

cmd/incus/publish.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func (c *cmdPublish) Command() *cobra.Command {
4646
}
4747

4848
if len(args) == 1 {
49-
return c.global.cmpRemotes(false)
49+
return c.global.cmpRemotes(toComplete, false)
5050
}
5151

5252
return nil, cobra.ShellCompDirectiveNoFileComp

cmd/incus/storage.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ incus create storage s1 dir < config.yaml
110110

111111
cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
112112
if len(args) == 0 {
113-
return c.global.cmpRemotes(false)
113+
return c.global.cmpRemotes(toComplete, false)
114114
}
115115

116116
return nil, cobra.ShellCompDirectiveNoFileComp
@@ -692,7 +692,7 @@ Pre-defined column shorthand chars:
692692

693693
cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
694694
if len(args) == 0 {
695-
return c.global.cmpRemotes(false)
695+
return c.global.cmpRemotes(toComplete, false)
696696
}
697697

698698
return nil, cobra.ShellCompDirectiveNoFileComp

0 commit comments

Comments
 (0)