Skip to content

Commit 034587e

Browse files
authored
Merge pull request #3465 from songponssw/fix-hiding-private-snippets
fix: hiding template private snippets from bashCompletion
2 parents 3fea800 + 23c85f5 commit 034587e

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

cmd/limactl/completion.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
package main
55

66
import (
7+
"strings"
8+
79
"github.com/lima-vm/lima/pkg/store"
810
"github.com/lima-vm/lima/pkg/templatestore"
911
"github.com/spf13/cobra"
@@ -17,11 +19,25 @@ func bashCompleteInstanceNames(_ *cobra.Command) ([]string, cobra.ShellCompDirec
1719
return instances, cobra.ShellCompDirectiveNoFileComp
1820
}
1921

20-
func bashCompleteTemplateNames(_ *cobra.Command) ([]string, cobra.ShellCompDirective) {
22+
func bashCompleteTemplateNames(_ *cobra.Command, toComplete string) ([]string, cobra.ShellCompDirective) {
2123
var comp []string
2224
if templates, err := templatestore.Templates(); err == nil {
2325
for _, f := range templates {
24-
comp = append(comp, "template://"+f.Name)
26+
name := "template://" + f.Name
27+
if !strings.HasPrefix(name, toComplete) {
28+
continue
29+
}
30+
if len(toComplete) == len(name) {
31+
comp = append(comp, name)
32+
continue
33+
}
34+
35+
// Skip private snippets (beginning with '_') from completion.
36+
if (name[len(toComplete)-1] == '/') && (name[len(toComplete)] == '_') {
37+
continue
38+
}
39+
40+
comp = append(comp, name)
2541
}
2642
}
2743
return comp, cobra.ShellCompDirectiveDefault

cmd/limactl/start.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -454,12 +454,12 @@ func startAction(cmd *cobra.Command, args []string) error {
454454
return instance.Start(ctx, inst, "", launchHostAgentForeground)
455455
}
456456

457-
func createBashComplete(cmd *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
458-
return bashCompleteTemplateNames(cmd)
457+
func createBashComplete(cmd *cobra.Command, _ []string, toComplete string) ([]string, cobra.ShellCompDirective) {
458+
return bashCompleteTemplateNames(cmd, toComplete)
459459
}
460460

461-
func startBashComplete(cmd *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
461+
func startBashComplete(cmd *cobra.Command, _ []string, toComplete string) ([]string, cobra.ShellCompDirective) {
462462
compInst, _ := bashCompleteInstanceNames(cmd)
463-
compTmpl, _ := bashCompleteTemplateNames(cmd)
463+
compTmpl, _ := bashCompleteTemplateNames(cmd, toComplete)
464464
return append(compInst, compTmpl...), cobra.ShellCompDirectiveDefault
465465
}

0 commit comments

Comments
 (0)