Module: Pebbles::Helpers
- Extended by:
- Helpers
- Defined in:
- lib/pebbles/helpers.rb
Constant Summary collapse
- @@kb =
1024
- @@mb =
1024 * @@kb
- @@gb =
1024 * @@mb
Class Method Summary collapse
Instance Method Summary collapse
- #action(message, options = {}) ⇒ Object
- #ask ⇒ Object
- #confirm_command(app_to_confirm = app, message = nil) ⇒ Object
- #create_git_remote(remote, url) ⇒ Object
- #debug(*args) ⇒ Object
- #debugging? ⇒ Boolean
- #display(msg = "", new_line = true) ⇒ Object
- #error(message, report = false) ⇒ Object
- #flatten_hash(hash, key) ⇒ Object
- #format_bytes(amount) ⇒ Object
- #format_error(error, message = 'Pebblescape client internal error.') ⇒ Object
- #format_with_bang(message) ⇒ Object
- #git(args) ⇒ Object
- #has_git? ⇒ Boolean
- #has_git_remote?(remote) ⇒ Boolean
- #has_http_git_entry_in_netrc ⇒ Object
- #home_directory ⇒ Object
- #hputs(string = '') ⇒ Object
- #json_decode(json) ⇒ Object
- #launchy(message, url) ⇒ 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
- #running_on_a_mac? ⇒ Boolean
- #running_on_windows? ⇒ Boolean
- #styled_array(array, options = {}) ⇒ Object
- #styled_error(error, message = 'Pebblescape client internal error.') ⇒ Object
- #styled_hash(hash, keys = nil) ⇒ Object
- #styled_header(header) ⇒ Object
- #update_git_remote(remote, url) ⇒ Object
- #with_tty(&block) ⇒ Object
Class Method Details
.error_with_failure ⇒ Object
68 69 70 |
# File 'lib/pebbles/helpers.rb', line 68 def self.error_with_failure @@error_with_failure ||= false end |
.error_with_failure=(new_error_with_failure) ⇒ Object
72 73 74 |
# File 'lib/pebbles/helpers.rb', line 72 def self.error_with_failure=(new_error_with_failure) @@error_with_failure = new_error_with_failure end |
Instance Method Details
#action(message, options = {}) ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/pebbles/helpers.rb', line 138 def action(, ={}) = "#{} in organization #{org}" if [:org] display("#{}... ", false) Pebbles::Helpers.error_with_failure = true ret = yield Pebbles::Helpers.error_with_failure = false display(([:success] || "done"), false) if @status display(", #{@status}", false) @status = nil end display ret end |
#ask ⇒ Object
55 56 57 |
# File 'lib/pebbles/helpers.rb', line 55 def ask $stdin.gets.to_s.strip end |
#confirm_command(app_to_confirm = app, message = nil) ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/pebbles/helpers.rb', line 153 def confirm_command(app_to_confirm = app, =nil) if confirmed_app = Pebbles::Command.[:confirm] unless confirmed_app == app_to_confirm raise(Pebbles::Command::CommandFailed, "Confirmed app #{confirmed_app} did not match the selected app #{app_to_confirm}.") end return true else display ||= "WARNING: Destructive Action\nThis command will affect the app: #{app_to_confirm}" << "\nTo proceed, type \"#{app_to_confirm}\" or re-run this command with --confirm #{app_to_confirm}" output_with_bang() display display "> ", false if ask.downcase != app_to_confirm error("Confirmation did not match #{app_to_confirm}. Aborted.") else true end end end |
#create_git_remote(remote, url) ⇒ Object
99 100 101 102 103 |
# File 'lib/pebbles/helpers.rb', line 99 def create_git_remote(remote, url) return if has_git_remote? remote git "remote add #{remote} #{url}" display "Git remote #{remote} added" if $?.success? end |
#debug(*args) ⇒ Object
47 48 49 |
# File 'lib/pebbles/helpers.rb', line 47 def debug(*args) $stderr.puts(*args) if debugging? end |
#debugging? ⇒ Boolean
51 52 53 |
# File 'lib/pebbles/helpers.rb', line 51 def debugging? ENV['PEBBLES_DEBUG'] end |
#display(msg = "", new_line = true) ⇒ Object
38 39 40 41 42 43 44 45 |
# File 'lib/pebbles/helpers.rb', line 38 def display(msg="", new_line=true) if new_line puts(msg) else print(msg) end $stdout.flush end |
#error(message, report = false) ⇒ Object
59 60 61 62 63 64 65 66 |
# File 'lib/pebbles/helpers.rb', line 59 def error(, report=false) if Pebbles::Helpers.error_with_failure display("failed") Pebbles::Helpers.error_with_failure = false end $stderr.puts(format_with_bang()) exit(1) end |
#flatten_hash(hash, key) ⇒ Object
264 265 266 267 268 269 270 |
# File 'lib/pebbles/helpers.rb', line 264 def flatten_hash(hash, key) hash[key].each do |k, v| hash["#{key}_#{k}"] = v end hash.delete(key) end |
#format_bytes(amount) ⇒ Object
114 115 116 117 118 119 120 121 |
# File 'lib/pebbles/helpers.rb', line 114 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_error(error, message = 'Pebblescape client internal error.') ⇒ Object
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/pebbles/helpers.rb', line 174 def format_error(error, ='Pebblescape client internal error.') formatted_error = [] formatted_error << " ! #{}" formatted_error << '' formatted_error << " Error: #{error.} (#{error.class})" command = ARGV.map do |arg| if arg.include?(' ') arg = %{"#{arg}"} else arg end end.join(' ') formatted_error << " Command: pebbles #{command}" require 'pebbles/auth' unless Pebbles::Auth.host == Pebbles::Auth.default_host formatted_error << " Host: #{Pebbles::Auth.host}" end if http_proxy = ENV['http_proxy'] || ENV['HTTP_PROXY'] formatted_error << " HTTP Proxy: #{http_proxy}" end if https_proxy = ENV['https_proxy'] || ENV['HTTPS_PROXY'] formatted_error << " HTTPS Proxy: #{https_proxy}" end formatted_error << " Version: #{Pebbles.user_agent}" formatted_error << "\n" formatted_error.join("\n") end |
#format_with_bang(message) ⇒ Object
33 34 35 36 |
# File 'lib/pebbles/helpers.rb', line 33 def format_with_bang() return '' if .to_s.strip == "" " ! " + .split("\n").join("\n ! ") end |
#git(args) ⇒ Object
89 90 91 92 93 |
# File 'lib/pebbles/helpers.rb', line 89 def git(args) return "" unless has_git? flattened_args = [args].flatten.compact.join(" ") %x{ git #{flattened_args} 2>&1 }.strip end |
#has_git? ⇒ Boolean
84 85 86 87 |
# File 'lib/pebbles/helpers.rb', line 84 def has_git? %x{ git --version } $?.success? end |
#has_git_remote?(remote) ⇒ Boolean
95 96 97 |
# File 'lib/pebbles/helpers.rb', line 95 def has_git_remote?(remote) git('remote').split("\n").include?(remote) && $?.success? end |
#has_http_git_entry_in_netrc ⇒ Object
280 281 282 |
# File 'lib/pebbles/helpers.rb', line 280 def has_http_git_entry_in_netrc Auth.netrc && Auth.netrc[Auth.http_git_host] end |
#home_directory ⇒ Object
5 6 7 8 |
# File 'lib/pebbles/helpers.rb', line 5 def home_directory return Dir.home if defined? Dir.home # Ruby 1.9+ running_on_windows? ? ENV['USERPROFILE'].gsub("\\","/") : ENV['HOME'] end |
#hputs(string = '') ⇒ Object
76 77 78 |
# File 'lib/pebbles/helpers.rb', line 76 def hputs(string='') Kernel.puts(string) end |
#json_decode(json) ⇒ Object
123 124 125 126 127 |
# File 'lib/pebbles/helpers.rb', line 123 def json_decode(json) MultiJson.load(json) rescue MultiJson::ParseError nil end |
#launchy(message, url) ⇒ Object
18 19 20 21 22 23 24 25 26 |
# File 'lib/pebbles/helpers.rb', line 18 def launchy(, url) action() do require("launchy") launchy = Launchy.open(url) if launchy.respond_to?(:join) launchy.join end end 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”
213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/pebbles/helpers.rb', line 213 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
80 81 82 |
# File 'lib/pebbles/helpers.rb', line 80 def longest(items) items.map { |i| i.to_s.length }.sort.last end |
#output_with_bang(message = "", new_line = true) ⇒ Object
28 29 30 31 |
# File 'lib/pebbles/helpers.rb', line 28 def output_with_bang(="", new_line=true) return if .to_s.strip == "" display(format_with_bang(), new_line) end |
#running_on_a_mac? ⇒ Boolean
14 15 16 |
# File 'lib/pebbles/helpers.rb', line 14 def running_on_a_mac? RUBY_PLATFORM =~ /-darwin\d/ end |
#running_on_windows? ⇒ Boolean
10 11 12 |
# File 'lib/pebbles/helpers.rb', line 10 def running_on_windows? RUBY_PLATFORM =~ /mswin32|mingw32/ end |
#styled_array(array, options = {}) ⇒ Object
227 228 229 230 231 232 233 234 |
# File 'lib/pebbles/helpers.rb', line 227 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 = 'Pebblescape client internal error.') ⇒ Object
272 273 274 275 276 277 278 |
# File 'lib/pebbles/helpers.rb', line 272 def styled_error(error, ='Pebblescape client internal error.') if Pebbles::Helpers.error_with_failure display("failed") Pebbles::Helpers.error_with_failure = false end $stderr.puts(format_error(error, )) end |
#styled_hash(hash, keys = nil) ⇒ Object
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
# File 'lib/pebbles/helpers.rb', line 236 def styled_hash(hash, keys=nil) max_key_length = hash.keys.map {|key| key.to_s.length}.max + 2 keys ||= hash.keys.sort {|x,y| x.to_s <=> y.to_s} keys.each do |key| case value = hash[key] when Array if value.empty? next else elements = value.sort {|x,y| x.to_s <=> y.to_s} display("#{key}: ".ljust(max_key_length), false) display(elements[0]) elements[1..-1].each do |element| display("#{' ' * max_key_length}#{element}") end if elements.length > 1 display end end when nil next else display("#{key}: ".ljust(max_key_length), false) display(value) end end end |
#styled_header(header) ⇒ Object
202 203 204 |
# File 'lib/pebbles/helpers.rb', line 202 def styled_header(header) display("=== #{header}") end |
#update_git_remote(remote, url) ⇒ Object
105 106 107 108 109 |
# File 'lib/pebbles/helpers.rb', line 105 def update_git_remote(remote, url) return unless has_git_remote? remote git "remote set-url #{remote} #{url}" display "Git remote #{remote} updated" if $?.success? end |
#with_tty(&block) ⇒ Object
129 130 131 132 133 134 135 136 |
# File 'lib/pebbles/helpers.rb', line 129 def with_tty(&block) return unless $stdin.isatty begin yield rescue # fails on windows end end |