Module: Pebblescape::Receiver::ShellHelpers

Included in:
Receiver
Defined in:
lib/pebblescape/receiver/shell_helpers.rb

Constant Summary collapse

@@user_env_hash =
{}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.blacklist?(key) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/pebblescape/receiver/shell_helpers.rb', line 21

def self.blacklist?(key)
  %w(PATH GEM_PATH GEM_HOME GIT_DIR).include?(key)
end

.initialize_env(path) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/pebblescape/receiver/shell_helpers.rb', line 25

def self.initialize_env(path)
  env_dir = Pathname.new("#{path}")
  if env_dir.exist? && env_dir.directory?
    env_dir.each_child do |file|
      key   = file.basename.to_s
      value = file.read.strip
      user_env_hash[key] = value unless blacklist?(key)
    end
  end
end

.user_env_hashObject



9
10
11
# File 'lib/pebblescape/receiver/shell_helpers.rb', line 9

def self.user_env_hash
  @@user_env_hash
end

Instance Method Details

#command_options_to_string(command, options) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/pebblescape/receiver/shell_helpers.rb', line 78

def command_options_to_string(command, options)
  options[:env] ||= {}
  options[:out] ||= "2>&1"
  options[:env] = user_env_hash.merge(options[:env]) if options[:user_env]
  env = options[:env].map {|key, value| "#{key.shellescape}=#{value.shellescape}" }.join(" ")
  "/usr/bin/env #{env} bash -c #{command.shellescape} #{options[:out]} "
end

#deprecate(message) ⇒ Object



133
134
135
136
# File 'lib/pebblescape/receiver/shell_helpers.rb', line 133

def deprecate(message)
  @deprecations ||= []
  @deprecations << message
end

#env(var) ⇒ Object



17
18
19
# File 'lib/pebblescape/receiver/shell_helpers.rb', line 17

def env(var)
  ENV[var] || user_env_hash[var]
end

#error(message) ⇒ Object

display error message and stop the build process

Parameters:

  • error (String)

    message



38
39
40
41
42
43
44
45
46
# File 'lib/pebblescape/receiver/shell_helpers.rb', line 38

def error(message)
  Kernel.puts " !"
  message.split("\n").each do |line|
    Kernel.puts " !     #{line.strip}"
  end
  Kernel.puts " !"
  log "exit", :error => message if respond_to?(:log)
  exit 1
end

#noshellescape(string) ⇒ Object



138
139
140
# File 'lib/pebblescape/receiver/shell_helpers.rb', line 138

def noshellescape(string)
  NoShellEscape.new(string)
end

#pipe(command, options = {}) ⇒ Object

run a shell command and stream the output

Parameters:

  • command (String)

    to be run



88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/pebblescape/receiver/shell_helpers.rb', line 88

def pipe(command, options = {})
  output = ""
  IO.popen(command_options_to_string(command, options)) do |io|
    until io.eof?
      buffer = io.gets
      output << buffer
      options[:no_indent] ? Kernel.puts(buffer) : puts(buffer)
    end
  end

  output
end

#pipe!(command, options = {}) ⇒ Object



101
102
103
104
# File 'lib/pebblescape/receiver/shell_helpers.rb', line 101

def pipe!(command, options = {})
  pipe(command, options)
  exit 1 unless $?.success?
end

#puts(message) ⇒ Object

display a message in line (indented by 6 spaces)

Parameters:

  • message (String)

    to be displayed



117
118
119
120
121
122
# File 'lib/pebblescape/receiver/shell_helpers.rb', line 117

def puts(message)
  message.split("\n").each do |line|
    super "       #{line.strip}"
  end
  $stdout.flush
end

#run(command, options = {}) ⇒ Object

run a shell command and pipe stderr to stdout

Parameters:

  • command (String)
  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :out (String)

    the IO redirect of the command

  • :env (Hash)

    explicit environment to run command in

  • :user_env (Boolean)

    whether or not a user’s environment variables will be loaded



66
67
68
# File 'lib/pebblescape/receiver/shell_helpers.rb', line 66

def run(command, options = {})
  %x{ #{command_options_to_string(command, options)} }
end

#run!(command, options = {}) ⇒ Object



48
49
50
51
52
# File 'lib/pebblescape/receiver/shell_helpers.rb', line 48

def run!(command, options = {})
  result = run(command, options)
  error("Command: '#{command}' failed unexpectedly:\n#{result}") unless $?.success?
  return result
end

#run_no_pipe(command, options = {}) ⇒ String

doesn’t do any special piping. stderr won’t be redirected.

Parameters:

  • command (String)

    to be run

Returns:

  • (String)

    output of stdout



57
58
59
# File 'lib/pebblescape/receiver/shell_helpers.rb', line 57

def run_no_pipe(command, options = {})
  run(command, options.merge({:out => ""}))
end

#run_stdout(command, options = {}) ⇒ String

run a shell command and pipe stderr to /dev/null

Parameters:

  • command (String)

    to be run

Returns:

  • (String)

    output of stdout



73
74
75
76
# File 'lib/pebblescape/receiver/shell_helpers.rb', line 73

def run_stdout(command, options = {})
  options[:out] ||= '2>/dev/null'
  run(command, options)
end

#topic(message) ⇒ Object

display a topic message (denoted by —–>)

Parameters:

  • topic (String)

    message to be displayed



109
110
111
112
# File 'lib/pebblescape/receiver/shell_helpers.rb', line 109

def topic(message)
  Kernel.puts "-----> #{message}"
  $stdout.flush
end

#user_env_hashObject



13
14
15
# File 'lib/pebblescape/receiver/shell_helpers.rb', line 13

def user_env_hash
  @@user_env_hash
end

#warn(message, options = {}) ⇒ Object



124
125
126
127
128
129
130
131
# File 'lib/pebblescape/receiver/shell_helpers.rb', line 124

def warn(message, options = {})
  if options.key?(:inline) ? options[:inline] : false
    topic "Warning:"
    puts message
  end
  @warnings ||= []
  @warnings << message
end