跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

tab_completion.go

123 行 · 2.9 KB历史下载
1// SPDX-FileCopyrightText: Copyright The Lima Authors
2// SPDX-License-Identifier: Apache-2.0
3
4package main
5
6import (
7 "reflect"
8 "strings"
9 "sync"
10
11 "github.com/spf13/cobra"
12
13 "github.com/lima-vm/lima/v2/pkg/limatype"
14)
15
16var (
17 setFlagProperties []string
18 setFlagPropertiesOnce sync.Once
19)
20
21func getSetFlagProperties() []string {
22 setFlagPropertiesOnce.Do(func() {
23 setFlagProperties = extractYAMLPaths(reflect.TypeFor[limatype.LimaYAML](), "", 0)
24 })
25 return setFlagProperties
26}
27
28// extractYAMLPaths recursively traverses a struct, reading `yaml` tags
29// Slice and map element types are not traversed; array-index paths like
30// .mounts[0].mountPoint must be typed manually by the user.
31func extractYAMLPaths(t reflect.Type, prefix string, depth int) []string {
32 var paths []string
33
34 // Limit recursion to top-level and one level deep to avoid autocomplete noise
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 // Recurse into nested structs to get deep paths like .ssh.localPort
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
82var 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
99func 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}