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/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

#hash_to_yaml(hash) ⇒ String

Convert hash to yaml.

Returns:



23
24
25
26
27
# File 'lib/takeltau/lib/system.rb', line 23

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



42
43
44
45
46
47
48
49
50
# File 'lib/takeltau/lib/system.rb', line 42

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 file, @content_file
  return nil unless _parse_yaml_file file, @content_yaml

  @content
end

#read_yaml_file(file) ⇒ Hash

Read yaml file.

Returns:

  • (Hash)

    content of yaml file



31
32
33
34
35
36
37
38
# File 'lib/takeltau/lib/system.rb', line 31

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 file, @content_file

  @content
end

#rm_fr(directory) ⇒ Object

Remove directory tree.



60
61
62
63
64
65
66
67
# File 'lib/takeltau/lib/system.rb', line 60

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



71
72
73
74
75
76
77
78
# File 'lib/takeltau/lib/system.rb', line 71

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:



84
85
86
87
88
89
90
91
# File 'lib/takeltau/lib/system.rb', line 84

def run_and_capture(command)
  log.debug "Running and 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.



94
95
96
97
# File 'lib/takeltau/lib/system.rb', line 94

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.



100
101
102
103
104
105
106
# File 'lib/takeltau/lib/system.rb', line 100

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



110
111
112
113
114
115
116
117
# File 'lib/takeltau/lib/system.rb', line 110

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

#write_file(file, content) ⇒ Object

Write content to file



53
54
55
56
57
# File 'lib/takeltau/lib/system.rb', line 53

def write_file(file, content)
  log.debug "Writing content to file \"#{file}\":"
  log.debug "\"#{content}\""
  File.write(file, content)
end