| 1 | |
| 2 | |
| 3 | |
| 4 | package main |
| 5 | |
| 6 | import ( |
| 7 | "maps" |
| 8 | "net" |
| 9 | "slices" |
| 10 | "strings" |
| 11 | |
| 12 | "github.com/spf13/cobra" |
| 13 | |
| 14 | "github.com/lima-vm/lima/v2/pkg/networks" |
| 15 | "github.com/lima-vm/lima/v2/pkg/store" |
| 16 | "github.com/lima-vm/lima/v2/pkg/templatestore" |
| 17 | ) |
| 18 | |
| 19 | func bashCompleteInstanceNames(_ *cobra.Command) ([]string, cobra.ShellCompDirective) { |
| 20 | instances, err := store.Instances() |
| 21 | if err != nil { |
| 22 | return nil, cobra.ShellCompDirectiveDefault |
| 23 | } |
| 24 | return instances, cobra.ShellCompDirectiveNoFileComp |
| 25 | } |
| 26 | |
| 27 | func bashCompleteTemplateNames(_ *cobra.Command, toComplete string) ([]string, cobra.ShellCompDirective) { |
| 28 | var comp []string |
| 29 | if templates, err := templatestore.Templates(); err == nil { |
| 30 | for _, f := range templates { |
| 31 | name := "template:" + f.Name |
| 32 | if !strings.HasPrefix(name, toComplete) { |
| 33 | continue |
| 34 | } |
| 35 | if len(toComplete) == len(name) { |
| 36 | comp = append(comp, name) |
| 37 | continue |
| 38 | } |
| 39 | |
| 40 | |
| 41 | if (name[len(toComplete)-1] == '/') && (name[len(toComplete)] == '_') { |
| 42 | continue |
| 43 | } |
| 44 | |
| 45 | comp = append(comp, name) |
| 46 | } |
| 47 | } |
| 48 | return comp, cobra.ShellCompDirectiveDefault |
| 49 | } |
| 50 | |
| 51 | func bashCompleteDiskNames(_ *cobra.Command) ([]string, cobra.ShellCompDirective) { |
| 52 | disks, err := store.Disks() |
| 53 | if err != nil { |
| 54 | return nil, cobra.ShellCompDirectiveDefault |
| 55 | } |
| 56 | return disks, cobra.ShellCompDirectiveNoFileComp |
| 57 | } |
| 58 | |
| 59 | func bashCompleteNetworkNames(_ *cobra.Command) ([]string, cobra.ShellCompDirective) { |
| 60 | config, err := networks.LoadConfig() |
| 61 | if err != nil { |
| 62 | return nil, cobra.ShellCompDirectiveDefault |
| 63 | } |
| 64 | networks := slices.Sorted(maps.Keys(config.Networks)) |
| 65 | return networks, cobra.ShellCompDirectiveNoFileComp |
| 66 | } |
| 67 | |
| 68 | func bashFlagCompleteNetworkInterfaceNames(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) { |
| 69 | intf, err := net.Interfaces() |
| 70 | if err != nil { |
| 71 | return nil, cobra.ShellCompDirectiveDefault |
| 72 | } |
| 73 | var intfNames []string |
| 74 | for _, f := range intf { |
| 75 | intfNames = append(intfNames, f.Name) |
| 76 | } |
| 77 | return intfNames, cobra.ShellCompDirectiveNoFileComp |
| 78 | } |