module SystemModule

Interaction with the operating system rubocop:disable Metrics/ModuleLength

Public Instance Methods

command_available_else_error?(command) click to toggle source

Check if a command is available else log error message @return [Boolean] is the command available?

# File lib/takeltau/lib/system.rb, line 12
def command_available_else_error?(command)
  unless _command_available? command
    log.error "The command \"#{command}\" is not available"
    return false
  end

  command_available command
end
command_available_else_warn?(command) click to toggle source

Check if a command is available else log warning message @return [Boolean] is the command available?

# File lib/takeltau/lib/system.rb, line 23
def command_available_else_warn?(command)
  unless _command_available? command
    log.warn "The command \"#{command}\" is not available"
    return false
  end

  command_available command
end
hash_to_yaml(hash) click to toggle source

Convert hash to yaml. @return [String] yaml of hash

# File lib/takeltau/lib/system.rb, line 34
def hash_to_yaml(hash)
  return nil.to_yaml if hash == {}

  hash.to_yaml({ line_width: -1 })
end
read_yaml_erb_file(file) click to toggle source

Read yaml file with erb templates. @return [Hash] content of yaml file

# File lib/takeltau/lib/system.rb, line 53
def read_yaml_erb_file(file)
  log.debug "Reading YAML ERB file \"#{file}\""
  return nil unless _file_exists? file
  return nil unless _file_read file
  return nil unless _parse_erb file, @content_file
  return nil unless _parse_yaml file, @content_yaml

  @content
end
read_yaml_file(file) click to toggle source

Read yaml file. @return [Hash] content of yaml file

# File lib/takeltau/lib/system.rb, line 42
def read_yaml_file(file)
  log.debug "Reading YAML file \"#{file}\""
  return nil unless _file_exists? file
  return nil unless _file_read file
  return nil unless _parse_yaml file, @content_file

  @content
end
rm_fr(directory) click to toggle source

Remove directory tree.

# File lib/takeltau/lib/system.rb, line 64
def rm_fr(directory)
  unless File.directory? directory
    log.error "Cannot remove non-existing directory \"#{directory}\""
    return
  end
  log.debug "Removing directory \"#{directory}\" recursively"
  Pathname.new(directory).rmtree
end
run(command) click to toggle source

Run a command and return the standard output. @return [String] stdout of command

# File lib/takeltau/lib/system.rb, line 75
def run(command)
  log.debug "Running command \"#{command}\""
  stdout_str, stderr_str, status = Open3.capture3 command
  log.debug "Command \"#{command}\" has stdout:\n\"\"\"\n#{stdout_str}\"\"\""
  log.debug "Command \"#{command}\" has stderr:\n\"\"\"\n#{stderr_str}\"\"\""
  log.debug "Command \"#{command}\" has exit status: \"#{status.exitstatus}\""
  stdout_str
end
run_and_capture(command) click to toggle source

Run a command and return the standard output the standard error and the exit status @return [[String, String, Integer]] array of stdout, stderr, exitstatus of command

# File lib/takeltau/lib/system.rb, line 88
def run_and_capture(command)
  log.debug "Running amd capturing command \"#{command}\""
  stdout_str, stderr_str, status = Open3.capture3 command
  log.debug "Command \"#{command}\" has stdout:\n\"\"\"\n#{stdout_str}\"\"\""
  log.debug "Command \"#{command}\" has stderr:\n\"\"\"\n#{stderr_str}\"\"\""
  log.debug "Command \"#{command}\" has exit status: \"#{status.exitstatus}\""
  [stdout_str, stderr_str, status.exitstatus]
end
run_and_exit(command) click to toggle source

Use Kernel#exec to replace the ruby process with a command.

# File lib/takeltau/lib/system.rb, line 98
def run_and_exit(command)
  log.debug "Running command \"#{command}\" and exiting afterwards"
  exec command
end
run_and_fork(command) click to toggle source

Use Kernel#fork and Kernel#exec to run a command as a background process.

# File lib/takeltau/lib/system.rb, line 104
def run_and_fork(command)
  log.debug "Running command \"#{command}\" as a background process"
  job = fork do
    exec command
  end
  Process.detach(job)
end
try(command) click to toggle source

Run a command and return the result. @return [Boolean] success of command run

# File lib/takeltau/lib/system.rb, line 114
def try(command)
  log.debug "Running command \"#{command}\""
  stdout_str, stderr_str, status = Open3.capture3 command
  log.debug "Command \"#{command}\" has stdout:\n\"\"\"\n#{stdout_str}\"\"\""
  log.debug "Command \"#{command}\" has stderr:\n\"\"\"\n#{stderr_str}\"\"\""
  log.debug "Command \"#{command}\" has exit status: \"#{status.exitstatus}\""
  status
end

Private Instance Methods

_command_available?(command) click to toggle source

Check if command is available

# File lib/takeltau/lib/system.rb, line 126
def _command_available?(command)
  return true if instance_variable_get("@command_available_#{command}")

  log.debug "Check if the command \"#{command}\" is available"
  status = try "which #{command}"
  return false unless status.exitstatus.zero?

  true
end
_file_exists?(file) click to toggle source

Check if file exists.

# File lib/takeltau/lib/system.rb, line 143
def _file_exists?(file)
  unless File.exist? File.expand_path(file)
    log.debug "File \"#{file}\" doesn't exist"
    return false
  end
  true
end
_file_read(file) click to toggle source

Read yaml file.

# File lib/takeltau/lib/system.rb, line 152
def _file_read(file)
  begin
    @content_file = File.read File.expand_path(file)
  rescue SystemCallError
    log.debug "Unable to read file \"#{file}\""
    return false
  end
  true
end
_parse_erb(file, content_erb) click to toggle source

Parse erb file.

# File lib/takeltau/lib/system.rb, line 163
def _parse_erb(file, content_erb)
  begin
    @content_yaml = ERB.new(content_erb).result
  rescue StandardError => e
    log.debug e.class
    log.debug "Invalid ERB in YAML file \"#{file}\". " \
      "#{e.class}: \"#{e.message}\""
    return false
  end
  true
end
_parse_yaml(file, content_yaml) click to toggle source

Parse yaml file.

# File lib/takeltau/lib/system.rb, line 176
def _parse_yaml(file, content_yaml)
  begin
    @content = YAML.safe_load content_yaml
  rescue Psych::SyntaxError
    log.debug "Invalid YAML file \"#{file}\""
    log.debug "Try: yamllint #{file}"
    return false
  end
  true
end
command_available(command) click to toggle source

Command is available

# File lib/takeltau/lib/system.rb, line 137
def command_available(command)
  log.debug "The command \"#{command}\" is available"
  instance_variable_set("@command_available_#{command}", true)
end
pluralize(number, singular, plural) click to toggle source

Pluralize a verb in relation to a number

# File lib/takeltau/lib/system.rb, line 188
def pluralize(number, singular, plural)
  return singular if number == 1

  plural
end