Skip to content

Commit

Permalink
Add basic bash completion script and framework for others
Browse files Browse the repository at this point in the history
  • Loading branch information
scotje committed Jul 26, 2017
1 parent 5fa65b6 commit 41e967e
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,22 @@ Here are the keys that vmfloaty currently supports:
- url
+ String
### Tab Completion
There is a basic completion script for Bash (and possibly other shells) included with the gem in the [extras/completions](https://github.com/briancain/vmfloaty/blob/master/extras/completions) folder. To activate, that file simply needs to be sourced somehow in your shell profile.
For convenience, the path to the completion script for the currently active version of the gem can be found with the `floaty completion` subcommand. This makes it easy to add the completion script to your profile like so:

```bash
source $(floaty completion --shell bash)
```

If you are running on macOS and use Homebrew's `bash-completion` formula, you can symlink the script to `/usr/local/etc/bash_completion.d/floaty` and it will be sourced automatically:

```bash
ln -s $(floaty completion --shell bash) /usr/local/etc/bash_completion.d/floaty
```

## vmpooler API

This cli tool uses the [vmpooler API](https://github.com/puppetlabs/vmpooler/blob/master/API.md).
Expand Down
27 changes: 27 additions & 0 deletions extras/completions/floaty.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env bash

_vmfloaty()
{
local cur prev subcommands template_subcommands hostname_subcommands
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"

subcommands="delete get help list modify query revert snapshot ssh status summary token"
template_subcommands="get ssh"
hostname_subcommands="delete modify query revert snapshot"

if [[ $template_subcommands =~ (^| )$prev($| ) ]] ; then
if [ -z "$_vmfloaty_avail_templates" ] ; then
_vmfloaty_avail_templates=`floaty list`
fi

COMPREPLY=( $(compgen -W "${_vmfloaty_avail_templates}" -- ${cur}) )
elif [[ $hostname_subcommands =~ (^| )$prev($| ) ]] ; then
_vmfloaty_active_hostnames=`floaty list --active | grep '^-' | cut -d' ' -f2`
COMPREPLY=( $(compgen -W "${_vmfloaty_active_hostnames}" -- ${cur}) )
else
COMPREPLY=( $(compgen -W "${subcommands}" -- ${cur}) )
fi
}
complete -F _vmfloaty floaty
19 changes: 19 additions & 0 deletions lib/vmfloaty.rb
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,25 @@ def run
end
end

command :completion do |c|
c.syntax = 'floaty completion [options]'
c.summary = 'Outputs path to completion script'
c.description = "Outputs path to a completion script for the specified shell, or bash if not specified.\nWill exit non-zero if no completion script is available for the requested shell."
c.example 'Gets path to bash tab completion script', 'floaty completion --shell bash'
c.option '--shell STRING', String, 'Shell to request completion script for'
c.action do |args, options|
shell = (options.shell || 'bash').downcase.strip
completion_file = File.expand_path(File.join('..', '..', 'extras', 'completions', "floaty.#{shell}"), __FILE__)

if File.exist?(completion_file)
puts completion_file
exit 0
else
exit 1
end
end
end

run!
end
end

0 comments on commit 41e967e

Please sign in to comment.