Class: CF::CLI

Inherits:
App show all
Defined in:
lib/cf/cli.rb

Overview

subclass App since we operate on Apps by default

Constant Summary

Constants inherited from App

App::MEM_CHOICES

Constants included from Dots

Dots::COLOR_CODES, Dots::DOT_COUNT, Dots::DOT_TICK

Instance Method Summary collapse

Methods inherited from App

#apps, #delete, #file, #files, #health, #instances, #logs, #map, #push, #restart, #scale, #start, #stats, #stop, #unmap, #update

Methods inherited from Command

add_callback, after, around, before, callbacks, callbacks_for, ensuring, flag, #invoke_task

Methods included from Interactive

#ask, #force?, #handler, #input_state, #list_choices, #prompt

Methods included from Dots

b, c, #color?, #dots!, #stop_dots!, #with_progress

Instance Method Details

#infoObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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
86
87
88
89
90
91
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
# File 'lib/cf/cli.rb', line 33

def info
  info =
    with_progress("Getting target information") do
      client.info
    end

  if input(:runtimes)
    runtimes = {}
    info["frameworks"].each do |_, f|
      f["runtimes"].each do |r|
        runtimes[r["name"]] = r
      end
    end

    runtimes = runtimes.values.sort_by { |x| x["name"] }

    if simple_output?
      runtimes.each do |r|
        puts r["name"]
      end
      return
    end

    runtimes.each do |r|
      puts ""
      puts "#{c(r["name"], :blue)}:"
      puts "  version: #{b(r["version"])}"
      puts "  description: #{b(r["description"])}"
    end

    return
  end

  if input(:services)
    services = {}
    client.system_services.each do |_, svcs|
      svcs.each do |name, versions|
        services[name] = versions.values
      end
    end

    if simple_output?
      services.each do |name, _|
        puts name
      end

      return
    end

    services.each do |name, versions|
      puts ""
      puts "#{c(name, :blue)}:"
      puts "  versions: #{versions.collect { |v| v["version"] }.join ", "}"
      puts "  description: #{versions[0]["description"]}"
      puts "  type: #{versions[0]["type"]}"
    end

    return
  end

  puts ""

  puts info["description"]
  puts ""
  puts "target: #{b(client.target)}"
  puts "  version: #{info["version"]}"
  puts "  support: #{info["support"]}"

  if info["user"]
    puts ""
    puts "user: #{b(info["user"])}"
    puts "  usage:"

    limits = info["limits"]
    info["usage"].each do |k, v|
      m = limits[k]
      if k == "memory"
        puts "    #{k}: #{usage(v * 1024 * 1024, m * 1024 * 1024)}"
      else
        puts "    #{k}: #{b(v)} of #{b(m)} limit"
      end
    end
  end
end

#login(email = nil) ⇒ Object

TODO: implement new authentication scheme



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/cf/cli.rb', line 143

def (email = nil)
  unless simple_output?
    display_target
    puts ""
  end

  email ||= input(:email)
  password = input(:password)

  authenticated = false
  failed = false
  until authenticated
    unless force?
      if failed || !password
        password = ask("Password", :echo => "*", :forget => true)
      end
    end

    with_progress("Authenticating") do |s|
      begin
        save_token(client.(email, password))
        authenticated = true
      rescue CFoundry::Denied
        return if force?

        s.fail do
          failed = true
        end
      end
    end
  end
ensure
  $exit_status = 1 if not authenticated
end

#logoutObject



179
180
181
182
183
# File 'lib/cf/cli.rb', line 179

def logout
  with_progress("Logging out") do
    remove_token
  end
end

#register(email = nil) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/cf/cli.rb', line 193

def register(email = nil)
  unless simple_output?
    puts "Target: #{c(client_target, :blue)}"
    puts ""
  end

  email ||= input(:email)
  password = input(:password)

  with_progress("Creating user") do
    client.register(email, password)
  end

  unless input(:skip_login)
    with_progress("Logging in") do
      save_token(client.(email, password))
    end
  end
end

#servicesObject



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/cf/cli.rb', line 214

def services
  services =
    with_progress("Getting services") do
      client.services
    end

  puts "" unless simple_output?

  if services.empty? and !simple_output?
    puts "No services."
  end

  services.each do |s|
    display_service(s)
  end
end

#target(url = nil) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/cf/cli.rb', line 119

def target(url = nil)
  if url.nil?
    display_target
    return
  end

  target = sane_target_url(url)
  display = c(target.sub(/https?:\/\//, ""), :blue)
  with_progress("Setting target to #{display}") do
    unless force?
      # check that the target is valid
      CFoundry::Client.new(target).info
    end

    set_target(target)
  end
end

#usersObject



232
233
234
235
236
237
238
239
240
241
# File 'lib/cf/cli.rb', line 232

def users
  users =
    with_progress("Getting users") do
      client.users
    end

  users.each do |u|
    display_user(u)
  end
end