vaguerent/lib/vagrant/util/install_cli_autocomplete.rb
2020-05-29 15:10:13 -04:00

117 lines
4.0 KiB
Ruby

module Vagrant
module Util
# Generic installation of content to shell config file
class InstallShellConfig
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
@string_insert = string_insert
@append_string = append_string
@config_paths = config_paths
@logger = Log4r::Logger.new("vagrant::util::install_shell_config")
end
# Searches a users home dir for a shell config file based on a
# given home dir and a configured set of config paths. If there
# are multiple config paths, it will return the first match.
#
# @param [string] path to users home dir
# @return [string] path to shell config file if exists
def shell_installed(home)
@logger.info("Searching for config in home #{home}")
@config_paths.each do |path|
config_file = File.join(home, path)
if File.exists?(config_file)
@logger.info("Found config file #{config_file}")
return config_file
end
end
return nil
end
# Searches a given file for the existence of a set prepend string.
# This can be used to find if vagrant has inserted some strings to a file
#
# @param [string] path to a file (config file)
# @return [boolean] true if the prepend string is found in the file
def is_installed(path)
File.foreach(path) do |line|
if line.include?(@prepend_string)
@logger.info("Found completion already installed in #{path}")
return true
end
end
return false
end
# Given a path to the users home dir, will install some given strings
# marked by a prepend and append string
#
# @param [string] path to users home dir
# @return [string] path to shell config file that was modified if exists
def install(home)
path = shell_installed(home)
if path && !is_installed(path)
File.open(path, "a") do |f|
f.write("\n")
f.write(@prepend_string)
f.write("\n")
f.write(@string_insert)
f.write("\n")
f.write(@append_string)
f.write("\n")
end
end
return path
end
end
# Install autocomplete script to zsh config located as .zshrc
class InstallZSHShellConfig < InstallShellConfig
def initialize()
prepend_string = "# >>>> Vagrant zsh completion (start)".freeze
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)
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
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)
end
end
# Install autocomplete script for supported shells
class InstallCLIAutocomplete
SUPPORTED_SHELLS = {
"zsh" => Vagrant::Util::InstallZSHShellConfig.new(),
"bash" => Vagrant::Util::InstallBashShellConfig.new()
}
def self.install
home = Dir.home
written_paths = []
SUPPORTED_SHELLS.each do |k, shell|
p = shell.install(home)
if p
written_paths.push(p)
end
end
return written_paths
end
end
end
end