Module: Mortar::Helpers
- Extended by:
- Helpers
- Included in:
- Auth, CLI, Command, Command::Base, Command::Plugins, Command::Version, Generators::Base, Helpers, Local::InstallUtil, Local::Jython, Local::Pig, Local::Python, Local::Sqoop, Plugin, Plugin, S3::S3
- Defined in:
- lib/mortar/helpers.rb
Constant Summary collapse
- @@kb =
1024- @@mb =
1024 * @@kb
- @@gb =
1024 * @@mb
Class Method Summary collapse
- .error_with_failure ⇒ Object
- .error_with_failure=(new_error_with_failure) ⇒ Object
- .extended(base) ⇒ Object
- .extended_into ⇒ Object
- .included(base) ⇒ Object
- .included_into ⇒ Object
Instance Method Summary collapse
-
#action(message, options = {}) ⇒ Object
DISPLAY HELPERS.
- #ask ⇒ Object
- #confirm(message = "Are you sure you wish to continue? (y/n)?") ⇒ Object
- #copy_if_not_present_at_dest(res_src, res_dest) ⇒ Object
- #default_host ⇒ Object
- #deprecate(message) ⇒ Object
- #display(msg = "", new_line = true) ⇒ Object
- #display_header(message = "", new_line = true) ⇒ Object
- #display_object(object) ⇒ Object
- #display_row(row, lengths) ⇒ Object
- #display_table(objects, columns, headers) ⇒ Object
- #display_with_indent(indent, msg = "", new_line = true) ⇒ Object
- #download_to_file(url, path, mkdir_p = true) ⇒ Object
- #ensure_dir_exists(dir) ⇒ Object
- #error(message) ⇒ Object
- #format_bytes(amount) ⇒ Object
- #format_date(date) ⇒ Object
- #format_with_bang(message) ⇒ Object
- #full_host ⇒ Object
- #get_terminal_environment ⇒ Object
- #home_directory ⇒ Object
- #host ⇒ Object
- #hprint(string = '') ⇒ Object
- #hputs(string = '') ⇒ Object
- #installed_with_omnibus? ⇒ Boolean
- #json_decode(json) ⇒ Object
- #json_encode(object) ⇒ Object
-
#line_formatter(array) ⇒ Object
produces a printf formatter line for an array of items if an individual line item is an array, it will create columns that are lined-up.
- #longest(items) ⇒ Object
- #output_with_bang(message = "", new_line = true) ⇒ Object
- #pending_github_team_state_message(invite_url) ⇒ Object
- #quantify(string, num) ⇒ Object
- #redisplay(line, line_break = false) ⇒ Object
- #retry_on_exception(*exceptions) ⇒ Object
- #running_on_a_mac? ⇒ Boolean
- #running_on_windows? ⇒ Boolean
- #set_buffer(enable) ⇒ Object
- #shell(cmd) ⇒ Object
- #spinner(ticks) ⇒ Object
- #status(message) ⇒ Object
- #string_distance(first, last) ⇒ Object
- #styled_array(array, options = {}) ⇒ Object
- #styled_error(error, message = 'Mortar Development Framework internal error.') ⇒ Object
- #styled_hash(hash, keys = nil, indent = 0) ⇒ Object
- #styled_header(header) ⇒ Object
- #suggestion(actual, possibilities) ⇒ Object
- #test_name ⇒ Object
- #ticking(sleep_time) ⇒ Object
- #time_ago(elapsed) ⇒ Object
- #truncate(text, length) ⇒ Object
- #warning(msg = "", new_line = true) ⇒ Object
- #with_tty(&block) ⇒ Object
- #write_to_file(str_data, path, mkdir_p = true) ⇒ Object
Class Method Details
.error_with_failure ⇒ Object
283 284 285 |
# File 'lib/mortar/helpers.rb', line 283 def self.error_with_failure @@error_with_failure ||= false end |
.error_with_failure=(new_error_with_failure) ⇒ Object
287 288 289 |
# File 'lib/mortar/helpers.rb', line 287 def self.error_with_failure=(new_error_with_failure) @@error_with_failure = new_error_with_failure end |
.extended(base) ⇒ Object
303 304 305 |
# File 'lib/mortar/helpers.rb', line 303 def self.extended(base) extended_into << base end |
.extended_into ⇒ Object
295 296 297 |
# File 'lib/mortar/helpers.rb', line 295 def self.extended_into @@extended_into ||= [] end |
.included(base) ⇒ Object
299 300 301 |
# File 'lib/mortar/helpers.rb', line 299 def self.included(base) included_into << base end |
.included_into ⇒ Object
291 292 293 |
# File 'lib/mortar/helpers.rb', line 291 def self.included_into @@included_into ||= [] end |
Instance Method Details
#action(message, options = {}) ⇒ Object
DISPLAY HELPERS
246 247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/mortar/helpers.rb', line 246 def action(, ={}) display("#{}... ", false) Mortar::Helpers.error_with_failure = true ret = yield Mortar::Helpers.error_with_failure = false display(([:success] || "done"), false) if @status display(", #{@status}", false) @status = nil end display ret end |
#ask ⇒ Object
123 124 125 |
# File 'lib/mortar/helpers.rb', line 123 def ask $stdin.gets.to_s.strip end |
#confirm(message = "Are you sure you wish to continue? (y/n)?") ⇒ Object
113 114 115 116 |
# File 'lib/mortar/helpers.rb', line 113 def confirm(="Are you sure you wish to continue? (y/n)?") display("#{} ", false) ['y', 'yes'].include?(ask.downcase) end |
#copy_if_not_present_at_dest(res_src, res_dest) ⇒ Object
527 528 529 530 531 |
# File 'lib/mortar/helpers.rb', line 527 def copy_if_not_present_at_dest(res_src, res_dest) unless File.exists?(res_dest) FileUtils.cp(res_src, res_dest) end end |
#default_host ⇒ Object
41 42 43 |
# File 'lib/mortar/helpers.rb', line 41 def default_host "mdog.datadoghq.com" end |
#deprecate(message) ⇒ Object
109 110 111 |
# File 'lib/mortar/helpers.rb', line 109 def deprecate() display "WARNING: #{}" end |
#display(msg = "", new_line = true) ⇒ Object
86 87 88 89 90 91 92 93 |
# File 'lib/mortar/helpers.rb', line 86 def display(msg="", new_line=true) if new_line puts(msg) else print(msg) $stdout.flush end end |
#display_header(message = "", new_line = true) ⇒ Object
307 308 309 310 |
# File 'lib/mortar/helpers.rb', line 307 def display_header(="", new_line=true) return if .to_s.strip == "" display("=== " + .to_s.split("\n").join("\n=== "), new_line) end |
#display_object(object) ⇒ Object
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 |
# File 'lib/mortar/helpers.rb', line 312 def display_object(object) case object when Array # list of objects object.each do |item| display_object(item) end when Hash # if all values are arrays, it is a list with headers # otherwise it is a single header with pairs of data if object.values.all? {|value| value.is_a?(Array)} object.keys.sort_by {|key| key.to_s}.each do |key| display_header(key) display_object(object[key]) hputs end end else hputs(object.to_s) end end |
#display_row(row, lengths) ⇒ Object
198 199 200 201 202 203 204 205 |
# File 'lib/mortar/helpers.rb', line 198 def display_row(row, lengths) row_data = [] row.zip(lengths).each do |column, length| format = column.is_a?(Fixnum) ? "%#{length}s" : "%-#{length}s" row_data << format % column end display(row_data.join(" ")) end |
#display_table(objects, columns, headers) ⇒ Object
183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/mortar/helpers.rb', line 183 def display_table(objects, columns, headers) lengths = [] columns.each_with_index do |column, index| header = headers[index] lengths << longest([header].concat(objects.map { |o| o[column].to_s })) end lines = lengths.map {|length| "-" * length} lengths[-1] = 0 # remove padding from last column display_row headers, lengths display_row lines, lengths objects.each do |row| display_row columns.map { |column| row[column] }, lengths end end |
#display_with_indent(indent, msg = "", new_line = true) ⇒ Object
429 430 431 |
# File 'lib/mortar/helpers.rb', line 429 def display_with_indent(indent, msg="", new_line=true) display("#{"".ljust(indent)}#{msg.to_s}", new_line) end |
#download_to_file(url, path, mkdir_p = true) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/mortar/helpers.rb', line 68 def download_to_file(url, path, mkdir_p=true) content_length = 0 set_content_length = lambda do |l| content_length = l end set_progress = lambda do |p| redisplay("Downloading #{path}: #{p.to_i} of #{content_length.to_i}") end open(url, :content_length_proc => set_content_length, :progress_proc => set_progress) do |f| write_to_file(f.read, path, mkdir_p) end end |
#ensure_dir_exists(dir) ⇒ Object
521 522 523 524 525 |
# File 'lib/mortar/helpers.rb', line 521 def ensure_dir_exists(dir) unless File.directory? dir Dir.mkdir(dir) end end |
#error(message) ⇒ Object
274 275 276 277 278 279 280 281 |
# File 'lib/mortar/helpers.rb', line 274 def error() if Mortar::Helpers.error_with_failure display("failed") Mortar::Helpers.error_with_failure = false end $stderr.puts(format_with_bang()) exit(1) end |
#format_bytes(amount) ⇒ Object
166 167 168 169 170 171 172 173 |
# File 'lib/mortar/helpers.rb', line 166 def format_bytes(amount) amount = amount.to_i return '(empty)' if amount == 0 return amount if amount < @@kb return "#{(amount / @@kb).round}k" if amount < @@mb return "#{(amount / @@mb).round}M" if amount < @@gb return "#{(amount / @@gb).round}G" end |
#format_date(date) ⇒ Object
118 119 120 121 |
# File 'lib/mortar/helpers.rb', line 118 def format_date(date) date = Time.parse(date) if date.is_a?(String) date.strftime("%Y-%m-%d %H:%M %Z") end |
#format_with_bang(message) ⇒ Object
264 265 266 267 |
# File 'lib/mortar/helpers.rb', line 264 def format_with_bang() return '' if .to_s.strip == "" " ! " + .split("\n").join("\n ! ") end |
#full_host ⇒ Object
49 50 51 |
# File 'lib/mortar/helpers.rb', line 49 def full_host (host =~ /^http/) ? host : "https://#{host}" end |
#get_terminal_environment ⇒ Object
238 239 240 241 242 |
# File 'lib/mortar/helpers.rb', line 238 def get_terminal_environment { "TERM" => ENV["TERM"], "COLUMNS" => `tput cols`.strip, "LINES" => `tput lines`.strip } rescue { "TERM" => ENV["TERM"] } end |
#home_directory ⇒ Object
29 30 31 |
# File 'lib/mortar/helpers.rb', line 29 def home_directory running_on_windows? ? ENV['USERPROFILE'].gsub("\\","/") : ENV['HOME'] end |
#host ⇒ Object
45 46 47 |
# File 'lib/mortar/helpers.rb', line 45 def host ENV['MORTAR_HOST'] || default_host end |
#hprint(string = '') ⇒ Object
338 339 340 341 |
# File 'lib/mortar/helpers.rb', line 338 def hprint(string='') Kernel.print(string) $stdout.flush end |
#hputs(string = '') ⇒ Object
334 335 336 |
# File 'lib/mortar/helpers.rb', line 334 def hputs(string='') Kernel.puts(string) end |
#installed_with_omnibus? ⇒ Boolean
57 58 59 |
# File 'lib/mortar/helpers.rb', line 57 def installed_with_omnibus? File.exists?('/opt/mortar/install/version-manifest.txt') end |
#json_decode(json) ⇒ Object
213 214 215 216 217 |
# File 'lib/mortar/helpers.rb', line 213 def json_decode(json) Mortar::OkJson.decode(json) rescue Mortar::OkJson::Error nil end |
#json_encode(object) ⇒ Object
207 208 209 210 211 |
# File 'lib/mortar/helpers.rb', line 207 def json_encode(object) Mortar::OkJson.encode(object) rescue Mortar::OkJson::Error nil end |
#line_formatter(array) ⇒ Object
produces a printf formatter line for an array of items if an individual line item is an array, it will create columns that are lined-up
line_formatter([“foo”, “barbaz”]) # => “%-6s” line_formatter([“foo”, “barbaz”], [“bar”, “qux”]) # => “%-3s %-6s”
363 364 365 366 367 368 369 370 371 372 373 374 375 |
# File 'lib/mortar/helpers.rb', line 363 def line_formatter(array) if array.any? {|item| item.is_a?(Array)} cols = [] array.each do |item| if item.is_a?(Array) item.each_with_index { |val,idx| cols[idx] = [cols[idx]||0, (val || '').length].max } end end cols.map { |col| "%-#{col}s" }.join(" ") else "%s" end end |
#longest(items) ⇒ Object
179 180 181 |
# File 'lib/mortar/helpers.rb', line 179 def longest(items) items.map { |i| i.to_s.length }.sort.last end |
#output_with_bang(message = "", new_line = true) ⇒ Object
269 270 271 272 |
# File 'lib/mortar/helpers.rb', line 269 def output_with_bang(="", new_line=true) return if .to_s.strip == "" display(format_with_bang(), new_line) end |
#pending_github_team_state_message(invite_url) ⇒ Object
533 534 535 536 537 538 539 540 541 |
# File 'lib/mortar/helpers.rb', line 533 def (invite_url) """ \nYour GitHub username has been set, but you must accept GitHub's invitation to join the Mortar GitHub organization. If you have not received an email you can visit #{invite_url} to view and accept your invitation. You will need to accept this invitation to start using Mortar projects. """ end |
#quantify(string, num) ⇒ Object
175 176 177 |
# File 'lib/mortar/helpers.rb', line 175 def quantify(string, num) "%d %s" % [ num, num.to_i == 1 ? string : "#{string}s" ] end |
#redisplay(line, line_break = false) ⇒ Object
105 106 107 |
# File 'lib/mortar/helpers.rb', line 105 def redisplay(line, line_break = false) display("\r\e[0K#{line}", line_break) end |
#retry_on_exception(*exceptions) ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/mortar/helpers.rb', line 131 def retry_on_exception(*exceptions) retry_count = 0 begin yield rescue *exceptions => ex raise ex if retry_count >= 3 sleep 3 retry_count += 1 retry end end |
#running_on_a_mac? ⇒ Boolean
37 38 39 |
# File 'lib/mortar/helpers.rb', line 37 def running_on_a_mac? RUBY_PLATFORM =~ /-darwin\d/ end |
#running_on_windows? ⇒ Boolean
33 34 35 |
# File 'lib/mortar/helpers.rb', line 33 def running_on_windows? RUBY_PLATFORM =~ /mswin32|mingw32/ end |
#set_buffer(enable) ⇒ Object
219 220 221 222 223 224 225 226 227 |
# File 'lib/mortar/helpers.rb', line 219 def set_buffer(enable) with_tty do if enable `stty icanon echo` else `stty -icanon -echo` end end end |
#shell(cmd) ⇒ Object
127 128 129 |
# File 'lib/mortar/helpers.rb', line 127 def shell(cmd) FileUtils.cd(Dir.pwd) {|d| return `#{cmd}`} end |
#spinner(ticks) ⇒ Object
352 353 354 |
# File 'lib/mortar/helpers.rb', line 352 def spinner(ticks) %w(/ - \\ |)[ticks % 4] end |
#status(message) ⇒ Object
260 261 262 |
# File 'lib/mortar/helpers.rb', line 260 def status() @status = end |
#string_distance(first, last) ⇒ Object
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 |
# File 'lib/mortar/helpers.rb', line 467 def string_distance(first, last) distances = [] # 0x0s 0.upto(first.length) do |index| distances << [index] + [0] * last.length end distances[0] = 0.upto(last.length).to_a 1.upto(last.length) do |last_index| 1.upto(first.length) do |first_index| first_char = first[first_index - 1, 1] last_char = last[last_index - 1, 1] if first_char == last_char distances[first_index][last_index] = distances[first_index - 1][last_index - 1] # noop else distances[first_index][last_index] = [ distances[first_index - 1][last_index], # deletion distances[first_index][last_index - 1], # insertion distances[first_index - 1][last_index - 1] # substitution ].min + 1 # cost if first_index > 1 && last_index > 1 first_previous_char = first[first_index - 2, 1] last_previous_char = last[last_index - 2, 1] if first_char == last_previous_char && first_previous_char == last_char distances[first_index][last_index] = [ distances[first_index][last_index], distances[first_index - 2][last_index - 2] + 1 # transposition ].min end end end end end distances[first.length][last.length] end |
#styled_array(array, options = {}) ⇒ Object
377 378 379 380 381 382 383 384 |
# File 'lib/mortar/helpers.rb', line 377 def styled_array(array, ={}) fmt = line_formatter(array) array = array.sort unless [:sort] == false array.each do |element| display((fmt % element).rstrip) end display end |
#styled_error(error, message = 'Mortar Development Framework internal error.') ⇒ Object
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 |
# File 'lib/mortar/helpers.rb', line 386 def styled_error(error, ='Mortar Development Framework internal error.') if Mortar::Helpers.error_with_failure display("failed") Mortar::Helpers.error_with_failure = false end $stderr.puts(" ! #{}.") $stderr.puts(" ! Search for help at: http://help.mortardata.com") $stderr.puts(" ! Or report a bug at: https://github.com/mortardata/mortar/issues/new") $stderr.puts $stderr.puts(" Error: #{error.} (#{error.class})") $stderr.puts(" Backtrace: #{error.backtrace.first}") error.backtrace[1..-1].each do |line| $stderr.puts(" #{line}") end if error.backtrace.length > 1 $stderr.puts end command = ARGV.map do |arg| if arg.include?(' ') arg = %{"#{arg}"} else arg end end.join(' ') $stderr.puts(" Command: mortar #{command}") unless Mortar::Auth.host == Mortar::Auth.default_host $stderr.puts(" Host: #{Mortar::Auth.host}") end if http_proxy = ENV['http_proxy'] || ENV['HTTP_PROXY'] $stderr.puts(" HTTP Proxy: #{http_proxy}") end if https_proxy = ENV['https_proxy'] || ENV['HTTPS_PROXY'] $stderr.puts(" HTTPS Proxy: #{https_proxy}") end $stderr.puts(" Version: #{Mortar::USER_AGENT}") $stderr.puts end |
#styled_hash(hash, keys = nil, indent = 0) ⇒ Object
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 |
# File 'lib/mortar/helpers.rb', line 428 def styled_hash(hash, keys=nil, indent=0) def display_with_indent(indent, msg="", new_line=true) display("#{"".ljust(indent)}#{msg.to_s}", new_line) end max_key_length = (hash.keys.map {|key| key.to_s.length}.max || 0) + 2 keys ||= hash.keys.sort {|x,y| x.to_s <=> y.to_s} keys.each do |key| case value = hash[key] when Hash display_with_indent(indent, "#{key}: ".ljust(max_key_length)) styled_hash(hash[key], nil, indent + 2) when Array if value.empty? next else elements = value.sort {|x,y| x.to_s <=> y.to_s} display_with_indent(indent, "#{key}: ".ljust(max_key_length)) elements[0..-1].each do |element| case element when Hash styled_hash(element, nil, indent + 2) else display_with_indent(indent, "#{' ' * max_key_length}#{element}") end end if elements.length > 1 display end end when nil next else display_with_indent(indent, "#{key}: ".ljust(max_key_length), false) display_with_indent(indent, value) end end end |
#styled_header(header) ⇒ Object
424 425 426 |
# File 'lib/mortar/helpers.rb', line 424 def styled_header(header) display("=== #{header}") end |
#suggestion(actual, possibilities) ⇒ Object
501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 |
# File 'lib/mortar/helpers.rb', line 501 def suggestion(actual, possibilities) distances = Hash.new {|hash,key| hash[key] = []} possibilities.each do |suggestion| distances[string_distance(actual, suggestion)] << suggestion end minimum_distance = distances.keys.min if minimum_distance < 4 suggestions = distances[minimum_distance].sort if suggestions.length == 1 "Perhaps you meant `#{suggestions.first}`." else "Perhaps you meant #{suggestions[0...-1].map {|suggestion| "`#{suggestion}`"}.join(', ')} or `#{suggestions.last}`." end else nil end end |
#test_name ⇒ Object
53 54 55 |
# File 'lib/mortar/helpers.rb', line 53 def test_name ENV['MORTAR_TEST_NAME'] end |
#ticking(sleep_time) ⇒ Object
343 344 345 346 347 348 349 350 |
# File 'lib/mortar/helpers.rb', line 343 def ticking(sleep_time) ticks = 0 loop do yield(ticks) ticks +=1 sleep sleep_time end end |
#time_ago(elapsed) ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/mortar/helpers.rb', line 143 def time_ago(elapsed) if elapsed <= 60 "#{elapsed.floor}s ago" elsif elapsed <= (60 * 60) "#{(elapsed / 60).floor}m ago" elsif elapsed <= (60 * 60 * 25) "#{(elapsed / 60 / 60).floor}h ago" else (Time.now - elapsed).strftime("%Y/%m/%d %H:%M:%S") end end |
#truncate(text, length) ⇒ Object
155 156 157 158 159 160 161 |
# File 'lib/mortar/helpers.rb', line 155 def truncate(text, length) if text.size > length text[0, length - 2] + '..' else text end end |
#warning(msg = "", new_line = true) ⇒ Object
95 96 97 98 99 100 101 102 103 |
# File 'lib/mortar/helpers.rb', line 95 def warning(msg="", new_line=true) = "WARNING: #{msg}" if new_line display() else print(msg) $stdout.flush end end |
#with_tty(&block) ⇒ Object
229 230 231 232 233 234 235 236 |
# File 'lib/mortar/helpers.rb', line 229 def with_tty(&block) return unless $stdin.isatty begin yield rescue # fails on windows end end |
#write_to_file(str_data, path, mkdir_p = true) ⇒ Object
61 62 63 64 65 66 |
# File 'lib/mortar/helpers.rb', line 61 def write_to_file(str_data, path, mkdir_p=true) if mkdir_p FileUtils.mkdir_p File.dirname(path) end File.open(path, "w"){|f| f.write(str_data)} end |