class Blufin::Tools

Constants

OS_LINUX
OS_MAC
OS_OTHER
OS_UNIX
OS_WINDOWS

Public Class Methods

check_remote_is_reachable(host_address) click to toggle source

Check that remote host is reachable. @return void

# File lib/core/tools.rb, line 23
def self.check_remote_is_reachable(host_address)
    if ping(host_address) != 0
        Tools::Terminal::error('Cannot reach remote host', ["#{Tools::Terminal::format_highlight(host_address)} cannot be reached.", 'Please make sure the host is online and/or configured correctly.'])
    end
end
check_sshpass_is_installed() click to toggle source

Check that SSHPASS is installed. @return void

# File lib/core/tools.rb, line 39
def self.check_sshpass_is_installed
    if @sshpass_installed.nil?
        sshpass_result = Tools::Terminal::command_capture('sshpass -h', nil, false, false)
        sshpass_result = sshpass_result[0].split(' ')
        unless sshpass_result[0].downcase == 'usage:'
            if this_is_a_mac
                error_message = "Find how to install it at: #{Tools::Terminal::format_highlight('https://www.google.co.uk/search?q=install+sshpass+on+mac')}"
            else
                error_message = "Install it using: #{Tools::Terminal::format_command('sudo apt-get install sshpass')}"
            end
            Tools::Terminal::error("#{Tools::Terminal::format_highlight('sshpass')} is not installed", error_message, true)
        end
        @sshpass_installed = true
    end
end
get_base_path() click to toggle source

Get PATH to assets, scripts, etc. @return String

# File lib/core/tools.rb, line 15
def self.get_base_path
    base_path = File.dirname(File.expand_path(__FILE__))
    base_path = base_path.gsub(/\/\w+\/\w+\z/i, '')
    base_path
end
get_char_at(char_at, string) click to toggle source

Get the character at a specific index in a string. @return string

# File lib/core/tools.rb, line 95
def self.get_char_at(char_at, string)
    if is_whole_number(char_at)
        char_at = char_at.to_i
        char_at = (char_at - 1)
        string[char_at]
    else
        raise(RuntimeError, "The value for CharAt must be a whole number. The script received (#{char_at.class}) #{char_at}.")
    end
end
is_whole_number(value) click to toggle source

Checks if a String or Integer is a whole number, @return boolean

# File lib/core/tools.rb, line 83
def self.is_whole_number(value)
    if value =~ /^\s*\d+\s*$/
        true
    elsif value.is_a? Integer
        true
    else
        false
    end
end
os() click to toggle source

Get the operating system. @return String

# File lib/core/tools.rb, line 63
def self.os
    @os ||= (
    host_os = RbConfig::CONFIG['host_os']
    case host_os
        when /mswin|msys|mingw|cygwin|bccwin|wince|emc/
            OS_WINDOWS
        when /darwin|mac os/
            OS_MAC
        when /linux/
            OS_LINUX
        when /solaris|bsd/
            OS_UNIX
        else
            OS_OTHER
    end
    )
end
os_not_supported(array_of_os) click to toggle source

Throws uniform error for non-supported os'. @return void

# File lib/core/tools.rb, line 133
def self.os_not_supported(array_of_os)
    raise RuntimeError, "Expected Array, instead got: #{array_of_os.class}" unless array_of_os.is_a?(Array)
    array_of_os.each { |os| raise RuntimeError, "Unsupported OS value: #{Blufin::Terminal::format_invalid(os)}" unless [OS_MAC, OS_WINDOWS, OS_LINUX, OS_UNIX].include?(os) }
    current_os = os
    if array_of_os.include?(current_os)
        Blufin::Terminal::error("#{Blufin::Terminal::format_highlight(current_os.capitalize)} is not supported to run this command/operation.", 'Someone needs to program it and make it compatible.')
    end
end
ping(host_address, verbose = true) click to toggle source

Ping a URL or IP and returns the exit status. 0 = success, anything else means it failed. @return Integer

# File lib/core/tools.rb, line 31
def self.ping(host_address, verbose = true)
    Tools::Terminal::output("Checking if #{Tools::Terminal::format_highlight(host_address)} is reachable...") if verbose == true
    `ping -t 1 -c 1 #{host_address}`
    $?.exitstatus
end
this_is_a_mac() click to toggle source

Returns TRUE if Mac, FALSE if Linux (or anything else for that matter) @return boolean

# File lib/core/tools.rb, line 57
def self.this_is_a_mac
    return os == OS_MAC
end
value_based_on_os(mac: nil, windows: nil, linux: nil) click to toggle source

Returns a value based on OS. If any of the values are nil, throws an Exception. Linux supports both LINUX and UNIX enums. @return string

# File lib/core/tools.rb, line 108
def self.value_based_on_os(mac: nil, windows: nil, linux: nil)
    begin
        case Blufin::Tools::os
            when Blufin::Tools::OS_WINDOWS
                raise RuntimeError if windows.nil?
                return windows
            when Blufin::Tools::OS_MAC
                raise RuntimeError if mac.nil?
                return mac
            when Blufin::Tools::OS_LINUX
                raise RuntimeError if linux.nil?
                return linux
            when Blufin::Tools::OS_UNIX
                raise RuntimeError if linux.nil?
                return linux
            else
                raise RuntimeError
        end
    rescue => e
        Blufin::Terminal::error("This command/operation is not yet supported on: #{Blufin::Terminal::format_highlight(Blufin::Tools::os)}", e.split("\n"), true)
    end
end