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
- PERPEND_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.
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_string ⇒ Object
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_paths ⇒ Object
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_string ⇒ Object
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_insert ⇒ Object
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
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
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.
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 |