Another copy of my dotfiles. Because I don't completely trust GitHub.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

439 lines
13 KiB

  1. #compdef gcloud
  2. #------------------------------------------------------------
  3. # Description:
  4. #
  5. # Completion script for Google Cloud SDK
  6. #
  7. # Author:
  8. #
  9. # * Colin Su (https://github.com/littleq0903)
  10. #
  11. # Source Code:
  12. #
  13. # https://github.com/littleq0903/gcloud-zsh-completion
  14. #
  15. #------------------------------------------------------------
  16. ## Util functions
  17. # get authorized account list
  18. __account_list ()
  19. {
  20. _wanted application expl 'Authorized Google Accounts' compadd $(command gcloud auth list 2> /dev/null | grep - | sed -e "s/^ - //g" | sed -e "s/(active)//g")
  21. }
  22. # get config variable names
  23. __variable_list ()
  24. {
  25. _wanted application expl 'gcloud :: config :: Configuration Variables' compadd $(command gcloud config list 2> /dev/null | grep "=" | sed -e "s/ = .*//g")
  26. }
  27. ## Common stuffs
  28. local -a _first_arguments
  29. _first_arguments=(
  30. # these are command groups
  31. 'auth:Manage oauth2 credentials for the Google Cloud SDK'
  32. 'components:Install, update, or remove the tools in the Google Cloud SDK'
  33. 'compute:Read and manipulate Google Compute Engine resources'
  34. 'config:View and edit Google Cloud SDK properties'
  35. 'sql:Manage Cloud SQL databases'
  36. # these are just commands
  37. 'init:Initialize a gcloud workspace in the current directory'
  38. 'interactive:Use this tool in an interactive python shell'
  39. 'version:Print version information for Cloud SDK components'
  40. )
  41. # common options
  42. common_ops=(
  43. {--project+,-p+}":Google Cloud Platform project to use for this invocation:( )"
  44. {--quiet,-q}"[Disable all interactive prompts when running gcloud commands. If input is required, defaults will be used, or an error will be raised.]"
  45. "--user-output-enabled+:Control whether user intended output is printed to the console.:(true false)"
  46. "--verbosity+:Override the default verbosity for this command. This must be a standard logging verbosity level:(debug info warning error critical none)"
  47. )
  48. ## for 'auth' command group
  49. # Commands
  50. local -a _auth_arguments
  51. _auth_arguments=(
  52. "activate-refresh-token:Get credentials via an existing refresh token"
  53. "list:List the accounts for known credentials"
  54. "login:Get credentials via Google's oauth2 web flow"
  55. "revoke:Revoke authorization for credentials"
  56. )
  57. __gcloud-auth ()
  58. {
  59. local curcontext="$curcontext" state line
  60. local -A opt_args
  61. _arguments \
  62. ':command:->command' \
  63. '*::options:->options'
  64. case $state in
  65. (command)
  66. _describe -t subcommand "gcloud :: auth Commands" _auth_arguments
  67. return
  68. ;;
  69. (options)
  70. case $line[1] in
  71. (list)
  72. _arguments \
  73. "--account+::List only credentials for one account:( )"
  74. ;;
  75. (login)
  76. # TODO: make options repeatable
  77. local args
  78. args=(
  79. "--account+:Override the account acquired from the web flow:( )"
  80. "--do-not-activate[Do not set the new credentials as active]"
  81. "--no-launch-browser[Print a URL to be copied instead of launching a web browser]"
  82. )
  83. _arguments -C $args
  84. ;;
  85. (revoke)
  86. _arguments \
  87. "--all[Revoke all known credentials]"
  88. ;;
  89. esac
  90. ;;
  91. esac
  92. }
  93. ## for 'components' command groups
  94. local -a _components_arguments
  95. _components_arguments=(
  96. 'list:Command to list the current state of installed components'
  97. 'remove:Command to remove installed components'
  98. 'restore:Command to restore a backup of a Cloud SDK installation'
  99. 'update:Command to update existing or install new components'
  100. )
  101. __gcloud-components ()
  102. {
  103. local curcontext="$curcontext" state line
  104. local -A opt_args
  105. _arguments -C \
  106. ':command:->command' \
  107. '*::options:->options'
  108. case $state in
  109. (command)
  110. _describe -t commands "gcloud :: components Commands" _components_arguments
  111. return
  112. ;;
  113. (options)
  114. case $line[1] in
  115. (list)
  116. _arguments \
  117. "--show-versions[Show version information for all components]"
  118. ;;
  119. (remove)
  120. ;;
  121. (restore)
  122. ;;
  123. (update)
  124. ;;
  125. esac
  126. ;;
  127. esac
  128. }
  129. ## for 'compute' command groups
  130. local -a _compute_arguments
  131. _compute_arguments=(
  132. "addresses:Read and manipulate Google Compute Engine addresses."
  133. "backend-services:List, create, and delete backend services."
  134. "disk-types:Read Google Compute Engine virtual disk types."
  135. "disks:Read and manipulate Google Compute Engine disks."
  136. "firewall-rules:List, create, and delete Google Compute Engine firewall rules."
  137. "forwarding-rules:Read and manipulate forwarding rules to send traffic to load balancers."
  138. "http-health-checks:Read and manipulate HTTP health checks for load balanced instances."
  139. "images:List, create, and delete Google Compute Engine images."
  140. "instances:Read and manipulate Google Compute Engine virtual machine instances."
  141. "machine-types:Read Google Compute Engine virtual machine types."
  142. "networks:List, create, and delete Google Compute Engine networks."
  143. "operations:Read and manipulate Google Compute Engine operations."
  144. "project-info:Read and manipulate project-level data like quotas and metadata."
  145. "regions:List Google Compute Engine regions."
  146. "routes:Read and manipulate routes."
  147. "snapshots:List, describe, and delete Google Compute Engine snapshots."
  148. "target-http-proxies:List, create, and delete target HTTP proxies."
  149. "target-instances:Read and manipulate Google Compute Engine virtual target instances."
  150. "target-pools:Read and manipulate Google Compute Engine target pools."
  151. "url-maps:List, create, and delete URL maps."
  152. "zones:List Google Compute Engine zones."
  153. "config-ssh:Populate SSH config files with Host entries from each instance"
  154. "copy-files:Copy files to and from Google Compute Engine virtual machines."
  155. "ssh:SSH into a virtual machine instance."
  156. )
  157. __gcloud-compute ()
  158. {
  159. local curcontext="$curcontext" state line
  160. local -A opt_args
  161. _arguments -C \
  162. ':command:->command' \
  163. '*::options:->options'
  164. case $state in
  165. (command)
  166. _describe -t commands "gcloud :: compute commands" _compute_arguments
  167. return
  168. ;;
  169. esac
  170. }
  171. ## for 'config' command groups
  172. local -a _config_arguments
  173. _config_arguments=(
  174. 'list:View Google Cloud SDK properties'
  175. 'set:Edit Google Cloud SDK properties'
  176. 'unset:Erase Google Cloud SDK properties'
  177. )
  178. __gcloud-config ()
  179. {
  180. local curcontext="$curcontext" state line
  181. local -A opt_args
  182. _arguments -C \
  183. ':command:->command' \
  184. '*::options:->options'
  185. case $state in
  186. (command)
  187. _describe -t commands "gcloud :: config Commands" _config_arguments
  188. return
  189. ;;
  190. (options)
  191. case $line[1] in
  192. (list)
  193. _arguments \
  194. "--all[List all set and unset properties that match the arguments]" \
  195. {--section+,-s+}":The section whose properties shall be listed:( )"
  196. ;;
  197. (set)
  198. # FEATURE: gcloud config set <property>, will complete the property names
  199. local -a _config_set_arguments
  200. _config_set_arguments=(
  201. "--global-only[Set the option in the global properties file]"
  202. {--section+,-s+}":The section containing the option to be set:( )"
  203. )
  204. _arguments -C \
  205. $_config_set_arguments \
  206. '1:feature:__variable_list' \
  207. '*:value:->value'
  208. case $state in
  209. (value)
  210. case $line[1] in
  211. (account)
  212. # FEATURE: gcloud config set account <authed_account>
  213. # when `config set account <cursor>`, completing by authroized accounts
  214. _arguments -C \
  215. '2:feature:__account_list'
  216. ;;
  217. esac
  218. ;;
  219. esac
  220. ;;
  221. (unset)
  222. # FEATURE: gcloud config unset <property>, will complete the property names
  223. local -a _config_unset_arguments
  224. _config_unset_arguments=(
  225. "--global-only[Unset the option in the global properties file]"
  226. {--section+,-s+}":The section containing the option to be unset:( )"
  227. )
  228. _arguments \
  229. $_config_unset_arguments \
  230. '1:feature:__variable_list'
  231. ;;
  232. esac
  233. ;;
  234. esac
  235. }
  236. ## for 'sql' command groups
  237. local -a _sql_arguments
  238. _sql_arguments=(
  239. 'backups:Provide commands for working with backups of Cloud SQL instances'
  240. 'instances:Provide commands for managing Cloud SQL instances'
  241. 'operations:Provide commands for working with Cloud SQL instance operations'
  242. 'ssl-certs:Provide commands for managing SSL certificates of Cloud SQL instances'
  243. 'tiers:Provide a command to list tiers'
  244. )
  245. __gcloud-sql ()
  246. {
  247. local curcontext="$curcontext" state line
  248. local -A opt_args
  249. _arguments -C \
  250. ':command:->command' \
  251. '*::options:->options'
  252. case $state in
  253. (command)
  254. _describe -t commands "gcloud :: sql Commands" _sql_arguments
  255. return
  256. ;;
  257. (options)
  258. case $line[1] in
  259. (backups)
  260. _sql_backups_arguments=(
  261. "get:Retrieves information about a backup"
  262. "list:Lists all backups associated with a given instance"
  263. )
  264. _arguments \
  265. ':command:->command' \
  266. {--instance+,-i+}":Cloud SQL instance ID:( )" \
  267. '*::options:->options'
  268. case $state in
  269. (command)
  270. local -a _sql_backups_arguments
  271. _describe -t commands "gcloud :: sql :: backup Commands" _sql_backups_arguments
  272. ;;
  273. esac
  274. ;;
  275. (instances)
  276. # too many optional options in here, cry ;(
  277. _sql_instances_arguments=(
  278. 'create:Creates a new Cloud SQL instance'
  279. 'delete:Deletes a Cloud SQL instance'
  280. 'export:Exports data from a Cloud SQL instance'
  281. 'get:Retrieves information about a Cloud SQL instance'
  282. 'import:Imports data into a Cloud SQL instance from Google Cloud Storage'
  283. 'list:Lists Cloud SQL instances in a given project'
  284. 'patch:Updates the settings of a Cloud SQL instance'
  285. 'reset-ssl-config:Deletes all client certificates and generates a new server certificate'
  286. 'restart:Restarts a Cloud SQL instance'
  287. 'restore-backup:Restores a backup of a Cloud SQL instance'
  288. 'set-root-password:Sets the password of the MySQL root user'
  289. )
  290. _arguments \
  291. ':command:->command' \
  292. '*::options:->options'
  293. case $state in
  294. (command)
  295. _describe -t commands "gcloud :: sql :: instances Commands" _sql_instances_arguments
  296. ;;
  297. esac
  298. ;;
  299. (operations)
  300. local -a _sql_operations_arguments
  301. _sql_operations_arguments=(
  302. "get:Retrieves information about a Cloud SQL instance operation."
  303. "list:Lists all instance operations for the given Cloud SQL instance"
  304. )
  305. _arguments \
  306. ':command:->command' \
  307. {--instance+,-i+}":Cloud SQL instance ID:( )" \
  308. '*::options:->options'
  309. case $state in
  310. (command)
  311. _describe -t commands "gcloud :: sql :: operations Commands" _sql_backups_arguments
  312. ;;
  313. esac
  314. ;;
  315. (ssl-certs)
  316. local -a _sql_sslcerts_arguments
  317. _sql_sslcerts_arguments=(
  318. 'create:Creates an SSL certificate for a Cloud SQL instance'
  319. 'delete:Deletes an SSL certificate for a Cloud SQL instance'
  320. 'get:Retrieves information about an SSL cert for a Cloud SQL instance'
  321. 'list:Lists all SSL certs for a Cloud SQL instance'
  322. )
  323. _arguments \
  324. ':command:->command' \
  325. {--instance+,-i+}":Cloud SQL instance ID:( )" \
  326. '*::options:->options'
  327. case $state in
  328. (command)
  329. _describe -t commands "gcloud :: sql :: ssl-certs Commands" _sql_sslcerts_arguments
  330. ;;
  331. esac
  332. ;;
  333. (tiers)
  334. local -a _sql_tiers_arguments
  335. _sql_tiers_arguments=(
  336. "list:Lists all available service tiers for Google Cloud SQL"
  337. )
  338. _arguments \
  339. ':command:->command' \
  340. '*::options:->options'
  341. case $state in
  342. (command)
  343. _describe -t commands "gcloud :: sql :: tiers Commands" _sql_tiers_arguments
  344. ;;
  345. esac
  346. ;;
  347. esac
  348. ;;
  349. esac
  350. }
  351. ## Top-level completion function
  352. local expl
  353. local curcontext="$curcontext" state line
  354. local -A opt_args
  355. _arguments -C \
  356. $common_ops \
  357. ':command:->command' \
  358. '*::options:->options'
  359. case $state in
  360. (command)
  361. _describe -t commands "Google Cloud SDK Commands" _first_arguments
  362. ;;
  363. (options)
  364. # subcommands
  365. case $line[1] in
  366. (auth)
  367. __gcloud-auth
  368. ;;
  369. (compute)
  370. __gcloud-compute
  371. ;;
  372. (components)
  373. __gcloud-components
  374. ;;
  375. (config)
  376. __gcloud-config
  377. ;;
  378. (sql)
  379. __gcloud-sql
  380. ;;
  381. esac
  382. ;;
  383. esac
  384. return 0
  385. # Local Variables:
  386. # mode: Shell-Script
  387. # sh-indentation: 2
  388. # indent-tabs-mode: nil
  389. # sh-basic-offset: 2
  390. # End:
  391. # vim: ft=zsh sw=2 ts=2 et