Module: Satorix::Shared::Console
- Extended by:
- Console
- Included in:
- Satorix, CI::Deploy::Flynn, CI::Shared::BuildpackManager, CI::Shared::BuildpackManager::Buildpack, CI::Shared::Ruby::GemManager, CI::Shared::YarnManager, CI::Test::Python::DjangoTest, CI::Test::Python::Safety, CI::Test::Ruby::Brakeman, CI::Test::Ruby::BundlerAudit, CI::Test::Ruby::Cucumber, CI::Test::Ruby::RailsTest, CI::Test::Ruby::Rspec, CI::Test::Ruby::Rubocop, CI::Test::Shared::Database, Console
- Defined in:
- lib/satorix/shared/console.rb
Instance Method Summary collapse
- #colorize(text, color = nil) ⇒ Object
- #colors ⇒ Object
- #humanize_time(seconds) ⇒ Object
- #log(text, color = nil) ⇒ Object
- #log_bench(message) ⇒ Object
- #log_command(text) ⇒ Object
- #log_duration(text) ⇒ Object
- #log_error(text) ⇒ Object
- #log_error_and_abort(text) ⇒ Object
- #log_header(text) ⇒ Object
-
#run_command(command, filtered_text: [], quiet: false, string_verified_safe: false) ⇒ Object
The preferred way to use this method is by passing an array containing the command.
-
#source_env_from(file) ⇒ Object
stackoverflow.com/questions/1197224/source-shell-script-into-environment-within-a-ruby-script TODO : reduce / consolidate? Find variables changed as a result of sourcing the given file, and update in ENV.
Instance Method Details
#colorize(text, color = nil) ⇒ Object
12 13 14 |
# File 'lib/satorix/shared/console.rb', line 12 def colorize(text, color = nil) "\033[#{ colors[color] }m#{ text }\033[0m" end |
#colors ⇒ Object
17 18 19 20 21 22 23 24 25 |
# File 'lib/satorix/shared/console.rb', line 17 def colors Hash.new(39).merge(red: '0;31', light_red: '1;31', green: '0;32', light_green: '1;32', yellow: '0;33', light_yellow: '1;33', blue: '0;34', light_blue: '1;34', magenta: '0;35', light_magenta: '1;35', cyan: '0;36', light_cyan: '1;36', white: '0;37', light_white: '1;37') end |
#humanize_time(seconds) ⇒ Object
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/satorix/shared/console.rb', line 28 def humanize_time(seconds) return 'less than 1 second' if seconds < 1 [[60, :second], [60, :minute], [24, :hour], [1000, :day]].map do |count, name| if seconds > 0 seconds, n = seconds.divmod(count) n = n.to_i "#{ n } #{ name }#{ 's' if n > 1 }" if n > 0 end end.compact.reverse.join(' ') end |
#log(text, color = nil) ⇒ Object
40 41 42 |
# File 'lib/satorix/shared/console.rb', line 40 def log(text, color = nil) puts color ? colorize(text, color) : text end |
#log_bench(message) ⇒ Object
71 72 73 74 75 76 |
# File 'lib/satorix/shared/console.rb', line 71 def log_bench() log_header result = nil log_duration "Time elapsed: #{ humanize_time Benchmark.realtime { result = yield } }." result end |
#log_command(text) ⇒ Object
45 46 47 |
# File 'lib/satorix/shared/console.rb', line 45 def log_command(text) log text, :cyan end |
#log_duration(text) ⇒ Object
50 51 52 |
# File 'lib/satorix/shared/console.rb', line 50 def log_duration(text) log text, :light_cyan end |
#log_error(text) ⇒ Object
60 61 62 |
# File 'lib/satorix/shared/console.rb', line 60 def log_error(text) log text, :light_red end |
#log_error_and_abort(text) ⇒ Object
65 66 67 68 |
# File 'lib/satorix/shared/console.rb', line 65 def log_error_and_abort(text) log_error text abort text end |
#log_header(text) ⇒ Object
55 56 57 |
# File 'lib/satorix/shared/console.rb', line 55 def log_header(text) log("\n#{ text }", :light_green) end |
#run_command(command, filtered_text: [], quiet: false, string_verified_safe: false) ⇒ Object
The preferred way to use this method is by passing an array containing the command. This will ensure that items are properly escaped. It is also possible to use nested arrays. See tests for example use cases.
If you want to see how your string command translates into an arry, you can use shellsplit method.
For example: ‘ruby -v’.shellsplit
=> ["ruby", "-v"]
If, for some reason, you need to supply a string that is already escaped or should not be escaped, it is possible to supply a string argument instead of an array. If you do this, you will need to supply an additional parameter of string_verified_safe, to ensure that the operation is deliberate.
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/satorix/shared/console.rb', line 92 def run_command(command, filtered_text: [], quiet: false, string_verified_safe: false) # This method is used *EVERYWHERE*. # Seemingly small changes can have far-ranging and extreme consequences. # Modify only with extreme caution. # # Note: Many applications (like echo and the Flynn CLI) append a newline to their output. # If you are using the command result, it may be desirable to chomp the trailing # newline. It is left to the implementing call to handle this, as it proved # cumbersome to handle in this method in a way that worked perfectly in all cases. raise('Potentially unsafe or problematic command. Please refer to the method documentation for more information.') if command.is_a?(String) && !string_verified_safe command = normalized_command(command) logged_command = logged_command(command, filtered_text) log_command(logged_command) unless quiet ''.tap do |output| IO.popen(bash(command)) do |io| until io.eof? line = io.gets puts line unless quiet || line.empty? $stdout.flush output.concat line end end handle_run_command_error(logged_command, quiet) unless $CHILD_STATUS.success? end end |
#source_env_from(file) ⇒ Object
stackoverflow.com/questions/1197224/source-shell-script-into-environment-within-a-ruby-script TODO : reduce / consolidate? Find variables changed as a result of sourcing the given file, and update in ENV.
125 126 127 |
# File 'lib/satorix/shared/console.rb', line 125 def source_env_from(file) bash_source(file).each { |k, v| ENV[k] = v } end |