Class: JDC::App::Base

Inherits:
CLI show all
Includes:
LoginRequirements
Defined in:
lib/jdc/cli/app/base.rb

Instance Method Summary collapse

Methods included from LoginRequirements

#precondition

Methods inherited from CLI

#add_exception_name_to_msg, #build_client, #check_key, #check_logged_in, #check_organization_and_space, #check_target, client, #client, client=, #client_target, #color_enabled?, #debug?, #default_action, #ensure_config_dir, #err, #execute, #fail, #fail_unknown, #force?, #formatted_exception_output, #help, #help_header, #invalidate_client, #log_error, #name_list, #normalize_targets_info, #one_of, #quiet?, #remove_target_info, #sane_target_url, #save_target_info, #save_targets, #save_token_if_it_changes, #set_target, #table, #target_info, #targets_info, #user_colors, #verbose?, #wrap_errors

Methods included from Spacing

#indented, #justify, #line, #lines, #quiet?, #spaced, #start_line, #tabular, #text_width, #trim_escapes

Methods included from Interactive

#ask, #handler, #input_state, #list_choices, #prompt, #show_default

Instance Method Details

#app_status(a) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/jdc/cli/app/base.rb', line 26

def app_status(a)
  health = a.health

  if a.debug_mode == "suspend" && health == "0%"
    c("suspended", :neutral)
  else
    c(health.downcase, state_color(health))
  end
end

#human_mb(num) ⇒ Object



40
41
42
# File 'lib/jdc/cli/app/base.rb', line 40

def human_mb(num)
  human_size(num * 1024 * 1024, 0)
end

#human_size(num, precision = 1) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/jdc/cli/app/base.rb', line 44

def human_size(num, precision = 1)
  sizes = %w(G M K)
  sizes.each.with_index do |suf, i|
    pow = sizes.size - i
    unit = 1024.0 ** pow
    if num >= unit
      return format("%.#{precision}f%s", num / unit, suf)
    end
  end

  format("%.#{precision}fB", num)
end

#megabytes(str) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/jdc/cli/app/base.rb', line 57

def megabytes(str)
  if str =~ /T$/i
    str.to_i * 1024 * 1024
  elsif str =~ /G$/i
    str.to_i * 1024
  elsif str =~ /M$/i
    str.to_i
  elsif str =~ /K$/i
    str.to_i / 1024
  else # assume megabytes
    str.to_i
  end
end

#memory_choicesObject



36
37
38
# File 'lib/jdc/cli/app/base.rb', line 36

def memory_choices
  [128, 256, 512, 1024].map{|n| human_mb(n)}
end

#state_color(s) ⇒ Object

choose the right color for app/instance state



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/jdc/cli/app/base.rb', line 9

def state_color(s)
  case s
  when "STARTING"
    :neutral
  when "STARTED", "RUNNING"
    :good
  when "DOWN"
    :bad
  when "FLAPPING"
    :error
  when "N/A"
    :unknown
  else
    :warning
  end
end