zsh-workers
 help / color / mirror / code / Atom feed
* Ansible tab completion: re-read cached hosts & groups on directory change
@ 2022-01-07  9:21 Moritz Bunkus
  2022-03-31  5:55 ` Lawrence Velázquez
  0 siblings, 1 reply; 3+ messages in thread
From: Moritz Bunkus @ 2022-01-07  9:21 UTC (permalink / raw)
  To: zsh-workers

[-- Attachment #1: Type: text/plain, Size: 1281 bytes --]

Hey,

zsh 5.8

we have several Ansible repositories in different directories. I often have
to switch between them.

Unfortunately the tab completion for all the Ansible commands caches known
Hosts & Groups in the global variables _ansible_hosts &
_ansible_groups. For me the effect is that the wrong set of hosts & groups
is offered when I change to a different Ansible repo.

How to reproduce:

1. Have two or more Ansible repositories with different inventories
   somewhere
2. cd into /path/to/first_ansible_tree
3. Type `anssible <TAB>`, let tab completion cache hosts & groups
4. Hit <TAB> again to verify that the offered completions match expected
   ones
5. cd into /path/to/other_ansible_tree
6. Type `ansible <TAB><TAB>` and observe that the offered completions still
   come from the first_ansible_tree, not from other_ansible_tree

As a quick workaround I've modified the completion to also cache the
working directory where _ansible_hosts & _ansible_groups were created. If
the current working directory doesn't match the cached working directory,
both variables are unset & their values cached again. This seems to work.

The attached patch implements this. Feel free to use it however you want,
or to apply a totally different solution.

Thanks.

Kind regards,
mosu

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: ansible-uncache-hosts-on-directory-change.diff --]
[-- Type: text/x-patch, Size: 912 bytes --]

--- /usr/share/zsh/functions/Completion/Unix/_ansible	2020-02-16 18:55:21.000000000 +0100
+++ /home/mosu/.config/zsh/functions/_ansible	2022-01-07 09:43:00.841820539 +0100
@@ -204,10 +204,15 @@
     else
       local -a inventory
       typeset -ga _ansible_hosts _ansible_groups
+      typeset -g _ansible_inventory_last_cwd
+      if [[ $PWD != $_ansible_inventory_last_cwd ]]; then
+        unset _ansible_hosts _ansible_groups
+      fi
       if (( !$#_ansible_hosts || !$#_ansible_groups )); then
        	inventory=( ${(f)"$(_call_program groups ansible-inventory --graph)"} )
        	_ansible_hosts=( ${${(M)inventory%--[^:]#}#--} )
 	_ansible_groups=( ${${${(M)inventory%@*:}%:}#@} )
+        _ansible_inventory_last_cwd=$PWD
       fi
       [[ $IPREFIX = *[:,] ]] &&
 	alts=( 'operators:operator:_values -S "" operator "![exclude hosts]" "&[intersection of hosts]" "~[regular expression pattern]"' )

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Ansible tab completion: re-read cached hosts & groups on directory change
  2022-01-07  9:21 Ansible tab completion: re-read cached hosts & groups on directory change Moritz Bunkus
@ 2022-03-31  5:55 ` Lawrence Velázquez
  2022-04-10 21:45   ` Daniel Shahaf
  0 siblings, 1 reply; 3+ messages in thread
From: Lawrence Velázquez @ 2022-03-31  5:55 UTC (permalink / raw)
  To: Moritz Bunkus; +Cc: zsh-workers

Hi Moritz,

On Fri, Jan 7, 2022, at 4:21 AM, Moritz Bunkus wrote:
> we have several Ansible repositories in different directories. I often have
> to switch between them.
>
> Unfortunately the tab completion for all the Ansible commands caches known
> Hosts & Groups in the global variables _ansible_hosts &
> _ansible_groups. For me the effect is that the wrong set of hosts & groups
> is offered when I change to a different Ansible repo.
>
> How to reproduce:
>
> 1. Have two or more Ansible repositories with different inventories
>    somewhere
> 2. cd into /path/to/first_ansible_tree
> 3. Type `anssible <TAB>`, let tab completion cache hosts & groups
> 4. Hit <TAB> again to verify that the offered completions match expected
>    ones
> 5. cd into /path/to/other_ansible_tree
> 6. Type `ansible <TAB><TAB>` and observe that the offered completions still
>    come from the first_ansible_tree, not from other_ansible_tree
>
> As a quick workaround I've modified the completion to also cache the
> working directory where _ansible_hosts & _ansible_groups were created. If
> the current working directory doesn't match the cached working directory,
> both variables are unset & their values cached again. This seems to work.
>
> The attached patch implements this. Feel free to use it however you want,
> or to apply a totally different solution.

Thanks for the contribution, and apologies for the delay.

workers: Are we interested in integrating this?

-- 
vq


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Ansible tab completion: re-read cached hosts & groups on directory change
  2022-03-31  5:55 ` Lawrence Velázquez
@ 2022-04-10 21:45   ` Daniel Shahaf
  0 siblings, 0 replies; 3+ messages in thread
From: Daniel Shahaf @ 2022-04-10 21:45 UTC (permalink / raw)
  To: Lawrence Velázquez; +Cc: Moritz Bunkus, zsh-workers

Lawrence Velázquez wrote on Thu, Mar 31, 2022 at 01:55:49 -0400:
> Hi Moritz,
> 
> On Fri, Jan 7, 2022, at 4:21 AM, Moritz Bunkus wrote:
> > we have several Ansible repositories in different directories. I often have
> > to switch between them.
> >
> > Unfortunately the tab completion for all the Ansible commands caches known
> > Hosts & Groups in the global variables _ansible_hosts &
> > _ansible_groups. For me the effect is that the wrong set of hosts & groups
> > is offered when I change to a different Ansible repo.
> >
> > How to reproduce:
> >
> > 1. Have two or more Ansible repositories with different inventories
> >    somewhere
> > 2. cd into /path/to/first_ansible_tree
> > 3. Type `anssible <TAB>`, let tab completion cache hosts & groups
> > 4. Hit <TAB> again to verify that the offered completions match expected
> >    ones
> > 5. cd into /path/to/other_ansible_tree
> > 6. Type `ansible <TAB><TAB>` and observe that the offered completions still
> >    come from the first_ansible_tree, not from other_ansible_tree
> >
> > As a quick workaround I've modified the completion to also cache the
> > working directory where _ansible_hosts & _ansible_groups were created. If
> > the current working directory doesn't match the cached working directory,
> > both variables are unset & their values cached again. This seems to work.
> >
> > The attached patch implements this. Feel free to use it however you want,
> > or to apply a totally different solution.
> 
> Thanks for the contribution, and apologies for the delay.
> 
> workers: Are we interested in integrating this?

Yes.  Tested, and it's correct, but we're in release freeze now ☹

An alternative would have been _store_cache with $PWD as part of the key
(and possibly the hostname too, in case $HOME is shared across multiple
hosts).

Cheers,

Daniel


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-04-10 21:45 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-07  9:21 Ansible tab completion: re-read cached hosts & groups on directory change Moritz Bunkus
2022-03-31  5:55 ` Lawrence Velázquez
2022-04-10 21:45   ` Daniel Shahaf

Code repositories for project(s) associated with this inbox:

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).