Class: VMC::Start

Inherits:
CLI
  • Object
show all
Defined in:
lib/vmc/cli/start.rb

Constant Summary collapse

@@displayed_target =

Make sure we only show the target once

false

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from CLI

#check_logged_in, #check_target, #client, client, client=, #client_target, #color_enabled?, #default_action, #ensure_config_dir, #err, #execute, #fail, #force?, #invalidate_client, #log_error, #name_list, #no_v2, #one_of, #quiet?, #remove_target_info, #sane_target_url, #save_target_info, #save_targets, #set_target, #table, #target_file, #target_info, #targets_info, #tokens_file, #user_colors, #v2?, #verbose?

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

Class Method Details

.find_by_name(what) ⇒ Object



17
18
19
20
21
22
# File 'lib/vmc/cli/start.rb', line 17

def self.find_by_name(what)
  proc { |name, choices|
    choices.find { |c| c.name == name } ||
      fail("Unknown #{what} '#{name}'")
  }
end

Instance Method Details

#colorsObject



314
315
316
317
318
# File 'lib/vmc/cli/start.rb', line 314

def colors
  user_colors.each do |n, c|
    line "#{n}: #{c(c.to_s, n)}"
  end
end

#displayed_target?Boolean

Returns:

  • (Boolean)


8
9
10
# File 'lib/vmc/cli/start.rb', line 8

def displayed_target?
  @@displayed_target
end

#infoObject



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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/vmc/cli/start.rb', line 35

def info
  all = input[:all]

  if all || input[:runtimes]
    runtimes =
      with_progress("Getting runtimes") do
        client.runtimes
      end
  end

  if all || input[:frameworks]
    frameworks =
      with_progress("Getting frameworks") do
        client.frameworks
      end
  end

  if all || input[:services]
    services =
      with_progress("Getting services") do
        client.services
      end
  end

  info = client.info

  showing_any = runtimes || services || frameworks

  unless !all && showing_any
    line if showing_any
    line info[:description]
    line
    line "target: #{b(client.target)}"

    indented do
      line "version: #{info[:version]}"
      line "support: #{info[:support]}"
    end

    if user = client.current_user
      line
      line "user: #{b(user.email || user.guid)}"
    end
  end

  if runtimes
    line unless quiet?

    if runtimes.empty? && !quiet?
      line "#{d("none")}"
    elsif input[:quiet]
      runtimes.each do |r|
        line r.name
      end
    else
      table(
        %w{runtime description},
        runtimes.sort_by(&:name).collect { |r|
          [c(r.name, :name), r.description]
        })
    end
  end

  if frameworks
    line unless quiet?

    if frameworks.empty? && !quiet?
      line "#{d("none")}"
    elsif input[:quiet]
      frameworks.each do |f|
        line f.name
      end
    else
      table(
        %w{framework description},
        frameworks.sort_by(&:name).collect { |f|
          [c(f.name, :name), f.description]
        })
    end
  end

  if services
    line unless quiet?

    if services.empty? && !quiet?
      line "#{d("none")}"
    elsif input[:quiet]
      services.each do |s|
        line s.label
      end
    else
      table(
        ["service", "version", "provider", v2? && "plans", "description"],
        services.sort_by(&:label).collect { |s|
          next if !v2? && s.deprecated?

          [ c(s.label, :name),
            s.version,
            s.provider,
            v2? && s.service_plans.collect(&:name).join(", "),
            s.description
          ]
        })
    end
  end
end

#loginObject



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
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
263
264
265
266
267
# File 'lib/vmc/cli/start.rb', line 215

def 
  show_context

  credentials =
    { :username => input[:username],
      :password => input[:password]
    }

  prompts = client.

  # ask username first
  if prompts.key? :username
    type, label = prompts.delete :username
    credentials[:username] ||= ask_prompt(type, label)
  end

  info = target_info

  authenticated = false
  failed = false
  remaining_attempts = 3
  until authenticated || remaining_attempts <= 0
    remaining_attempts -= 1
    unless force?
      ask_prompts(credentials, prompts)
    end

    with_progress("Authenticating") do |s|
      begin
        info[:token] = client.(credentials)
        authenticated = true
      rescue CFoundry::Denied
        return if force?

        s.fail do
          failed = true
          credentials.delete(:password)
        end
      end
    end
  end

  save_target_info(info)
  invalidate_client

  if v2?
    line if input.interactive?(:organization) || input.interactive?(:space)
    select_org_and_space(input, info)
    save_target_info(info)
  end
ensure
  exit_status 1 if not authenticated
end

#logoutObject



272
273
274
275
276
# File 'lib/vmc/cli/start.rb', line 272

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

#preconditionObject

These commands don’t require authentication.



14
# File 'lib/vmc/cli/start.rb', line 14

def precondition; end

#registerObject



292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/vmc/cli/start.rb', line 292

def register
  show_context

  email = input[:email]
  password = input[:password]

  if !force? && password != input[:verify]
    fail "Passwords do not match."
  end

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

  if input[:login]
    invoke :login, :username => email, :password => password
  end
end

#targetObject



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/vmc/cli/start.rb', line 155

def target
  if !input.given?(:url) && !input.given?(:organization) && !input.given?(:space)
    display_target
    display_org_and_space unless quiet?
    return
  end

  if input.given?(:url)
    target = sane_target_url(input[:url])
    with_progress("Setting target to #{c(target, :name)}") do
      client(target).info # check that it's valid before setting
      set_target(target)
    end
  end

  return unless v2? && client.logged_in?

  if input.given?(:organization) || input.given?(:space)
    info = target_info

    select_org_and_space(input, info)

    save_target_info(info)
  end

  return if quiet?

  invalidate_client

  line
  display_target
  display_org_and_space
end

#targetsObject



192
193
194
195
196
197
# File 'lib/vmc/cli/start.rb', line 192

def targets
  targets_info.each do |target, _|
    line target
    # TODO: print org/space
  end
end