Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ZSH completions when using glab plugin #122

Open
jmccure opened this issue Dec 20, 2022 · 8 comments
Open

ZSH completions when using glab plugin #122

jmccure opened this issue Dec 20, 2022 · 8 comments
Labels
bug Something isn't working op-cli Functionality to be implemented in 1Password CLI. Needs to be done by 1Password Developers.

Comments

@jmccure
Copy link

jmccure commented Dec 20, 2022

Platform or tool

GitLab

Desired or expected behavior

Hi team, really enjoying the GitLab 1password plugin. https://developer.1password.com/docs/cli/shell-plugins/gitlab

One thing I noticed once I installed the glab shell plugin is that my completions stopped working, since op aliases glab

I had to add setopt completealiases to my ~/.zshrc file so zsh does not expand aliases before performing completion.

I was thinking if it would be worth mentioning here: https://developer.1password.com/docs/cli/shell-plugins/gitlab ?

Current behavior

Completions don't work by default with zsh + op

Relevant log output

No response

op CLI version

2.8.0-beta.10

@mrjones2014
Copy link
Member

Have not tested, but I would guess this is the same for all shells (except maybe Fish, Fish shell is pretty good about doing that kind of stuff by default) and all Shell Plugins.

Maybe plugins.sh should set the correct equivalent of completealiases in all shells, or otherwise mention it somewhere in the docs that applies to all shell plugins.

@SimonBarendse SimonBarendse added bug Something isn't working op-cli Functionality to be implemented in 1Password CLI. Needs to be done by 1Password Developers. labels Dec 20, 2022
@mrjones2014
Copy link
Member

mrjones2014 commented Jan 3, 2023

Some findings from looking into this a bit:

  • For zsh, you just need to setopt completealiases
  • For fish, no changes are needed, Fish shell follows alias completions by default
  • For bash, there is not any equivalent of setopt completealiases, so you would need to do one of a few things (see below)

For these reasons, I think the best approach is to document it, rather than trying to set it automatically, because the plugins.sh file has no shebang line (e.g. #!/usr/bin/env bash) and so it runs in the user's own shell environment. Trying to set it automatically would mean adding some shell detection, in a syntax that is compatible with all of zsh, bash, and fish, which might be complex; fish syntax is mostly compatible with bash/zsh but not fully.

Options for bash completions:
Disclaimer: I've not validated these, we'll need to audit them before we can officially recommend them. For now, use at your own risk.

@jrpedrianes
Copy link

jrpedrianes commented Jan 16, 2023

For ZSH we can do the following:

function _glab_command () {
  op plugin run -- glab $@
}

alias glab="_glab_command"
compdef _glab _glab_command

I no tested it yet for glab, but I did it for "doctl" and it works well

@aaronkollasch
Copy link

aaronkollasch commented Feb 22, 2023

I'd prefer not to setopt completealiases in zsh as that would mess up completions of other aliases that include command line args.

I tried this for gh and while @jrpedrianes solution worked, it asked for authentication on every completion, unless I switched the security settings from "ask approval for each new application and terminal session" to "ask approval for each new application". I think this is because a new subshell is spawned for each function call?

My interim solution has been to update the op completion functions to call _gh directly – see this gist for the changes. In particular, within _op_plugin_run:

  case "$line[1]" in
  gh)
    _gh
    ;;
  esac

This should work for _glab as well. There's probably a better way to make it work for all completions, but I haven't found it yet.


Edit: the general solution for zsh looks to be as simple as replacing the lines above with:

_dispatch "$curcontext" "$line[1]"

@Forst
Copy link

Forst commented Oct 31, 2023

@aaronkollasch's solution above works for me for op plugin run glab:

% op plugin run glab #TAB
alias         -- Create, list and delete aliases
api           -- Make an authenticated request to GitLab API
...

However op's aliases default to including the -- separator, e.g. op plugin run -- glab, which doesn't work and autocompletes files/dirs instead:

% op plugin run -- glab #TAB
bin/               Desktop/           Downloads/
...

zsh 5.9. op autogenerates the aliases, I'd prefer not to mess with those manually. Any idea what could be the issue here? Thank you!

@aaronkollasch
Copy link

aaronkollasch commented Feb 26, 2024

@Forst I finally found the problem – there was an extra -- hanging around before the command in the zsh variable $words.

Adding the following lines before _dispatch "$curcontext" "$line[1]" fixes the issue:

  if [[ "$words[1]" == "--" ]]; then
    shift words
  fi

I've also updated the gist. This should get things to work properly with op's autogenerated aliases, i.e. op plugin run -- glab

aaronkollasch added a commit to aaronkollasch/dotfiles that referenced this issue Feb 26, 2024
aaronkollasch added a commit to aaronkollasch/dotfiles that referenced this issue Feb 26, 2024
@mrjones2014
Copy link
Member

Related: #433

@punk-dev-robot
Copy link

The thing that works reliably for me is also monkey patching op completion function with:

function _op_plugin_run_fix() {
    for (( i=2; i < CURRENT; i++ )); do
        if [[ ${words[i]} == -- ]]; then
            shift $i words
            (( CURRENT -= i ))
            _normal
            return
        fi
    done

    # run original op completion
    _op
}

function __load_op_completion() {
  local op_completion_fn="$(op completion zsh)"
  local fix_fn="$(which _op_plugin_run_fix)"

  # replace all references to _op with _op_plugin_run_fix (except for the function definition)
  local patched_op=$(sed -E -e 's/\b_op\b([^()]|$)/_op_plugin_run_fix\1/g' <<< $op_completion_fn)

  echo -e "$patched_op\n\n$fix_fn"
}

eval "$(__load_op_completion)"

The only problem with it is that I need to use eval instead of keeping it on fpath.
With fpath the guard in _op on $funcstack means that when loading completion for the first time I have to press <TAB> twice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working op-cli Functionality to be implemented in 1Password CLI. Needs to be done by 1Password Developers.
Projects
None yet
Development

No branches or pull requests

7 participants