Class: Vagrant::Util::InstallShellConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant/util/install_cli_autocomplete.rb

Overview

Generic installation of content to shell config file

Constant Summary collapse

PERPEND_STRING =
"# >>>> Vagrant command completion (start)".freeze
APPEND_STRING =
"# <<<<  Vagrant command completion (end)".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string_insert, config_paths) ⇒ InstallShellConfig

Returns a new instance of InstallShellConfig.


14
15
16
17
18
19
20
# File 'lib/vagrant/util/install_cli_autocomplete.rb', line 14

def initialize(string_insert, config_paths)
  @prepend_string = PERPEND_STRING
  @string_insert = string_insert
  @append_string = APPEND_STRING
  @config_paths = config_paths
  @logger = Log4r::Logger.new("vagrant::util::install_shell_config")
end

Instance Attribute Details

#append_stringObject

Returns the value of attribute append_string


11
12
13
# File 'lib/vagrant/util/install_cli_autocomplete.rb', line 11

def append_string
  @append_string
end

#config_pathsObject

Returns the value of attribute config_paths


12
13
14
# File 'lib/vagrant/util/install_cli_autocomplete.rb', line 12

def config_paths
  @config_paths
end

#prepend_stringObject

Returns the value of attribute prepend_string


9
10
11
# File 'lib/vagrant/util/install_cli_autocomplete.rb', line 9

def prepend_string
  @prepend_string
end

#string_insertObject

Returns the value of attribute string_insert


10
11
12
# File 'lib/vagrant/util/install_cli_autocomplete.rb', line 10

def string_insert
  @string_insert
end

Instance Method Details

#install(home) ⇒ string

Given a path to the users home dir, will install some given strings marked by a prepend and append string

Parameters:

  • path (string)

    to users home dir

Returns:

  • (string)

    path to shell config file that was modified if exists


60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# 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) ⇒ boolean

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

Parameters:

  • path (string)

    to a file (config file)

Returns:

  • (boolean)

    true if the prepend string is found in the file


45
46
47
48
49
50
51
52
53
# 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) ⇒ string

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.

Parameters:

  • path (string)

    to users home dir

Returns:

  • (string)

    path to shell config file if exists


28
29
30
31
32
33
34
35
36
37
38
# 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.exists?(config_file)
      @logger.info("Found config file #{config_file}")
      return config_file
    end
  end
  return nil
end