module Kplay::System::ClassMethods

Class level methods

Constants

DEFAULT_SH_OPTS

Public Instance Methods

assert_program_presence!(name) click to toggle source

Checks if program with given name is present. Raises an error if the program is not found.

# File lib/kplay/system.rb, line 47
def assert_program_presence!(name)
  sh(['which', name], echo: false, output: false)
rescue
  raise "Failed to find required program: #{name}"
end
host_os() click to toggle source

Returns the OS identifier

@return [Symbol] :linux, :macosx or :unknown

# File lib/kplay/system.rb, line 66
def host_os
  name = `uname`.split(' ').first.downcase.to_sym
  case name
  when :linux
    :linux
  when :darwin
    :macosx
  else
    :unknown
  end
end
print(text) click to toggle source

Outputs a string to STDOUT without adding a line break

@param text [String]

sh(cmd, opts = {}) click to toggle source

Executes a shell command on the host

@param cmd [String] command to execute @param opts [Hash] @option opts [true,false] :echo echo command to stdout (default: true) @option opts [true,false] :output display (true) or suppress (false) commands output (default: true) @option opts [true,false] :tty attach a TTY (stdin is preserved and output is never suppressed), (default: false)

# File lib/kplay/system.rb, line 30
def sh(cmd, opts = {})
  opts = DEFAULT_SH_OPTS.merge(opts)
  cmd = [cmd] if cmd.is_a?(String)
  puts cmd.join(' ') if opts[:echo]
  exit_status = nil
  if opts[:tty]
    exit_status = system(*cmd) ? 0 : -1
  else
    out, status = Open3.capture2(*cmd)
    puts out if opts[:output]
    exit_status = status.exitstatus
  end
  raise "Failed to execute '#{cmd.join(' ')}' (#{exit_status})" unless exit_status == 0
end