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 e1b9e63 commit b628f2f
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 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 @@ -948,7 +948,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 +959,7 @@ function _nix_completion () {
break
;;
show-console-output)
main_options=(':_known_hosts')
main_options=(':->nixops_machines')
break
;;
export)
Expand All @@ -986,6 +986,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 +1000,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 b628f2f

Please sign in to comment.