module Obfusk::Util::Cmd

Constants

SH_RX
SIG_RX
VAR_RX

Public Class Methods

env_to_a(h) click to toggle source

env hash as array (w/o nil values) @return [<String>] ‘[’k1=“v1”‘, …]`

# File lib/obfusk/util/cmd.rb, line 63
def self.env_to_a(h)
  h.reject { |k,v| v.nil? } .map { |k,v| "#{k}=#{v.inspect}" }
end
killsig(cmd, default = 'SIGTERM') click to toggle source

parses optional ‘SIG*` prefix in command string; (e.g. `’SIGINT foo bar …‘`); if there is no prefix, signal is default @return [Hash] `{ command: command, signal: signal }`

# File lib/obfusk/util/cmd.rb, line 25
def self.killsig(cmd, default = 'SIGTERM')                    # {{{1
  if m = cmd.match(SIG_RX)
    { command: m[2], signal: m[1] }
  else
    { command: cmd, signal: default }
  end
end
nohup(*args) click to toggle source

prepend nohup to args

# File lib/obfusk/util/cmd.rb, line 49
def self.nohup(*args)
  ['nohup'] + args
end
set_vars(cmd, vars) click to toggle source

replaces ‘${VAR}s` in command string using vars hash; missing values are replaced with empty strings

# File lib/obfusk/util/cmd.rb, line 55
def self.set_vars(cmd, vars)
  cmd.gsub(VAR_RX) { |m| vars[$1] }
end
shell(cmd, default = 'bash') click to toggle source

parses optional ‘SHELL` prefix in command string (e.g. `’SHELL=bash foo bar …‘`, `’SHELL foo bar …‘`); if there is no prefix, shell is nil; if there is no `=…`, shell is default @return [Hash] `{ command: command, shell: shell }`

# File lib/obfusk/util/cmd.rb, line 38
def self.shell(cmd, default = 'bash')                         # {{{1
  if m = cmd.match(SH_RX)
    { command: m[3], shell: (m[2] || default) }
  else
    { command: cmd, shell: nil }
  end
end