Module: Asgard::Shell

Included in:
Base
Defined in:
lib/asgard/shell.rb

Instance Method Summary collapse

Instance Method Details

#sh(script, silent: false) ⇒ Object

Run a shell script. Multiline strings are passed to bash -c; single-line strings are passed to system directly. Exits with the command’s status code on failure.



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/asgard/shell.rb', line 10

def sh(script, silent: false)
  script = script.strip
  $stdout.puts script unless silent

  success = if script.include?("\n")
    system("bash", "-c", script)
  else
    system(script)
  end

  exit($?.exitstatus) unless success
end

#shebang(interpreter, script, silent: false) ⇒ Object

Write script to a tempfile and execute it with interpreter. Useful for embedding Python, Node, Ruby, or any shebang-style body.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/asgard/shell.rb', line 25

def shebang(interpreter, script, silent: false)
  extensions = {
    python3: ".py", python: ".py",
    node:    ".js",
    ruby:    ".rb",
    perl:    ".pl",
    bash:    ".sh", sh: ".sh"
  }
  ext = extensions.fetch(interpreter.to_sym, ".tmp")

  Tempfile.create(["asgard_", ext]) do |f|
    f.write(script)
    f.flush
    system(interpreter.to_s, f.path)
    exit($?.exitstatus) unless $?.success?
  end
end