Module: Obfusk::Util::Cmd

Defined in:
lib/obfusk/util/cmd.rb

Constant Summary collapse

SIG_RX =
/^(SIG[A-Z0-9]+)\s+(.*)$/
SH_RX =
/^SHELL(=(\S+))?\s+(.*)$/
VAR_RX =
/ \$ \{ ([A-Z_]+) \} /x

Class Method Summary collapse

Class Method Details

.env_to_a(h) ⇒ <String>

env hash as array (w/o nil values)

Returns:

  • (<String>)

    ['k1="v1"', ...]



63
64
65
# 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') ⇒ Hash

parses optional SIG* prefix in command string; (e.g. 'SIGINT foo bar ...'); if there is no prefix, signal is default

Returns:

  • (Hash)

    { command: command, signal: signal }



25
26
27
28
29
30
31
# 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) ⇒ Object

prepend nohup to args



49
50
51
# File 'lib/obfusk/util/cmd.rb', line 49

def self.nohup(*args)
  ['nohup'] + args
end

.set_vars(cmd, vars) ⇒ Object

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



55
56
57
# 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') ⇒ Hash

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

Returns:

  • (Hash)

    { command: command, shell: shell }



38
39
40
41
42
43
44
# 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