vultr-cli icon indicating copy to clipboard operation
vultr-cli copied to clipboard

[Question] Auto-complete for bash / zsh

Open rohankrishnadev opened this issue 4 years ago • 4 comments

Is there a way to enable shell completion? If not can this be added as a feature?

rohankrishnadev avatar Apr 26 '20 13:04 rohankrishnadev

+1 for this. The Digital Ocean CLI utility, doctl has completion built in where all you have to do is add source <(doctl completion bash) to your .bashrc

nullmem avatar May 31 '20 22:05 nullmem

With the release of vultr-cli 2.6.0 the addition of shell completions was added just not documented yet.

If you run vultr-cli completion you will get

vultr-cli completion

Generate the autocompletion script for vultr-cli for the specified shell.
See each sub-command's help for details on how to use the generated script.

Usage:
  vultr-cli completion [command]

Available Commands:
  bash        generate the autocompletion script for bash
  fish        generate the autocompletion script for fish
  powershell  generate the autocompletion script for powershell
  zsh         generate the autocompletion script for zsh

These autocompletions will get printed to stdout and will require you to set them up.

Some guides

Bash:

  $ source <(yourprogram completion bash)

  # To load completions for each session, execute once:
  # Linux:
  $ yourprogram completion bash > /etc/bash_completion.d/yourprogram
  # macOS:
  $ yourprogram completion bash > /usr/local/etc/bash_completion.d/yourprogram

Zsh:

  # If shell completion is not already enabled in your environment,
  # you will need to enable it.  You can execute the following once:

  $ echo "autoload -U compinit; compinit" >> ~/.zshrc

  # To load completions for each session, execute once:
  $ yourprogram completion zsh > "${fpath[1]}/_yourprogram"

  # You will need to start a new shell for this setup to take effect.

fish:

  $ yourprogram completion fish | source

  # To load completions for each session, execute once:
  $ yourprogram completion fish > ~/.config/fish/completions/yourprogram.fish

PowerShell:

  PS> yourprogram completion powershell | Out-String | Invoke-Expression

  # To load completions for every new session, run:
  PS> yourprogram completion powershell > yourprogram.ps1
  # and source this file from your PowerShell profile.

ddymko avatar Jul 10 '21 17:07 ddymko

yo quick info, we're having trouble generating the shell completions in the Arch Linux package build because we build in a clean environment and there's no api key configured:

[kpcyrd@build vultr-cli-2.6.0]$ ./vultr-cli completion bash
Please export your VULTR API key as an environment variable or add `api-key` to your config file, eg:
export VULTR_API_KEY='<api_key_from_vultr_account>'
[kpcyrd@build vultr-cli-2.6.0]$ 

We're working around this by exporting a dummy value, but tab completion also requires a dummy value to work if no api key is already configured:

[user@localhost ~]% vultr-cli <tab>
export VULTR_API_KEY='<api_key_from_vultr_account>'
Please export your VULTR API key as an environment variable or add `api-key` to your config file, eg:
[user@localhost ~]% export VULTR_API_KEY=x
[user@localhost ~]% 
[user@localhost ~]% 
[user@localhost ~]% vultr-cli <tab>
account         -- Retrieve information about your account
apps            -- Display all available applications
backups         -- Display backups
bare-metal      -- bare-metal is used to access bare metal server commands
block-storage   -- block storage commands
completion      -- generate the autocompletion script for the specified shell
dns             -- dns is used to access dns commands
firewall        -- firewall is used to access firewall commands
help            -- Help about any command
instance        -- commands to interact with instances on vultr
iso             -- iso is used to access iso commands
load-balancer   -- load balancer commands
network         -- network interacts with network actions
object-storage  -- object storage commands
os              -- os is used to access os commands
plans           -- get information about Vultr plans
regions         -- get regions
reserved-ip     -- reserved-ip lets you interact with reserved-ip
script          -- startup script commands
snapshot        -- snapshot commands
ssh-key         -- ssh-key commands
user            -- user commands
version         -- Display current version of Vultr-cli

kpcyrd avatar Jul 11 '21 00:07 kpcyrd

@kpcyrd

This is due to how the CLI is currently designed. One of the first things it checks when executing your command is to see if the API Token is set (doesn't validate it yet). What you are doing is the only way around the current design. Set a dummy value so that it passes initial checks.

We have a ongoing ticket that will fix this behavior and introduce new features (#155)

ddymko avatar Jul 16 '21 12:07 ddymko