Module: SystemModule

Overview

Interaction with the operating system rubocop:disable Metrics/ModuleLength

Instance Method Summary collapse

Instance Method Details

#command_available_else_error?(command) ⇒ Boolean

Check if a command is available else log error message

Returns:

  • (Boolean)

    is the command available?



12
13
14
15
16
17
18
19
# File 'lib/takelage/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) ⇒ Boolean

Check if a command is available else log warning message

Returns:

  • (Boolean)

    is the command available?



23
24
25
26
27
28
29
30
# File 'lib/takelage/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) ⇒ String

Convert hash to yaml.

Returns:



34
35
36
37
38
# File 'lib/takelage/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) ⇒ Hash

Read yaml file with erb templates.

Returns:

  • (Hash)

    content of yaml file



53
54
55
56
57
58
59
60
61
# File 'lib/takelage/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) ⇒ Hash

Read yaml file.

Returns:

  • (Hash)

    content of yaml file



42
43
44
45
46
47
48
49
# File 'lib/takelage/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) ⇒ Object

Remove directory tree.



64
65
66
67
68
69
70
71
# File 'lib/takelage/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) ⇒ String

Run a command and return the standard output.

Returns:

  • (String)

    stdout of command



75
76
77
78
79
80
81
82
# File 'lib/takelage/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) ⇒ [String, String, Integer]

Run a command and return the standard output the standard error and the exit status stdout, stderr, exitstatus of command

Returns:



88
89
90
91
92
93
94
95
# File 'lib/takelage/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) ⇒ Object

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



98
99
100
101
# File 'lib/takelage/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) ⇒ Object

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



104
105
106
107
108
109
110
# File 'lib/takelage/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) ⇒ Boolean

Run a command and return the result.

Returns:

  • (Boolean)

    success of command run



114
115
116
117
118
119
120
121
# File 'lib/takelage/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