Class: Vagrant::Util::InstallShellConfig
- Inherits:
-
Object
- Object
- Vagrant::Util::InstallShellConfig
- Defined in:
- lib/vagrant/util/install_cli_autocomplete.rb
Overview
Generic installation of content to shell config file
Direct Known Subclasses
Constant Summary collapse
- PREPEND_STRING =
"# >>>> Vagrant command completion (start)".freeze
- APPEND_STRING =
"# <<<< Vagrant command completion (end)".freeze
Instance Attribute Summary collapse
-
#append_string ⇒ Object
Returns the value of attribute append_string.
-
#config_paths ⇒ Object
Returns the value of attribute config_paths.
-
#prepend_string ⇒ Object
Returns the value of attribute prepend_string.
-
#string_insert ⇒ Object
Returns the value of attribute string_insert.
Instance Method Summary collapse
-
#initialize(string_insert, config_paths) ⇒ InstallShellConfig
constructor
A new instance of InstallShellConfig.
-
#install(home) ⇒ string
Given a path to the users home dir, will install some given strings marked by a prepend and append string.
-
#is_installed(path) ⇒ boolean
Searches a given file for the existence of a set prepend string.
-
#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.
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_string ⇒ Object
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_paths ⇒ Object
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_string ⇒ Object
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_insert ⇒ Object
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
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
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.
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 |