module Doable::Helpers::OS
Public Instance Methods
Die if a directory __does__ exist. Useful for ensuring software wasn’t previously installed, etc. @raise [DirectoryExists] @param directory [String] Directory to verify does not exist @return [Boolean]
# File lib/doable/helpers/os.rb, line 39 def die_if_dir_exists(directory) full_path = File.expand_path(directory) if File.directory?(full_path) raise DirectoryExists, directory else return true end end
Die if a directory doesn’t exist @raise [MissingDirectory] @param directory [String] Directory to verify @return [String]
# File lib/doable/helpers/os.rb, line 28 def find_directory(directory) full_path = File.expand_path(directory) raise MissingDirectory, directory unless File.directory?(full_path) return full_path end
Throws an exception if a file doesn’t exist @raise [MissingFile] @param file [String] File or directory to verify @param message [String] Option message to log if file does not exist @return [String]
# File lib/doable/helpers/os.rb, line 97 def find_file(file, message = "") unless File.exists?(File.expand_path(file)) message.empty? ? log("Filename #{file} is invalid", :error) : log(message, :error) raise MissingFile, file end return File.expand_path(file) end
Ensure a directory exists @param directory [String] Directory to create or verify @return [String]
# File lib/doable/helpers/os.rb, line 18 def find_or_create_directory(directory) full_path = File.expand_path(directory) FileUtils.mkdir_p(full_path) unless File.directory?(full_path) return full_path end
Used for backwards compatibility (just a wrapper for touch) @param file [String] File to create or verify
# File lib/doable/helpers/os.rb, line 84 def find_or_create_file(file) unless File.exists?(File.expand_path(file)) log "File '#{file}' does not seem to exist... creating it...", :warn touch File.expand_path(file) end return File.expand_path(file) end
Confirms the current script is running as a certain user @raise [WrongUser] @param user [String] User to confirm we’re using
# 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 a file or directory @param file_or_directory [String] File or directory path to remove
# File lib/doable/helpers/os.rb, line 50 def remove(file_or_directory) FileUtils.rm_rf(File.expand_path(file_or_directory)) end
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. @raise [InvalidInput] @param substitutions [Hash] Hash of Strings indexing Strings. Keys are replaced with their value. @param input_file [IO,String] IO object or String to use as the source @param output_file [IO,String] IO object or String to use as the destination
# 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
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. @param command [String] Command to run @return [Fixnum, String] Returns the exit code of the command, along with any output
# 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
Used like Unix touch @param file_list [Array<String>,String] List of files to create or verify
# File lib/doable/helpers/os.rb, line 78 def touch(file_list, options = {}) FileUtils.touch(file_list, options) end
Check that the OS
can resolve a hostname @raise [InvalidHostname] @param hostname [String] Hostname to validate is resolveable
# File lib/doable/helpers/os.rb, line 57 def validate_host(hostname) raise InvalidHostname, hostname unless Resolv.getaddress(hostname) end