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
- #command_options_to_string(command, options) ⇒ Object
- #deprecate(message) ⇒ Object
- #env(var) ⇒ Object
-
#error(message) ⇒ Object
display error message and stop the build process.
- #noshellescape(string) ⇒ Object
-
#pipe(command, options = {}) ⇒ Object
run a shell command and stream the output.
- #pipe!(command, options = {}) ⇒ Object
-
#puts(message) ⇒ Object
display a message in line (indented by 6 spaces).
-
#run(command, options = {}) ⇒ Object
run a shell command and pipe stderr to stdout.
- #run!(command, options = {}) ⇒ Object
-
#run_no_pipe(command, options = {}) ⇒ String
doesn’t do any special piping.
-
#run_stdout(command, options = {}) ⇒ String
run a shell command and pipe stderr to /dev/null.
-
#topic(message) ⇒ Object
display a topic message (denoted by —–>).
- #user_env_hash ⇒ Object
- #warn(message, options = {}) ⇒ Object
Class Method Details
.blacklist?(key) ⇒ 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_hash ⇒ Object
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, ) [:env] ||= {} [:out] ||= "2>&1" [:env] = user_env_hash.merge([:env]) if [:user_env] env = [: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() @deprecations ||= [] @deprecations << 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
38 39 40 41 42 43 44 45 46 |
# File 'lib/pebblescape/receiver/shell_helpers.rb', line 38 def error() Kernel.puts " !" .split("\n").each do |line| Kernel.puts " ! #{line.strip}" end Kernel.puts " !" log "exit", :error => 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
88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/pebblescape/receiver/shell_helpers.rb', line 88 def pipe(command, = {}) output = "" IO.popen((command, )) do |io| until io.eof? buffer = io.gets output << buffer [: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, = {}) pipe(command, ) exit 1 unless $?.success? end |
#puts(message) ⇒ Object
display a message in line (indented by 6 spaces)
117 118 119 120 121 122 |
# File 'lib/pebblescape/receiver/shell_helpers.rb', line 117 def puts() .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
66 67 68 |
# File 'lib/pebblescape/receiver/shell_helpers.rb', line 66 def run(command, = {}) %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, = {}) result = run(command, ) 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.
57 58 59 |
# File 'lib/pebblescape/receiver/shell_helpers.rb', line 57 def run_no_pipe(command, = {}) run(command, .merge({:out => ""})) end |
#run_stdout(command, options = {}) ⇒ String
run a shell command and pipe stderr to /dev/null
73 74 75 76 |
# File 'lib/pebblescape/receiver/shell_helpers.rb', line 73 def run_stdout(command, = {}) [:out] ||= '2>/dev/null' run(command, ) end |
#topic(message) ⇒ Object
display a topic message (denoted by —–>)
109 110 111 112 |
# File 'lib/pebblescape/receiver/shell_helpers.rb', line 109 def topic() Kernel.puts "-----> #{message}" $stdout.flush end |
#user_env_hash ⇒ Object
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(, = {}) if .key?(:inline) ? [:inline] : false topic "Warning:" puts end @warnings ||= [] @warnings << end |