Module: Morpheus::Cli::PrintHelper
- Included in:
- Credentials, ErrorHandler
- Defined in:
- lib/morpheus/cli/mixins/print_helper.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#generate_usage_bar(used_value, max_value, opts = {}) ⇒ Object
shows cyan, yellow, red progress bar where 50% looks like [||||| ] todo: render units used / available here too maybe.
- #print_available_options(option_types) ⇒ Object
- #print_dry_run(opts) ⇒ Object
- #print_errors(response, options = {}) ⇒ Object
- #print_green_success(msg) ⇒ Object
- #print_red_alert(msg) ⇒ Object
- #print_rest_exception(e, options = {}) ⇒ Object
- #print_rest_exception_request_and_response(e) ⇒ Object
- #print_rest_request(req) ⇒ Object
- #print_rest_response(res) ⇒ Object
- #print_results_pagination(json_response) ⇒ Object
- #print_stats_usage(stats, opts = {}) ⇒ Object
- #print_yellow_warning(msg) ⇒ Object
- #required_blue_prompt ⇒ Object
Class Method Details
.included(klass) ⇒ Object
7 8 9 |
# File 'lib/morpheus/cli/mixins/print_helper.rb', line 7 def self.included(klass) klass.send :include, Term::ANSIColor end |
Instance Method Details
#generate_usage_bar(used_value, max_value, opts = {}) ⇒ Object
shows cyan, yellow, red progress bar where 50% looks like [||||| ] todo: render units used / available here too maybe
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 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/morpheus/cli/mixins/print_helper.rb', line 176 def (used_value, max_value, opts={}) rainbow = opts[:rainbow] != false = opts[:max_bars] || 50 out = "" = [] percent = 0 if max_value.to_i == 0 percent = 0 else percent = ((used_value.to_f / max_value.to_f) * 100) end percent_label = (used_value.nil? || max_value.to_f == 0.0) ? "n/a" : "#{percent.round(2)}%".rjust(6, ' ') = "" if percent > 100 .times { << "|" } # percent = 100 else = ((percent / 100.0) * ).ceil .times { << "|" } end if rainbow = "" cur_rainbow_color = white .each_with_index {|, i| reached_percent = (i / .to_f) * 100 = cur_rainbow_color if reached_percent > 80 = red elsif reached_percent > 50 = yellow elsif reached_percent > 10 = cyan end if cur_rainbow_color != cur_rainbow_color = << cur_rainbow_color end << } padding = - .size if padding > 0 padding.times { << " " } #rainbow_bar << " " * padding end << reset = white + "[" + + white + "]" + " #{cur_rainbow_color}#{percent_label}#{reset}" out << else = cyan if percent > 80 = red elsif percent > 50 = yellow end = white + "[" + + .join.ljust(, ' ') + white + "]" + " #{percent_label}" + reset out << end return out end |
#print_available_options(option_types) ⇒ Object
251 252 253 254 |
# File 'lib/morpheus/cli/mixins/print_helper.rb', line 251 def (option_types) option_lines = option_types.collect {|it| "\t-O #{it['fieldContext'] ? it['fieldContext'] + '.' : ''}#{it['fieldName']}=\"value\"" }.join("\n") puts "Available Options:\n#{option_lines}\n\n" end |
#print_dry_run(opts) ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/morpheus/cli/mixins/print_helper.rb', line 129 def print_dry_run(opts) http_method = opts[:method] url = opts[:url] params = opts[:params] params = opts[:headers][:params] if opts[:headers] && opts[:headers][:params] query_string = params.respond_to?(:map) ? URI.encode_www_form(params) : query_string if query_string && !query_string.empty? url = "#{url}?#{query_string}" end request_string = "#{http_method.to_s.upcase} #{url}".strip payload = opts[:payload] print "\n" ,cyan, bold, "DRY RUN\n","==================", "\n\n", reset print cyan print "Request: ", "\n" print reset print request_string, "\n" print cyan if payload if payload.is_a?(String) begin payload = JSON.parse(payload) rescue => e #payload = "(unparsable) #{payload}" end end print "\n" print "JSON: ", "\n" print reset print JSON.pretty_generate(payload) end print "\n" print reset end |
#print_errors(response, options = {}) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/morpheus/cli/mixins/print_helper.rb', line 23 def print_errors(response, ={}) begin if [:json] print red print JSON.pretty_generate(response) print reset, "\n" else if !response['success'] print red,bold if response['msg'] puts response['msg'] end if response['errors'] response['errors'].each do |key, value| print "* #{key}: #{value}\n" end end print reset else # this should not really happen print cyan,bold, "\nSuccess!" end end ensure print reset end end |
#print_green_success(msg) ⇒ Object
19 20 21 |
# File 'lib/morpheus/cli/mixins/print_helper.rb', line 19 def print_green_success(msg) print "#{green}#{msg}#{reset}\n" end |
#print_red_alert(msg) ⇒ Object
11 12 13 |
# File 'lib/morpheus/cli/mixins/print_helper.rb', line 11 def print_red_alert(msg) print "#{red}#{msg}#{reset}\n" end |
#print_rest_exception(e, options = {}) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/morpheus/cli/mixins/print_helper.rb', line 51 def print_rest_exception(e, ={}) if e.response if [:debug] begin print_rest_exception_request_and_response(e) ensure print reset end return end if e.response.code == 400 response = JSON.parse(e.response.to_s) print_errors(response, ) else print_red_alert "Error Communicating with the Appliance. #{e}" if [:json] || [:debug] begin response = JSON.parse(e.response.to_s) print red print JSON.pretty_generate(response) print reset, "\n" rescue TypeError, JSON::ParserError => ex print_red_alert "Failed to parse JSON response: #{ex}" print red print response.to_s print reset, "\n" ensure print reset end end end else print_red_alert "Error Communicating with the Appliance. #{e}" end end |
#print_rest_exception_request_and_response(e) ⇒ Object
118 119 120 121 122 123 124 125 126 127 |
# File 'lib/morpheus/cli/mixins/print_helper.rb', line 118 def print_rest_exception_request_and_response(e) print_red_alert "Error Communicating with the Appliance. (#{e.response.code}) #{e}" response = e.response request = response.instance_variable_get("@request") print red print_rest_request(request) print "\n" print_rest_response(response) print reset end |
#print_rest_request(req) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/morpheus/cli/mixins/print_helper.rb', line 87 def print_rest_request(req) # JD: IOError when accessing payload... we should probably just be printing at the time the request is made.. #out = [] #out << "#{req.method} #{req.url.inspect}" #out << req.payload.short_inspect if req.payload # payload = req.instance_variable_get("@payload") # out << payload if payload #out << req.processed_headers.to_a.sort.map { |(k, v)| [k.inspect, v.inspect].join("=>") }.join(", ") #print out.join(', ') + "\n" print "Request:" print "\n" print "#{req.method.to_s.upcase} #{req.url.inspect}" print "\n" end |
#print_rest_response(res) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/morpheus/cli/mixins/print_helper.rb', line 102 def print_rest_response(res) # size = @raw_response ? File.size(@tf.path) : (res.body.nil? ? 0 : res.body.size) size = (res.body.nil? ? 0 : res.body.size) print "Response:" print "\n" display_size = Filesize.from("#{size} B").pretty rescue size print "HTTP #{res.net_http_res.code} - #{res.net_http_res.} | #{(res['Content-type'] || '').gsub(/;.*$/, '')} #{display_size}" print "\n" begin print JSON.pretty_generate(JSON.parse(res.body)) rescue print res.body.to_s end print "\n" end |
#print_results_pagination(json_response) ⇒ Object
163 164 165 166 167 |
# File 'lib/morpheus/cli/mixins/print_helper.rb', line 163 def print_results_pagination(json_response) if json_response && json_response["meta"] print cyan,"\nViewing #{json_response['meta']['offset'].to_i + 1}-#{json_response['meta']['offset'].to_i + json_response['meta']['size'].to_i} of #{json_response['meta']['total']}\n" end end |
#print_stats_usage(stats, opts = {}) ⇒ Object
237 238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/morpheus/cli/mixins/print_helper.rb', line 237 def print_stats_usage(stats, opts={}) opts[:include] ||= [:memory, :storage, :cpu] if opts[:include].include?(:memory) print cyan, "Memory:".ljust(10, ' ') + (stats['usedMemory'], stats['maxMemory']) + cyan + Filesize.from("#{stats['usedMemory']} B").pretty.strip.rjust(15, ' ') + " / " + Filesize.from("#{stats['maxMemory']} B").pretty.strip.ljust(15, ' ') + "\n" end if opts[:include].include?(:storage) print cyan, "Storage:".ljust(10, ' ') + (stats['usedStorage'], stats['maxStorage']) + cyan + Filesize.from("#{stats['usedStorage']} B").pretty.strip.rjust(15, ' ') + " / " + Filesize.from("#{stats['maxStorage']} B").pretty.strip.ljust(15, ' ') + "\n" end if opts[:include].include?(:cpu) cpu_usage = (stats['usedCpu'] || stats['cpuUsage']) print cyan, "CPU:".ljust(10, ' ') + (cpu_usage.to_f, 100) + "\n" end end |
#print_yellow_warning(msg) ⇒ Object
15 16 17 |
# File 'lib/morpheus/cli/mixins/print_helper.rb', line 15 def print_yellow_warning(msg) print "#{yellow}#{msg}#{reset}\n" end |
#required_blue_prompt ⇒ Object
169 170 171 |
# File 'lib/morpheus/cli/mixins/print_helper.rb', line 169 def required_blue_prompt "#{cyan}|#{reset}" end |