| 1 | |
| 2 | |
| 3 | |
| 4 | package main |
| 5 | |
| 6 | import ( |
| 7 | "reflect" |
| 8 | "strings" |
| 9 | "sync" |
| 10 | |
| 11 | "github.com/spf13/cobra" |
| 12 | |
| 13 | "github.com/lima-vm/lima/v2/pkg/limatype" |
| 14 | ) |
| 15 | |
| 16 | var ( |
| 17 | setFlagProperties []string |
| 18 | setFlagPropertiesOnce sync.Once |
| 19 | ) |
| 20 | |
| 21 | func getSetFlagProperties() []string { |
| 22 | setFlagPropertiesOnce.Do(func() { |
| 23 | setFlagProperties = extractYAMLPaths(reflect.TypeFor[limatype.LimaYAML](), "", 0) |
| 24 | }) |
| 25 | return setFlagProperties |
| 26 | } |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | func extractYAMLPaths(t reflect.Type, prefix string, depth int) []string { |
| 32 | var paths []string |
| 33 | |
| 34 | |
| 35 | if depth > 1 { |
| 36 | return paths |
| 37 | } |
| 38 | |
| 39 | if t.Kind() == reflect.Pointer { |
| 40 | t = t.Elem() |
| 41 | } |
| 42 | if t.Kind() != reflect.Struct { |
| 43 | return paths |
| 44 | } |
| 45 | |
| 46 | for field := range t.Fields() { |
| 47 | if !field.IsExported() { |
| 48 | continue |
| 49 | } |
| 50 | |
| 51 | yamlTag := field.Tag.Get("yaml") |
| 52 | if yamlTag == "-" { |
| 53 | continue |
| 54 | } |
| 55 | |
| 56 | name, _, _ := strings.Cut(yamlTag, ",") |
| 57 | if name == "" && strings.Contains(yamlTag, "inline") { |
| 58 | paths = append(paths, extractYAMLPaths(field.Type, prefix, depth)...) |
| 59 | continue |
| 60 | } |
| 61 | if name == "" { |
| 62 | continue |
| 63 | } |
| 64 | |
| 65 | currentPath := prefix + "." + name |
| 66 | paths = append(paths, currentPath) |
| 67 | |
| 68 | |
| 69 | ft := field.Type |
| 70 | if ft.Kind() == reflect.Pointer { |
| 71 | ft = ft.Elem() |
| 72 | } |
| 73 | if ft.Kind() == reflect.Bool { |
| 74 | setFlagEnumValues[currentPath] = []string{"true", "false"} |
| 75 | } else if ft.Kind() == reflect.Struct { |
| 76 | paths = append(paths, extractYAMLPaths(ft, currentPath, depth+1)...) |
| 77 | } |
| 78 | } |
| 79 | return paths |
| 80 | } |
| 81 | |
| 82 | var setFlagEnumValues = map[string][]string{ |
| 83 | ".vmType": { |
| 84 | limatype.QEMU, limatype.VZ, limatype.WSL2, |
| 85 | }, |
| 86 | ".os": { |
| 87 | limatype.LINUX, limatype.DARWIN, limatype.FREEBSD, |
| 88 | }, |
| 89 | ".arch": { |
| 90 | limatype.X8664, limatype.AARCH64, limatype.ARMV7L, |
| 91 | limatype.PPC64LE, limatype.RISCV64, limatype.S390X, |
| 92 | }, |
| 93 | ".mountType": { |
| 94 | limatype.REVSSHFS, limatype.NINEP, |
| 95 | limatype.VIRTIOFS, limatype.WSLMount, |
| 96 | }, |
| 97 | } |
| 98 | |
| 99 | func setFlagCompletion(_ *cobra.Command, _ []string, toComplete string) ([]string, cobra.ShellCompDirective) { |
| 100 | properties := getSetFlagProperties() |
| 101 | noFile := cobra.ShellCompDirectiveNoSpace | cobra.ShellCompDirectiveNoFileComp |
| 102 | |
| 103 | if prop, valuePrefix, found := strings.Cut(toComplete, "="); found { |
| 104 | if values, ok := setFlagEnumValues[prop]; ok { |
| 105 | var completions []string |
| 106 | for _, v := range values { |
| 107 | if strings.HasPrefix(v, valuePrefix) { |
| 108 | completions = append(completions, prop+"="+v) |
| 109 | } |
| 110 | } |
| 111 | return completions, noFile |
| 112 | } |
| 113 | return nil, noFile |
| 114 | } |
| 115 | |
| 116 | var completions []string |
| 117 | for _, p := range properties { |
| 118 | if strings.HasPrefix(p, toComplete) { |
| 119 | completions = append(completions, p+"=") |
| 120 | } |
| 121 | } |
| 122 | return completions, noFile |
| 123 | } |