Module: Doable::Helpers::OS
Instance Method Summary collapse
-
#die_if_dir_exists(directory) ⇒ Boolean
Die if a directory __does__ exist.
-
#find_directory(directory) ⇒ String
Die if a directory doesn’t exist.
-
#find_file(file, message = "") ⇒ String
Throws an exception if a file doesn’t exist.
-
#find_or_create_directory(directory) ⇒ String
Ensure a directory exists.
-
#find_or_create_file(file) ⇒ Object
Used for backwards compatibility (just a wrapper for touch).
-
#must_run_as(user) ⇒ Object
Confirms the current script is running as a certain user.
-
#remove(file_or_directory) ⇒ Object
Remove a file or directory.
-
#sed(substitutions, input_file, output_file) ⇒ Object
A simple replacement for ‘sed`.
-
#tee(command) ⇒ Fixnum, String
Ruby in-memory equivalent of ‘tee` Note that this command is dangerous for very long command output.
-
#touch(file_list, options = {}) ⇒ Object
Used like Unix touch.
-
#validate_host(hostname) ⇒ Object
Check that the OS can resolve a hostname.
Instance Method Details
#die_if_dir_exists(directory) ⇒ Boolean
Die if a directory __does__ exist. Useful for ensuring software wasn’t previously installed, etc.
39 40 41 42 43 44 45 46 |
# File 'lib/doable/helpers/os.rb', line 39 def die_if_dir_exists(directory) full_path = File.(directory) if File.directory?(full_path) raise DirectoryExists, directory else return true end end |
#find_directory(directory) ⇒ String
Die if a directory doesn’t exist
28 29 30 31 32 |
# File 'lib/doable/helpers/os.rb', line 28 def find_directory(directory) full_path = File.(directory) raise MissingDirectory, directory unless File.directory?(full_path) return full_path end |
#find_file(file, message = "") ⇒ String
Throws an exception if a file doesn’t exist
97 98 99 100 101 102 103 104 |
# File 'lib/doable/helpers/os.rb', line 97 def find_file(file, = "") unless File.exists?(File.(file)) .empty? ? log("Filename #{file} is invalid", :error) : log(, :error) raise MissingFile, file end return File.(file) end |
#find_or_create_directory(directory) ⇒ String
Ensure a directory exists
18 19 20 21 22 |
# File 'lib/doable/helpers/os.rb', line 18 def find_or_create_directory(directory) full_path = File.(directory) FileUtils.mkdir_p(full_path) unless File.directory?(full_path) return full_path end |
#find_or_create_file(file) ⇒ Object
Used for backwards compatibility (just a wrapper for touch)
84 85 86 87 88 89 90 |
# File 'lib/doable/helpers/os.rb', line 84 def find_or_create_file(file) unless File.exists?(File.(file)) log "File '#{file}' does not seem to exist... creating it...", :warn touch File.(file) end return File.(file) end |
#must_run_as(user) ⇒ Object
Confirms the current script is running as a certain user
10 11 12 13 |
# File 'lib/doable/helpers/os.rb', line 10 def must_run_as(user) raise WrongUser, user unless user == Etc.getpwuid.name log "Running as correct user" end |
#remove(file_or_directory) ⇒ Object
Remove a file or directory
50 51 52 |
# File 'lib/doable/helpers/os.rb', line 50 def remove(file_or_directory) FileUtils.rm_rf(File.(file_or_directory)) end |
#sed(substitutions, input_file, output_file) ⇒ Object
A simple replacement for ‘sed`. This is far from a complete implementation and should be used sparingly. A much better approach is to use ERB files.
112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/doable/helpers/os.rb', line 112 def sed(substitutions, input_file, output_file) input_file = input_file.kind_of?(IO) ? input_file : File.open(input_file, "r") output_file = output_file.kind_of?(IO) ? output_file : File.open(output_file, "w") raise InvalidInput unless substitutions.kind_of?(Hash) # Very fancy way to replace things in a file and write to a new file output_file.puts( input_file.read.gsub(Regexp.union(substitutions.keys)) do |match| # Hideous way to work with regular expressions *and* string substitutions in a single pass over a large file substitutions[substitutions.keys.keep_if {|s| substitutions[s].to_s if match == s or s.match(match) }.first].to_s end ) output_file.close end |
#tee(command) ⇒ Fixnum, String
Ruby in-memory equivalent of ‘tee` Note that this command is dangerous for very long command output. Need to consider setting a buffer max size for this.
65 66 67 68 69 70 71 72 73 74 |
# File 'lib/doable/helpers/os.rb', line 65 def tee(command) command_output = "" IO.popen(command) do |io| while line = io.gets command_output << line LOGGING_MUTEX.synchronize { puts line } end end return [$?.exitstatus, command_output] end |
#touch(file_list, options = {}) ⇒ Object
Used like Unix touch
78 79 80 |
# File 'lib/doable/helpers/os.rb', line 78 def touch(file_list, = {}) FileUtils.touch(file_list, ) end |
#validate_host(hostname) ⇒ Object
Check that the OS can resolve a hostname
57 58 59 |
# File 'lib/doable/helpers/os.rb', line 57 def validate_host(hostname) raise InvalidHostname, hostname unless Resolv.getaddress(hostname) end |