class BackupMinister
Attributes
config[RW]
Public Class Methods
new(file_name = nil)
click to toggle source
# File lib/backup_minister.rb, line 14 def initialize(file_name = nil) @config_file_name = file_name || CONFIG_FILE_NAME @config = check_config_file_exists end
Public Instance Methods
backup_minister_installed?()
click to toggle source
Check if backup_minister is installed
@return [Bool]
# File lib/backup_minister.rb, line 44 def backup_minister_installed? software_installed?(APP_NAME) end
create_nested_directory(path)
click to toggle source
@return [Bool]
# File lib/backup_minister.rb, line 71 def create_nested_directory(path) result = false begin FileUtils::mkdir_p(path) LOGGER.debug "Directory `#{path}` created." result = true rescue Error => error LOGGER.error "Can't create directory `#{path}` with error #{error.message}." end result end
execute(command)
click to toggle source
Wrapper for `system` with check
@return [Bool] true if exit status is 0
# File lib/backup_minister.rb, line 59 def execute(command) system command code = $?.exitstatus if code > 0 LOGGER.warn "Failed to execute command `#{command}` with code #{code}." false else true end end
execute_with_result(command, arguments = [])
click to toggle source
# File lib/backup_minister.rb, line 85 def execute_with_result(command, arguments = []) out, st = Open3.capture2(command, arguments) LOGGER.warn "Failed to execute command `#{command}` with code #{st.exitstatus}." unless st.success? out end
file_sha256_hash(file_path)
click to toggle source
Get SHA256 Hash of file
@param file_path [String] path to file
@return [String, nil]
# File lib/backup_minister.rb, line 96 def file_sha256_hash(file_path) file = File.read(file_path) Digest::SHA256.hexdigest(file) if file end
project_config(name: nil, index: nil)
click to toggle source
Get project configuration
@param name: [Symbol] Project name @param index: [Integer] Project index
@return [Hash]
# File lib/backup_minister.rb, line 30 def project_config(name: nil, index: nil) raise ArgumentError, 'At least one of arguments required' if name.nil? and index.nil? project_name = name.nil? ? @projects[index] : name.to_s if project_name and @config['projects'][project_name] @config['projects'][project_name] else raise ArgumentError, "No project #{name ? name : index} found." end end
software_installed?(name)
click to toggle source
Check if software is installed
@return [Bool]
# File lib/backup_minister.rb, line 51 def software_installed?(name) result = execute_with_result('type', name) result and /\A(#{name})(.)+(#{name})$/.match(result).to_a.count >= 3 end
system_config(*path)
click to toggle source
# File lib/backup_minister.rb, line 19 def system_config(*path) path.unshift 'settings' @config.dig(*path) end
Private Instance Methods
check_config_file_exists()
click to toggle source
# File lib/backup_minister.rb, line 105 def check_config_file_exists if File.exists?(@config_file_name) YAML.load File.read(@config_file_name) else raise RuntimeError, "No config file #{@config_file_name} found" end end