_golang
9799 bytes
1#compdef go
2
3__go_packages() {
4 local gopaths
5 declare -a gopaths
6 gopaths=("${(s/:/)$(go env GOPATH)}")
7 gopaths+=("$(go env GOROOT)")
8 for p in $gopaths; do
9 _path_files -W "$p/src" -/
10 done
11}
12
13__go_identifiers() {
14 local tmpl_path="${functions_source[$0]:h}/templates"
15 compadd $(godoc -templates "$tmpl_path" ${words[-2]} 2> /dev/null)
16}
17
18__go_tool_commands() {
19 local -a tools tool_commands
20 local -A command_seen short_count
21 local tool command
22
23 tools=("${(@f)$(go tool 2>/dev/null)}")
24
25 # Go 1.24+ lists module tools by package path, but also accepts unique
26 # default binary names for those tools.
27 for tool in "${tools[@]}"; do
28 [[ -n $tool ]] || continue
29
30 (( command_seen[$tool]++ ))
31
32 if [[ $tool == */* ]]; then
33 command=${tool:t}
34
35 if [[ $command == v[0-9]* && ${command#v} != *[^0-9]* ]] && (( ${command#v} > 1 )); then
36 command=${${tool%/$command}:t}
37 fi
38
39 (( short_count[$command]++ ))
40 fi
41 done
42
43 for tool in "${tools[@]}"; do
44 [[ -n $tool ]] || continue
45
46 tool_commands+=("$tool")
47
48 if [[ $tool == */* ]]; then
49 command=${tool:t}
50
51 if [[ $command == v[0-9]* && ${command#v} != *[^0-9]* ]] && (( ${command#v} > 1 )); then
52 command=${${tool%/$command}:t}
53 fi
54
55 if (( short_count[$command] == 1 && ! command_seen[$command] )); then
56 tool_commands+=("$command")
57 fi
58 fi
59 done
60
61 _values "go tool" "${tool_commands[@]}"
62}
63
64_go() {
65 typeset -a commands build_flags
66 commands+=(
67 'build[compile packages and dependencies]'
68 'clean[remove object files]'
69 'doc[run godoc on package sources]'
70 'env[print Go environment information]'
71 'fix[run go tool fix on packages]'
72 'fmt[run gofmt on package sources]'
73 'generate[generate Go files by processing source]'
74 'get[download and install packages and dependencies]'
75 'help[display help]'
76 'install[compile and install packages and dependencies]'
77 'list[list packages]'
78 'mod[modules maintenance]'
79 'run[compile and run Go program]'
80 'test[test packages]'
81 'tool[run specified go tool]'
82 'version[print Go version]'
83 'vet[run go tool vet on packages]'
84 )
85 if (( CURRENT == 2 )); then
86 # explain go commands
87 _values 'go tool commands' ${commands[@]}
88 return
89 fi
90 build_flags=(
91 '-a[force reinstallation of packages that are already up to date]'
92 '-n[print the commands but do not run them]'
93 '-p[number of parallel builds]:number'
94 '-race[enable data race detection]'
95 '-x[print the commands]'
96 '-work[print temporary directory name and keep it]'
97 '-ccflags[flags for 5c/6c/8c]:flags'
98 '-gcflags[flags for 5g/6g/8g]:flags'
99 '-ldflags[flags for 5l/6l/8l]:flags'
100 '-gccgoflags[flags for gccgo]:flags'
101 '-compiler[name of compiler to use]:name'
102 '-installsuffix[suffix to add to package directory]:suffix'
103 '-tags[list of build tags to consider satisfied]:tags'
104 )
105
106 case ${words[2]} in
107 doc)
108 _arguments -s -w \
109 "-c[symbol matching honors case (paths not affected)]" \
110 "-cmd[show symbols with package docs even if package is a command]" \
111 "-u[show unexported symbols as well as exported]" \
112 "2:importpaths:__go_packages" \
113 ":next identifiers:__go_identifiers"
114 ;;
115 clean)
116 _arguments -s -w \
117 "-i[remove the corresponding installed archive or binary (what 'go install' would create)]" \
118 "-n[print the remove commands it would execute, but not run them]" \
119 "-r[apply recursively to all the dependencies of the packages named by the import paths]" \
120 "-x[print remove commands as it executes them]" \
121 "*:importpaths:__go_packages"
122 ;;
123 fix|fmt|vet)
124 _alternative ':importpaths:__go_packages' ':files:_path_files -g "*.go"'
125 ;;
126 install)
127 _arguments -s -w : ${build_flags[@]} \
128 "-v[show package names]" \
129 '*:importpaths:__go_packages'
130 ;;
131 get)
132 _arguments -s -w : \
133 ${build_flags[@]}
134 ;;
135 build)
136 _arguments -s -w : \
137 ${build_flags[@]} \
138 "-v[show package names]" \
139 "-o[output file]:file:_files" \
140 "*:args:{ _alternative ':importpaths:__go_packages' ':files:_path_files -g \"*.go\"' }"
141 ;;
142 test)
143 _arguments -s -w : \
144 ${build_flags[@]} \
145 "-c[do not run, compile the test binary]" \
146 "-i[do not run, install dependencies]" \
147 "-v[print test output]" \
148 "-x[print the commands]" \
149 "-short[use short mode]" \
150 "-parallel[number of parallel tests]:number" \
151 "-cpu[values of GOMAXPROCS to use]:number list" \
152 "-run[run tests and examples matching regexp]:regexp" \
153 "-bench[run benchmarks matching regexp]:regexp" \
154 "-benchmem[print memory allocation stats]" \
155 "-benchtime[run each benchmark until taking this long]:duration" \
156 "-blockprofile[write goroutine blocking profile to file]:file" \
157 "-blockprofilerate[set sampling rate of goroutine blocking profile]:number" \
158 "-timeout[kill test after that duration]:duration" \
159 "-cpuprofile[write CPU profile to file]:file:_files" \
160 "-memprofile[write heap profile to file]:file:_files" \
161 "-memprofilerate[set heap profiling rate]:number" \
162 "*:args:{ _alternative ':importpaths:__go_packages' ':files:_path_files -g \"*.go\"' }"
163 ;;
164 list)
165 _arguments -s -w : \
166 "-f[alternative format for the list]:format" \
167 "-json[print data in json format]" \
168 "-compiled[set CompiledGoFiles to the Go source files presented to the compiler]" \
169 "-deps[iterate over not just the named packages but also all their dependencies]" \
170 "-e[change the handling of erroneous packages]" \
171 "-export[set the Export field to the name of a file containing up-to-date export information for the given package]" \
172 "-find[identify the named packages but not resolve their dependencies]" \
173 "-test[report not only the named packages but also their test binaries]" \
174 "-m[list modules instead of packages]" \
175 "-u[adds information about available upgrades]" \
176 "-versions[set the Module's Versions field to a list of all known versions of that module]:number" \
177 "*:importpaths:__go_packages"
178 ;;
179 mod)
180 local -a mod_commands
181 mod_commands+=(
182 'download[download modules to local cache]'
183 'edit[edit go.mod from tools or scripts]'
184 'graph[print module requirement graph]'
185 'init[initialize new module in current directory]'
186 'tidy[add missing and remove unused modules]'
187 'vendor[make vendored copy of dependencies]'
188 'verify[verify dependencies have expected content]'
189 'why[explain why packages or modules are needed]'
190 )
191
192 if (( CURRENT == 3 )); then
193 _values 'go mod commands' ${mod_commands[@]} "help[display help]"
194 return
195 fi
196
197 case ${words[3]} in
198 help)
199 _values 'go mod commands' ${mod_commands[@]}
200 ;;
201 download)
202 _arguments -s -w : \
203 "-json[print a sequence of JSON objects standard output]" \
204 "*:flags"
205 ;;
206 edit)
207 _arguments -s -w : \
208 "-fmt[reformat the go.mod file]" \
209 "-module[change the module's path]" \
210 "-replace[=old{@v}=new{@v} add a replacement of the given module path and version pair]:name" \
211 "-dropreplace[=old{@v}=new{@v} drop a replacement of the given module path and version pair]:name" \
212 "-go[={version} set the expected Go language version]:number" \
213 "-print[print the final go.mod in its text format]" \
214 "-json[print the final go.mod file in JSON format]" \
215 "*:flags"
216 ;;
217 graph)
218 ;;
219 init)
220 ;;
221 tidy)
222 _arguments -s -w : \
223 "-v[print information about removed modules]" \
224 "*:flags"
225 ;;
226 vendor)
227 _arguments -s -w : \
228 "-v[print the names of vendored]" \
229 "*:flags"
230 ;;
231 verify)
232 ;;
233 why)
234 _arguments -s -w : \
235 "-m[treats the arguments as a list of modules and finds a path to any package in each of the modules]" \
236 "-vendor[exclude tests of dependencies]" \
237 "*:importpaths:__go_packages"
238 ;;
239 esac
240 ;;
241 help)
242 _values "${commands[@]}" \
243 'environment[show Go environment variables available]' \
244 'gopath[GOPATH environment variable]' \
245 'packages[description of package lists]' \
246 'remote[remote import path syntax]' \
247 'testflag[description of testing flags]' \
248 'testfunc[description of testing functions]'
249 ;;
250 run)
251 _arguments -s -w : \
252 ${build_flags[@]} \
253 '*:file:_files -g "*.go"'
254 ;;
255 tool)
256 if (( CURRENT == 3 )); then
257 __go_tool_commands
258 return
259 fi
260 case ${words[3]} in
261 [568]g)
262 _arguments -s -w : \
263 '-I[search for packages in DIR]:includes:_path_files -/' \
264 '-L[show full path in file:line prints]' \
265 '-S[print the assembly language]' \
266 '-V[print the compiler version]' \
267 '-e[no limit on number of errors printed]' \
268 '-h[panic on an error]' \
269 '-l[disable inlining]' \
270 '-m[print optimization decisions]' \
271 '-o[file specify output file]:file' \
272 '-p[assumed import path for this code]:importpath' \
273 '-u[disable package unsafe]' \
274 "*:file:_files -g '*.go'"
275 ;;
276 [568]l)
277 local O=${words[3]%l}
278 _arguments -s -w : \
279 '-o[file specify output file]:file' \
280 '-L[search for packages in DIR]:includes:_path_files -/' \
281 "*:file:_files -g '*.[ao$O]'"
282 ;;
283 dist)
284 _values "dist tool" banner bootstrap clean env install version
285 ;;
286 *)
287 # use files by default
288 _files
289 ;;
290 esac
291 ;;
292 esac
293}
294
295_go "$@"