_golang 8.5 KB

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