From cbadcb10d096ef5397bf2b503ee0f120d94b4f77 Mon Sep 17 00:00:00 2001 From: Aldwin Vlasblom Date: Sun, 29 Mar 2020 19:20:11 +0200 Subject: [PATCH] Fix autocompletion of nixops machine names 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. --- _nix | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/_nix b/_nix index ad5c838..3cb8269 100644 --- a/_nix +++ b/_nix @@ -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') @@ -926,6 +926,9 @@ function _nix_completion () { break ;; check|ssh) + main_options=( + '--all' + ${nixops_include_exclude[*]}) break ;; ssh-for-each) @@ -948,7 +951,7 @@ function _nix_completion () { ;; show-option) main_options=( - '--xml' ':_known_hosts' ':->nixops_option') + '--xml' ':->nixops_machines' ':->nixops_option') break ;; set-args) @@ -959,7 +962,7 @@ function _nix_completion () { break ;; show-console-output) - main_options=(':_known_hosts') + main_options=(':->nixops_machines') break ;; export) @@ -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=() @@ -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)