diff --git a/lib/vagrant/util/install_cli_autocomplete.rb b/lib/vagrant/util/install_cli_autocomplete.rb index 709ad0e03..b23d3f855 100644 --- a/lib/vagrant/util/install_cli_autocomplete.rb +++ b/lib/vagrant/util/install_cli_autocomplete.rb @@ -2,16 +2,19 @@ module Vagrant module Util # Generic installation of content to shell config file class InstallShellConfig - + + PERPEND_STRING = "# >>>> Vagrant command completion (start)".freeze + APPEND_STRING = "# <<<< Vagrant command completion (end)".freeze + attr_accessor :prepend_string attr_accessor :string_insert attr_accessor :append_string attr_accessor :config_paths - def initialize(prepend_string, string_insert, append_string, config_paths) - @prepend_string = prepend_string + def initialize(string_insert, config_paths) + @prepend_string = PERPEND_STRING @string_insert = string_insert - @append_string = append_string + @append_string = APPEND_STRING @config_paths = config_paths @logger = Log4r::Logger.new("vagrant::util::install_shell_config") end @@ -73,23 +76,19 @@ module Vagrant # Install autocomplete script to zsh config located as .zshrc class InstallZSHShellConfig < InstallShellConfig - def initialize() - prepend_string = "# >>>> Vagrant zsh completion (start)".freeze + def initialize string_insert = """fpath=(#{File.join(Vagrant.source_root, "contrib", "zsh")} $fpath)\ncompinit""".freeze - append_string = "# <<<< Vagrant zsh completion (end)".freeze config_paths = [".zshrc".freeze].freeze - super(prepend_string, string_insert, append_string, config_paths) + super(string_insert, config_paths) end end # Install autocomplete script to bash config located as .bashrc or .bash_profile class InstallBashShellConfig < InstallShellConfig - def initialize() - prepend_string = "# >>>> Vagrant bash completion (start)".freeze + def initialize string_insert = ". #{File.join(Vagrant.source_root, 'contrib', 'bash', 'completion.sh')}".freeze - append_string = "# <<<< Vagrant bash completion (end)".freeze config_paths = [".bashrc".freeze, ".bash_profile".freeze].freeze - super(prepend_string, string_insert, append_string, config_paths) + super(string_insert, config_paths) end end @@ -101,17 +100,14 @@ module Vagrant } def self.install(shells=[]) - shells = shells.length == 0 ? SUPPORTED_SHELLS.keys() : shells + shells = SUPPORTED_SHELLS.keys() if shells.empty? home = Dir.home written_paths = [] - SUPPORTED_SHELLS.each do |k, shell| - if shells.include?(k) - p = shell.install(home) - if p - written_paths.push(p) - end - end - end + + shells.map do |shell| + next if !SUPPORTED_SHELLS[shell] + written_paths.push(SUPPORTED_SHELLS[shell].install(home)) + end.compact return written_paths end end diff --git a/plugins/commands/autocomplete/command/install.rb b/plugins/commands/autocomplete/command/install.rb index d9f577156..bde978688 100644 --- a/plugins/commands/autocomplete/command/install.rb +++ b/plugins/commands/autocomplete/command/install.rb @@ -8,7 +8,7 @@ module VagrantPlugins class Install < Vagrant.plugin("2", :command) def execute options = { - :shells => [] + shells: [] } opts = OptionParser.new do |o| diff --git a/plugins/commands/autocomplete/command/root.rb b/plugins/commands/autocomplete/command/root.rb index 160a074ad..fbbb10060 100644 --- a/plugins/commands/autocomplete/command/root.rb +++ b/plugins/commands/autocomplete/command/root.rb @@ -46,7 +46,7 @@ module VagrantPlugins # Add the available subcommands as separators in order to print them # out as well. keys = [] - @subcommands.each { |key, value| keys << key.to_s } + keys = @subcommands.keys.map(&:to_s) keys.sort.each do |key| opts.separator " #{key}"