Skip to content

Commit

Permalink
Add command-line completions for fish shell
Browse files Browse the repository at this point in the history
Since the command to enable them is different, also make that change in
the output based on the shell used.

Signed-off-by: Nathan L Smith <smith@chef.io>
  • Loading branch information
Nathan L Smith committed May 4, 2017
1 parent bfda54f commit e3c62ec
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
34 changes: 34 additions & 0 deletions lib/inspec/completions/fish.sh.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
function __fish_inspec_no_command --description 'Test if inspec has yet to be given the main command'
set -l cmd (commandline -opc)
test (count $cmd) -eq 1
end

function __fish_inspec_using_command
set -l cmd (commandline -opc)
set -q cmd[2]; and test "$argv[1]" = $cmd[2]
end

function __fish_inspec_using_command_and_no_subcommand
set -l cmd (commandline -opc)
test (count $cmd) -eq 2; and test "$argv[1]" = "$cmd[2]"
end

function __fish_inspec_using_subcommand --argument-names cmd_main cmd_sub
set -l cmd (commandline -opc)
set -q cmd[3]; and test "$cmd_main" = $cmd[2] -a "$cmd_sub" = $cmd[3]
end

<% top_level_commands_with_descriptions.each do |command_and_description| %>
<% command, description = command_and_description.split(':') %>
<% description.gsub!(/\\/, '') %>
# <%= command %> commands
complete -c inspec -f -n '__fish_inspec_no_command' -a <%= command %> -d "<%= description %>"
# <%= command %> help
complete -c inspec -f -n '__fish_inspec_using_command help' -a <%= command %> -d "<%= description %>"

<% (subcommands_with_commands_and_descriptions[command] || []).each do |command_and_description| %>
<% subcommand, description = command_and_description.split(':') %>
<% description.gsub!(/\\/, '') %>
complete -c inspec -f -n '__fish_inspec_using_command_and_no_subcommand <%= command %>' -a <%= subcommand %> -d "<%= description %>"
<% end %>
<% end %>
10 changes: 9 additions & 1 deletion lib/inspec/env_printer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@

module Inspec
class EnvPrinter
attr_reader :shell

EVAL_COMMANDS = {
'bash' => 'eval \"$(inspec env bash)\"',
'fish' => 'inspec env fish > ~/.config/fish/completions/inspec.fish',
'zsh' => 'eval \"$(inspec env zsh)\"',
}.freeze

def initialize(command_class, shell = nil)
if !shell
@detected = true
Expand Down Expand Up @@ -56,7 +64,7 @@ def print_usage_guidance
puts <<EOF
# To use this, eval it in your shell
#
# eval "$(inspec env #{@shell})"
# #{EVAL_COMMANDS[shell]}
#
#
EOF
Expand Down

0 comments on commit e3c62ec

Please sign in to comment.