Module: SystemModule

Overview

Interaction with the operating system

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?



11
12
13
14
15
16
17
18
# File 'lib/takeltau/lib/system.rb', line 11

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:



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

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



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

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



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

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.



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

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



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

def run(command)
  Dir.chdir(config.active['project_root_dir']) do
    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
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:



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

def run_and_capture(command)
  Dir.chdir(config.active['project_root_dir']) do
    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
end

#run_and_exit(command) ⇒ Object

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



97
98
99
100
101
102
# File 'lib/takeltau/lib/system.rb', line 97

def run_and_exit(command)
  Dir.chdir(config.active['project_root_dir']) do
    log.debug "Running command \"#{command}\" and exiting afterwards"
    exec command
  end
end

#run_and_fork(command) ⇒ Object

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



105
106
107
108
109
110
111
112
113
# File 'lib/takeltau/lib/system.rb', line 105

def run_and_fork(command)
  log.debug "Running command \"#{command}\" as a background process"
  job = fork do
    Dir.chdir(config.active['project_root_dir']) do
      exec command
    end
  end
  Process.detach(job)
end

#try(command) ⇒ Boolean

Run a command and return the result.

Returns:

  • (Boolean)

    success of command run



117
118
119
120
121
122
123
124
125
126
# File 'lib/takeltau/lib/system.rb', line 117

def try(command)
  Dir.chdir(config.active['project_root_dir']) do
    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
end

#write_file(file, content) ⇒ Object

Write content to file



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

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