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

PREPEND_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.



17
18
19
20
21
22
23
# File 'lib/vagrant/util/install_cli_autocomplete.rb', line 17

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

Instance Attribute Details

#append_stringObject

Returns the value of attribute append_string.



14
15
16
# File 'lib/vagrant/util/install_cli_autocomplete.rb', line 14

def append_string
  @append_string
end

#config_pathsObject

Returns the value of attribute config_paths.



15
16
17
# File 'lib/vagrant/util/install_cli_autocomplete.rb', line 15

def config_paths
  @config_paths
end

#prepend_stringObject

Returns the value of attribute prepend_string.



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

def prepend_string
  @prepend_string
end

#string_insertObject

Returns the value of attribute string_insert.



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

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



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/vagrant/util/install_cli_autocomplete.rb', line 63

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



48
49
50
51
52
53
54
55
56
# File 'lib/vagrant/util/install_cli_autocomplete.rb', line 48

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



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/vagrant/util/install_cli_autocomplete.rb', line 31

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