Module: NRSER::Rash::Helpers

Includes:
SemanticLogger::Loggable
Defined in:
lib/nrser/rash/helpers.rb

Overview

Helpful and not so helpful things.

Class Method Summary collapse

Class Method Details

.block(start, finish, &body) ⇒ Object



40
41
42
# File 'lib/nrser/rash/helpers.rb', line 40

def block(start, finish, &body)
  $stdout.block start, finish, &body
end

.double_quote(string) ⇒ Object Also known as: dq

double-quote a string for bash replaces " with \" and \ with \\



35
36
37
# File 'lib/nrser/rash/helpers.rb', line 35

def double_quote(string)
  '"' + string.gsub(/["\\]/, {'"' => '\"', '\\' => '\\\\'}) + '"'
end

.quote(value) ⇒ Object Also known as: q



21
22
23
# File 'lib/nrser/rash/helpers.rb', line 21

def quote(value)
  double_quote(value)
end

.single_quote(string) ⇒ Object Also known as: sq

single-quote a string for bash reaplces ' with '\''



28
29
30
# File 'lib/nrser/rash/helpers.rb', line 28

def single_quote(string)
  "'" + string.gsub(/'/, {"'" => "'\\''"}) + "'"
end

.user(name = nil, &body) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/nrser/rash/helpers.rb', line 44

def user name = nil, &body
  # if no name is provided, it just means 'not root'
  if name.nil?
    body.() if ENV['USER'] != 'root'
  else
    body.() if ENV['USER'] == name
  end
end