salt_common.fish 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. # salt-call completion for fish shell
  2. # This file contains common options and helper functions.
  3. # README:
  4. # Completion lines are structured as a table to make it easier edit them with
  5. # vim or similar editors. Long lines (that are longer than the completion line
  6. # until "-d 'help message'") are splitted. Descriptions are not splitted.
  7. # TAB width is set to 4 chars!
  8. # Completion lines are sorted by groups, in groups they are sorted by long
  9. # option name (by alphabet).
  10. # If you want to add some completions for arguments value you probably want to
  11. # add line into __fish_salt_args_types variable. First column is the name of
  12. # argument (_ is for unnamed arguments), second is the name of the function,
  13. # last one is the type of the completion (you can use any types that have
  14. # corresponding function __fish_salt_list_TYPE).
  15. #
  16. # VERSION:
  17. # Generated from the help of salt programs on commit ad89a752f807d5ea00d3a9b3257d283ef6b69c10
  18. #
  19. # ISSUES:
  20. # TODO: #1 add: salt-api salt-cloud salt-ssh
  21. # TODO: #2 write tests (use https://github.com/terlar/fish-tank)
  22. # TODO: #3 add completion for builtin states
  23. # TODO: #4 use caching (see https://github.com/saltstack/salt/issues/15321)
  24. # TODO: #5 add help to the positional arguments (like '(Minion)', '(Grain)')
  25. # using __fish_salt_list function everythere)
  26. # TODO: #6 add minion selection by grains (call "salt '*' grains.ls", use #4)
  27. # BUG: #7 salt-call autocompletion and salt packages not works; it hangs. Ask
  28. # fish devs?
  29. # TODO: #8 sort with `sort` or leave as is?
  30. # common general options (from --help)
  31. set -l salt_programs \
  32. salt salt-call salt-cp salt-key salt-master salt-minion \
  33. salt-run salt-syndic
  34. for program in $salt_programs
  35. complete -c $program -f -l version -d "show program's version number and exit"
  36. complete -c $program -f -l versions-report -d "show program's dependencies version number and exit"
  37. complete -c $program -f -s h -l help -d "show help message and exit"
  38. complete -c $program -r -s c -l config-dir -d "Pass in an alternative configuration directory. Default: /etc/salt"
  39. # BUG: (log file is different for different programs)
  40. complete -c $program -r -l log-file -d "Log file path. Default: /var/log/salt/master."
  41. complete -c $program -x -l log-file-level -d "Logfile logging log level. Default: \"warning\"." -a "all garbage trace debug info warning error critical quiet"
  42. complete -c $program -x -s l -l log-level -d "logging log level. Default: \"warning\"." -a "all garbage trace debug info warning error critical quiet"
  43. end
  44. set -l salt_programs_crash salt salt-call salt-cp \
  45. salt-key salt-run
  46. for program in $salt_programs_crash
  47. complete -c $program -f -l hard-crash -d "Raise any original exception rather than exiting gracefully. Default: False"
  48. end
  49. set -l salt_programs_output_color salt salt-call \
  50. salt-key salt-run
  51. for program in $salt_programs_output_color
  52. for color in color colour
  53. complete -c $program -f -l force-$color -d "Force colored output"
  54. complete -c $program -f -l no-$color -d "Disable all colored output"
  55. end
  56. end
  57. set -l salt_programs_output salt salt-call salt-key
  58. for program in $salt_programs_output
  59. for out in out output
  60. complete -c $program -x -l $out -d "Print the output from the \"$program\" command using the specified outputter" -a "raw compact no_return grains overstatestage pprint json nested yaml highstate quiet key txt virt_query newline_values_only"
  61. complete -c $program -r -l $out-file -d "Write the output to the specified file"
  62. complete -c $program -x -l $out-file-append -d "Append the output to the specified file"
  63. complete -c $program -x -l $out-indent -d "Print the output indented by the provided value in spaces. Negative values disables indentation. Only applicable in outputters that support indentation."
  64. end
  65. end
  66. set -l salt_programs_doc salt salt-call salt-run
  67. for program in $salt_programs_doc
  68. for doc in doc documentation
  69. complete -c $program -f -s d -l $doc -d "Display documentation for runners, pass a runner or runner.function to see documentation on only that runner or function."
  70. end
  71. end
  72. set -l salt_programs_select salt salt-cp
  73. for program in $salt_programs_select
  74. complete -c $program -f -s G -l grain -d "Instead of using shell globs to evaluate the target use a grain value to identify targets, the syntax for the target is the grain key followed by a globexpression: \"os:Arch*\""
  75. complete -c $program -f -l grain-pcre -d "Instead of using shell globs to evaluate the target use a grain value to identify targets, the syntax for the target is the grain key followed by a pcre regular expression: \"os:Arch.*\""
  76. complete -c $program -f -s L -l list -d "Instead of using shell globs to evaluate the target servers, take a comma or whitespace delimited list of servers."
  77. complete -c $program -f -s N -l nodegroup -d "Instead of using shell globs to evaluate the target use one of the predefined nodegroups to identify a list of targets."
  78. complete -c $program -f -s E -l pcre -d "Instead of using shell globs to evaluate the target servers, use pcre regular expressions"
  79. complete -c $program -f -s R -l range -d "Instead of using shell globs to evaluate the target use a range expression to identify targets. Range expressions look like %cluster"
  80. end
  81. set -l salt_programs_user_daemon_pidfile \
  82. salt-master salt-minion salt-syndic
  83. for program in $salt_programs_user_daemon_pidfile
  84. complete -c $program -x -s u -l user -d "Specify user to run $program"
  85. complete -c $program -f -s d -l daemon -d "Run the $program as a daemon"
  86. complete -c $program -l pid-file -d "Specify the location of the pidfile. Default: /var/run/$program.pid."
  87. end
  88. function __fish_salt_default_timeout
  89. echo (echo $argv[1] | sed '
  90. s/^salt$/5/g;
  91. s/^salt-call$/60/g;
  92. s/^salt-cp$/5/g;
  93. s/^salt-run$/1/g
  94. ')
  95. end
  96. set -l salt_programs_timeout salt salt-call salt-cp \
  97. salt-run
  98. for program in $salt_programs_timeout
  99. complete -c $program -x -s t -l timeout -d "Change the timeout, if applicable, for the running command; default="(__fish_salt_default_timeout $program)
  100. end
  101. set -l salt_programs_return salt salt-cp
  102. for program in $salt_programs_return
  103. complete -c $program -x -l return -d "Set an alternative return method. By default salt will send the return data from the command back to the master, but the return data can be redirected into any number of systems, databases or applications."
  104. end
  105. # convinience functions
  106. function __fish_salt_log
  107. echo $argv >&2
  108. end
  109. function __fish_salt_join
  110. # remove empty elements
  111. set a (echo $argv[2..-1] | sed 's/ /\n/g' | grep -Ev '^$')
  112. set delimiter $argv[1]
  113. printf "$delimiter%s" $a | cut -c 2-
  114. end
  115. function __fish_salt_clean_prefix
  116. set prefix '^'$argv[1]
  117. grep -E $prefix | sed "s/$prefix//g"
  118. end
  119. function __fish_salt_clean
  120. if [ $argv[1] = yaml ]
  121. __fish_salt_clean_prefix ' *- '
  122. else if [ $argv[1] = nested ]
  123. __fish_salt_clean_prefix ' *'
  124. end
  125. end
  126. set -g __fish_salt_max_line_count_in_yaml_block 1024
  127. function __fish_salt_lines_between
  128. set max $__fish_salt_max_line_count_in_yaml_block
  129. grep -A$max $argv[1] | grep -B$max $argv[2]
  130. end
  131. function __fish_salt_extract_first_yaml_block
  132. set max $__fish_salt_max_line_count_in_yaml_block
  133. sed '1d' | sed '$a\ stop' | grep -m 1 -B$max '^ \w' | sed '$d'
  134. end
  135. function __fish_salt_add_help
  136. sed "s/\$/\t$argv/"
  137. end
  138. function __fish_salt_underscore_to_space
  139. sed 's/^\w/\u&/g; s/_/ /g'
  140. end
  141. # information extraction from commandline
  142. set -g __fish_salt_default_program 'salt'
  143. # BUG: Completion doesn't work with correct commandline like
  144. # salt --out raw server test.ping
  145. # Consider rewriting using __fish_complete_subcommand
  146. function __fish_salt_program
  147. if status --is-interactive
  148. set result (commandline -pco)
  149. if test -n "$result"
  150. if [ $result[1] = 'salt-call' ]; and contains -- '--local' $result
  151. set options '--local'
  152. end
  153. set result $result[1] $options
  154. end
  155. end
  156. set result $__fish_salt_default_program
  157. echo $result
  158. end
  159. function __fish_salt_save_first_commandline_token_not_matching_args_to
  160. if status --is-interactive
  161. set -l cli (commandline -pco)
  162. for i in $cli
  163. if echo "$i" | grep -Ev (__fish_salt_join '|' $argv)
  164. set -g $argv[1] $i
  165. return 0
  166. end
  167. end
  168. end
  169. return 1
  170. end
  171. function __fish_salt_commandline_tokens_not_matching_args
  172. if status --is-interactive
  173. set tokens (commandline -pco)
  174. set result 1
  175. for token in $tokens
  176. if echo "$token" | grep -Ev (__fish_salt_join '|' $argv)
  177. set result 0
  178. end
  179. end
  180. end
  181. return $result
  182. end
  183. set __fish_salt_base_ignores (__fish_salt_join '|' $salt_programs '^-.*')
  184. function __fish_salt_ignores_minion
  185. echo $__fish_salt_base_ignores
  186. end
  187. function __fish_salt_extract_minion
  188. __fish_salt_save_first_commandline_token_not_matching_args_to __fish_salt_extracted_minion (__fish_salt_ignores_minion)
  189. end
  190. function __fish_salt_minion
  191. __fish_salt_extract_minion > /dev/null
  192. echo $__fish_salt_extracted_minion
  193. end
  194. function __fish_salt_ignores_function
  195. __fish_salt_join '|' $__fish_salt_base_ignores (__fish_salt_minion)
  196. end
  197. function __fish_salt_extract_function
  198. __fish_salt_save_first_commandline_token_not_matching_args_to __fish_salt_extracted_function (__fish_salt_ignores_function)
  199. end
  200. function __fish_salt_function
  201. __fish_salt_extract_function > /dev/null
  202. echo $__fish_salt_extracted_function
  203. end
  204. function __fish_salt_ignores_args
  205. __fish_salt_join '|' (__fish_salt_ignores_function) (__fish_salt_function)
  206. end
  207. function __fish_salt_args
  208. __fish_salt_commandline_tokens_not_matching_args (__fish_salt_ignores_args)
  209. end
  210. set __fish_salt_arg_name_re '\w*='
  211. function __fish_salt_arg_name
  212. set result (commandline -ct | grep -E --only-matching $__fish_salt_arg_name_re)
  213. if test -z $result
  214. set result '_='
  215. end
  216. echo $result | sed 's/=$//g'
  217. end
  218. function __fish_salt_arg_value
  219. commandline -ct | sed "s/$__fish_salt_arg_name_re//g"
  220. end
  221. function __fish_salt_arg_value_by_name
  222. set arg_name "$argv="
  223. __fish_salt_args | __fish_salt_clean_prefix $arg_name
  224. end
  225. # getting info from salt
  226. set -g __fish_salt_format_options --no-color --log-level=quiet
  227. function __fish_salt_exec
  228. set -l program (__fish_salt_program)
  229. set -l exe $program $__fish_salt_format_options $__fish_salt_format_options_temp
  230. if [ $program = salt ]
  231. set exe $exe (__fish_salt_minion)
  232. end
  233. eval $exe $argv
  234. end
  235. function __fish_salt_exec_output
  236. set -g __fish_salt_format_options_temp "--output=$argv[1]"
  237. __fish_salt_exec $argv[2..-1]
  238. set -e __fish_salt_format_options_temp
  239. end
  240. function __fish_salt_exec_and_clean
  241. __fish_salt_exec_output $argv | __fish_salt_clean $argv[1]
  242. end
  243. function __fish_salt_list
  244. begin
  245. for arg_type in $argv
  246. set f_list '__fish_salt_list_'$arg_type
  247. eval $f_list | __fish_salt_add_help (echo $arg_type | __fish_salt_underscore_to_space)
  248. end
  249. end
  250. end
  251. set -g __fish_salt_args_types '
  252. _ cmd.retcode : minion_cmd
  253. cmd cmd.retcode : minion_cmd
  254. shell cmd.retcode : minion_file
  255. _ cmd.run : minion_cmd
  256. cmd cmd.run : minion_cmd
  257. shell cmd.run : minion_file
  258. _ cmd.run_all : minion_cmd
  259. cmd cmd.run_all : minion_cmd
  260. shell cmd.run_all : minion_file
  261. _ cmd.run_stderr : minion_cmd
  262. cmd cmd.run_stderr : minion_cmd
  263. shell cmd.run_stderr : minion_file
  264. _ cmd.run_stdout : minion_cmd
  265. cmd cmd.run_stdout : minion_cmd
  266. shell cmd.run_stdout : minion_file
  267. shell cmd.script : minion_file
  268. shell cmd.script_retcode : minion_file
  269. _ cmd.which : minion_cmd
  270. cmd cmd.which : minion_cmd
  271. _ cp.get_dir : master_file
  272. _ cp.get_dir : minion_file
  273. path cp.get_dir : master_file
  274. dest cp.get_dir : minion_file
  275. _ cp.get_file : master_file
  276. _ cp.get_file : minion_file
  277. path cp.get_file : master_file
  278. dest cp.get_file : minion_file
  279. _ file.copy : minion_file
  280. src file.copy : minion_file
  281. dst file.copy : minion_file
  282. _ grains.append : grain
  283. key grains.append : grain
  284. _ grains.delval : grain
  285. key grains.delval : grain
  286. _ grains.get : grain
  287. key grains.get : grain
  288. _ grains.get_or_set_hash : grain
  289. name grains.get_or_set_hash : grain
  290. _ grains.has_value : grain
  291. key grains.has_value : grain
  292. _ grains.item : grain
  293. _ grains.items : grain
  294. _ grains.remove : grain
  295. key grains.remove : grain
  296. _ grains.setval : grain
  297. key grains.setval : grain
  298. exclude state.highstate : state
  299. _ state.sls : state
  300. _ state.show_sls : state
  301. _ state.sls : state
  302. exclude state.sls : state
  303. _ sys.argspec : function
  304. _ sys.argspec : module
  305. module sys.argspec : module
  306. _ sys.doc : function
  307. _ sys.doc : module
  308. '
  309. #_ pkg.remove : package
  310. function __fish_salt_argspec_function
  311. set function_line '^\s*'$argv[1]:
  312. set max $__fish_salt_max_line_count_in_yaml_block
  313. grep -A$max $function_line | __fish_salt_extract_first_yaml_block
  314. end
  315. function __fish_salt_argspec_args
  316. __fish_salt_lines_between '^\s*args:' '^\s*defaults:' | grep -v ':'
  317. end
  318. function __fish_salt_list_arg_name
  319. __fish_salt_exec_output yaml sys.argspec (__fish_salt_function) | __fish_salt_argspec_function (__fish_salt_function) | __fish_salt_argspec_args | __fish_salt_clean yaml | sed 's/$/=/g'
  320. end
  321. function __fish_salt_list_arg_value
  322. set arg_path_re (__fish_salt_arg_name)'\s*'(__fish_salt_function)'\s*:\s*'
  323. set arg_types (echo $__fish_salt_args_types | __fish_salt_clean_prefix $arg_path_re)
  324. __fish_salt_list $arg_types
  325. end
  326. function __fish_salt_list_function
  327. __fish_salt_exec_and_clean yaml sys.list_functions $argv
  328. end
  329. function __fish_salt_list_grain
  330. __fish_salt_exec_and_clean yaml grains.ls $argv
  331. end
  332. function __fish_salt_list_master_file_abs
  333. __fish_salt_exec_and_clean yaml cp.list_master
  334. end
  335. function __fish_salt_list_master_file
  336. __fish_salt_list_master_file_abs | sed 's/^/salt:\/\//g'
  337. end
  338. function __fish_salt_list_minion
  339. salt-key --no-color --list=$argv[1] | grep -Ev '^(Accepted|Unaccepted|Rejected) Keys:$'
  340. end
  341. function __fish_salt_list_minion_cmd
  342. set cmd (__fish_salt_arg_value | sed 's/^[\'"]//')
  343. set complete_cmd_exe '"complete --do-complete=\''$cmd'\'"'
  344. set cmd_without_last_word (echo $cmd | sed -E 's/\S*$//')
  345. # BUG: Static paths. Do we need to use which?
  346. set bash_shell '/bin/bash'
  347. set fish_shell '/usr/bin/fish'
  348. set sh_shell '/bin/sh'
  349. set zsh_shell '/usr/bin/zsh'
  350. set shell (__fish_salt_arg_value_by_name shell); and test -z $shell; and set shell $sh_shell
  351. switch $shell
  352. case $fish_shell
  353. __fish_salt_exec_and_clean nested cmd.run shell=$fish_shell cmd=$complete_cmd_exe | awk -v prefix="$cmd_without_last_word" '{print prefix $0}'
  354. case $bash_shell $zsh_shell
  355. # Not implemented; See
  356. # https://github.com/fish-shell/fish-shell/issues/1679#issuecomment-55487388
  357. case $sh_shell
  358. # sh doesn't have completions
  359. end
  360. end
  361. function __fish_salt_list_minion_file
  362. if [ (count $argv) -eq 0 ]
  363. set file (__fish_salt_arg_value)
  364. else
  365. set file $argv[1]
  366. end
  367. set exe '"ls --directory --file-type '$file'* 2> /dev/null"'
  368. __fish_salt_exec_output nested cmd.run $exe | __fish_salt_clean nested
  369. end
  370. function __fish_salt_list_module
  371. __fish_salt_exec_and_clean yaml sys.list_modules $argv
  372. end
  373. function __fish_salt_list_package
  374. __fish_salt_exec_and_clean yaml pkg.list_pkgs $argv | sed 's/:.*//g'
  375. end
  376. function __fish_salt_list_state
  377. __fish_salt_list_master_file_abs | grep '.sls' | sed 's/\//./g;s/\.init\.sls/.sls/g;s/\.sls//g'
  378. end
  379. function __fish_salt_prefix_with_arg_name
  380. set arg_name (__fish_salt_arg_name)
  381. if [ $arg_name != '_' ]
  382. sed "p;s/^/$arg_name=/g"
  383. else
  384. # leave stdout as is; don't remove this line, because if construction
  385. # clears stdout if condition fails
  386. tee
  387. end
  388. end