class Vagrant::Util::InstallShellConfig

Generic installation of content to shell config file

Constants

APPEND_STRING
PREPEND_STRING

Attributes

append_string[RW]
config_paths[RW]
prepend_string[RW]
string_insert[RW]

Public Class Methods

new(string_insert, config_paths) click to toggle source
# File lib/vagrant/util/install_cli_autocomplete.rb, line 14
def initialize(string_insert, 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

Public Instance Methods

install(home) click to toggle source

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

# File lib/vagrant/util/install_cli_autocomplete.rb, line 60
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
is_installed(path) click to toggle source

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

# File lib/vagrant/util/install_cli_autocomplete.rb, line 45
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
shell_installed(home) click to toggle source

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

# File lib/vagrant/util/install_cli_autocomplete.rb, line 28
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.exist?(config_file)
      @logger.info("Found config file #{config_file}")
      return config_file
    end
  end
  return nil
end