Skip to content

Commit

Permalink
Fix autocompletion of nixops machine names
Browse files Browse the repository at this point in the history
Machine names were being completed from _known_hosts. These commonly
don't align with actual machine names in a nixops deployment.

This commit introduces a new completion type for nixops machines based
on output from 'nixops info'. Furthermore, if the user had already
supplied a --state or --deployment argument, then these are respected.
  • Loading branch information
Avaq committed Mar 29, 2020
1 parent 0e24007 commit cbadcb1
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions _nix
Original file line number Diff line number Diff line change
Expand Up @@ -872,7 +872,7 @@ function _nix_completion () {
'(--deployment|-d)'{--deployment,-d}':->nixops_deployments'
'--confirm' '--debug')
local -a nixops_include_exclude=(
'--include:_known_hosts' '--exclude:_known_hosts')
'--include:->nixops_machines' '--exclude:->nixops_machines')
local -a nixops_search_path_args=('-I:->option-INCLUDE')


Expand Down Expand Up @@ -926,6 +926,9 @@ function _nix_completion () {
break
;;
check|ssh)
main_options=(
'--all'
${nixops_include_exclude[*]})
break
;;
ssh-for-each)
Expand All @@ -948,7 +951,7 @@ function _nix_completion () {
;;
show-option)
main_options=(
'--xml' ':_known_hosts' ':->nixops_option')
'--xml' ':->nixops_machines' ':->nixops_option')
break
;;
set-args)
Expand All @@ -959,7 +962,7 @@ function _nix_completion () {
break
;;
show-console-output)
main_options=(':_known_hosts')
main_options=(':->nixops_machines')
break
;;
export)
Expand All @@ -986,6 +989,12 @@ function _nix_completion () {
fi
fi

local deploymentcmd
if [[ "${opts[--deployment]}" || "${opts[-d]}" ]]; then
local deployment="${opt_args[--deployment]:-${opt_args[-d]}}"
deploymentcmd="--deployment $deployment"
fi

case "$state" in
nixops_deployments)
local -a deployments=()
Expand All @@ -994,6 +1003,13 @@ function _nix_completion () {
COMPREPLY=($(compgen -W "${deployments[*]}" -- "$cur"))
return
;;
nixops_machines)
local -a machines=()
while read -r _ name _; do machines+=("$name")
done < <(nixops info $statecmd $deploymentcmd | tail -n +10 | head -n -1)
COMPREPLY=($(compgen -W "${machines[*]}" -- "$cur"))
return
;;
esac
;;
nix)
Expand Down

0 comments on commit cbadcb1

Please sign in to comment.