Skip to content
This repository has been archived by the owner on May 25, 2022. It is now read-only.

add completion generation for zsh #137

Merged
merged 1 commit into from May 26, 2020

Conversation

MunifTanjim
Copy link
Contributor

@MunifTanjim MunifTanjim commented May 24, 2020

Completion support for ZSH

Previously, #76 took a manual approach for this and was abandoned later (no activity for a long time).

Usage

vanilla (.zshrc):

Add the following line in your .zshrc file:

eval "$(bw completion --shell zsh); compdef _bw bw;"

vanilla (vendor-completions)

Run the following command:

bw completion --shell zsh | sudo tee /usr/share/zsh/vendor-completions/_bw

zinit:

Run the following commands:

bw completion --shell zsh > ~/.local/share/zsh/completions/_bw
zinit creinstall ~/.local/share/zsh/completions

Screenshots

Screenshot from 2020-05-24 19-06-00

Screenshot from 2020-05-24 19-07-41

Generated Output:

#compdef _bw bw

function _bw {
  local -a commands

  _arguments -C \
    '--pretty[Format output. JSON is tabbed with two spaces.]' \
    '--raw[Return raw output instead of a descriptive message.]' \
    '--response[Return a JSON formatted version of response output.]' \
    '--quiet[Don'"'"'t return anything to stdout.]' \
    '--nointeraction[Do not prompt for interactive user input.]' \
    '--session[Pass session key instead of reading from env.]' \
    '(-v --version)'{-v,--version}'[output the version number]' \
    '(-h --help)'{-h,--help}'[output usage information]' \
    "1: :->cmnds" \
    "*::arg:->args"

  case $state in
    cmnds)
      commands=(
        "login:Log into a user account."
        "logout:Log out of the current user account."
        "lock:Lock the vault and destroy active session keys."
        "unlock:Unlock the vault and return a new session key."
        "sync:Pull the latest vault data from server."
        "list:List an array of objects from the vault."
        "get:Get an object from the vault."
        "create:Create an object in the vault."
        "edit:Edit an object from the vault."
        "delete:Delete an object from the vault."
        "restore:Restores an object from the trash."
        "share:Share an item to an organization."
        "confirm:Confirm an object to the organization."
        "import:Import vault data from a file."
        "export:Export vault data to a CSV or JSON file."
        "generate:Generate a password/passphrase."
        "encode:Base 64 encode stdin."
        "config:Configure CLI settings."
        "update:Check for updates."
        "completion:Generate shell completions."
      )
      _describe "command" commands
      ;;
  esac

  case "$words[1]" in
    login)
      _bw_login
      ;;
    logout)
      _bw_logout
      ;;
    lock)
      _bw_lock
      ;;
    unlock)
      _bw_unlock
      ;;
    sync)
      _bw_sync
      ;;
    list)
      _bw_list
      ;;
    get)
      _bw_get
      ;;
    create)
      _bw_create
      ;;
    edit)
      _bw_edit
      ;;
    delete)
      _bw_delete
      ;;
    restore)
      _bw_restore
      ;;
    share)
      _bw_share
      ;;
    confirm)
      _bw_confirm
      ;;
    import)
      _bw_import
      ;;
    export)
      _bw_export
      ;;
    generate)
      _bw_generate
      ;;
    encode)
      _bw_encode
      ;;
    config)
      _bw_config
      ;;
    update)
      _bw_update
      ;;
    completion)
      _bw_completion
      ;;
  esac
}

function _bw_login {
  _arguments -C \
    '--method[Two-step login method.]' \
    '--code[Two-step login code.]' \
    '--check[Check login status.]' \
    '(-h --help)'{-h,--help}'[output usage information]' \
    "*::arg:->args"
}

function _bw_logout {
  
}

function _bw_lock {
  
}

function _bw_unlock {
  _arguments -C \
    '--check[Check lock status.]' \
    '(-h --help)'{-h,--help}'[output usage information]' \
    "*::arg:->args"
}

function _bw_sync {
  _arguments -C \
    '(-f --force)'{-f,--force}'[Force a full sync.]' \
    '--last[Get the last sync date.]' \
    '(-h --help)'{-h,--help}'[output usage information]' \
    "*::arg:->args"
}

function _bw_list {
  _arguments -C \
    '--search[Perform a search on the listed objects.]' \
    '--url[Filter items of type login with a url-match search.]' \
    '--folderid[Filter items by folder id.]' \
    '--collectionid[Filter items by collection id.]' \
    '--organizationid[Filter items or collections by organization id.]' \
    '--trash[Filter items that are deleted and in the trash.]' \
    '(-h --help)'{-h,--help}'[output usage information]' \
    "*::arg:->args"
}

function _bw_get {
  _arguments -C \
    '--itemid[Attachment'"'"'s item id.]' \
    '--output[Output directory or filename for attachment.]' \
    '--organizationid[Organization id for an organization object.]' \
    '(-h --help)'{-h,--help}'[output usage information]' \
    "*::arg:->args"
}

function _bw_create {
  _arguments -C \
    '--file[Path to file for attachment.]' \
    '--itemid[Attachment'"'"'s item id.]' \
    '--organizationid[Organization id for an organization object.]' \
    '(-h --help)'{-h,--help}'[output usage information]' \
    "*::arg:->args"
}

function _bw_edit {
  _arguments -C \
    '--organizationid[Organization id for an organization object.]' \
    '(-h --help)'{-h,--help}'[output usage information]' \
    "*::arg:->args"
}

function _bw_delete {
  _arguments -C \
    '--itemid[Attachment'"'"'s item id.]' \
    '--organizationid[Organization id for an organization object.]' \
    '(-p --permanent)'{-p,--permanent}'[Permanently deletes the item instead of soft-deleting it (item only).]' \
    '(-h --help)'{-h,--help}'[output usage information]' \
    "*::arg:->args"
}

function _bw_restore {
  
}

function _bw_share {
  
}

function _bw_confirm {
  _arguments -C \
    '--organizationid[Organization id for an organization object.]' \
    '(-h --help)'{-h,--help}'[output usage information]' \
    "*::arg:->args"
}

function _bw_import {
  _arguments -C \
    '--formats[List formats]' \
    '(-h --help)'{-h,--help}'[output usage information]' \
    "*::arg:->args"
}

function _bw_export {
  _arguments -C \
    '--output[Output directory or filename.]' \
    '--format[Export file format.]' \
    '--organizationid[Organization id for an organization.]' \
    '(-h --help)'{-h,--help}'[output usage information]' \
    "*::arg:->args"
}

function _bw_generate {
  _arguments -C \
    '(-u --uppercase)'{-u,--uppercase}'[Include uppercase characters.]' \
    '(-l --lowercase)'{-l,--lowercase}'[Include lowercase characters.]' \
    '(-n --number)'{-n,--number}'[Include numeric characters.]' \
    '(-s --special)'{-s,--special}'[Include special characters.]' \
    '(-p --passphrase)'{-p,--passphrase}'[Generate a passphrase.]' \
    '--length[Length of the password.]' \
    '--words[Number of words.]' \
    '--separator[Word separator.]' \
    '(-h --help)'{-h,--help}'[output usage information]' \
    "*::arg:->args"
}

function _bw_encode {
  
}

function _bw_config {
  _arguments -C \
    '--web-vault[Provides a custom web vault URL that differs from the base URL.]' \
    '--api[Provides a custom API URL that differs from the base URL.]' \
    '--identity[Provides a custom identity URL that differs from the base URL.]' \
    '--icons[Provides a custom icons service URL that differs from the base URL.]' \
    '--notifications[Provides a custom notifications URL that differs from the base URL.]' \
    '--events[Provides a custom events URL that differs from the base URL.]' \
    '(-h --help)'{-h,--help}'[output usage information]' \
    "*::arg:->args"
}

function _bw_update {
  
}

function _bw_completion {
  _arguments -C \
    '--shell[Shell to generate completions for.]' \
    '(-h --help)'{-h,--help}'[output usage information]' \
    "*::arg:->args"
}

resolves #75

@CLAassistant
Copy link

CLAassistant commented May 24, 2020

CLA assistant check
All committers have signed the CLA.

@kspearrin kspearrin merged commit 723ff20 into bitwarden:master May 26, 2020
@kspearrin
Copy link
Member

Thanks. Could you please update the help center docs for the CLI to include this information?

@kspearrin
Copy link
Member

FYI, @MunifTanjim , formatting and lint fixes were added here: d6c5d0e

Please let me know if you see anything that might have broken.

@MunifTanjim
Copy link
Contributor Author

Thanks. Could you please update the help center docs for the CLI to include this information?

PR created.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Shell Completion
3 participants