Class: VMC::CLI

Inherits:
Mothership
  • Object
show all
Includes:
Interactive, Spacing
Defined in:
lib/vmc/cli.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Spacing

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

Methods included from Interactive

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

Class Method Details

.clientObject



387
388
389
# File 'lib/vmc/cli.rb', line 387

def client
  @@client
end

.client=(c) ⇒ Object



391
392
393
# File 'lib/vmc/cli.rb', line 391

def client=(c)
  @@client = c
end

Instance Method Details

#check_logged_inObject



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/vmc/cli.rb', line 72

def check_logged_in
  unless client.logged_in?
    if force?
      fail "Please log in with 'vmc login'."
    else
      line c("Please log in with 'vmc login'.", :warning)
      line
      invoke :login
      invalidate_client
    end
  end
end

#check_targetObject



66
67
68
69
70
# File 'lib/vmc/cli.rb', line 66

def check_target
  unless File.exists? target_file
    fail "Please select a target with 'vmc target'."
  end
end

#client(target = client_target) ⇒ Object



342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
# File 'lib/vmc/cli.rb', line 342

def client(target = client_target)
  return @@client if defined?(@@client) && @@client

  info = target_info(target)

  @@client =
    case info[:version]
    when 2
      CFoundry::V2::Client.new(target, info[:token])
    when 1
      CFoundry::V1::Client.new(target, info[:token])
    else
      CFoundry::Client.new(target, info[:token])
    end

  @@client.proxy = input[:proxy]
  @@client.trace = input[:trace]

  uri = URI.parse(target)
  @@client.log = File.expand_path("#{LOGS_DIR}/#{uri.host}.log")

  unless info.key? :version
    info[:version] =
      case @@client
      when CFoundry::V2::Client
        2
      else
        1
      end

    save_target_info(info, target)
  end

  if org = info[:organization]
    @@client.current_organization = @@client.organization(org)
  end

  if space = info[:space]
    @@client.current_space = @@client.space(space)
  end

  @@client
end

#client_targetObject



267
268
269
# File 'lib/vmc/cli.rb', line 267

def client_target
  File.read(target_file).chomp
end

#color_enabled?Boolean

Returns:

  • (Boolean)


178
179
180
# File 'lib/vmc/cli.rb', line 178

def color_enabled?
  input[:color]
end

#default_actionObject



58
59
60
61
62
63
64
# File 'lib/vmc/cli.rb', line 58

def default_action
  if input[:version]
    line "vmc #{VERSION}"
  else
    super
  end
end

#ensure_config_dirObject



271
272
273
274
# File 'lib/vmc/cli.rb', line 271

def ensure_config_dir
  config = File.expand_path(VMC::CONFIG_DIR)
  Dir.mkdir(config) unless File.exist? config
end

#err(msg, status = 1) ⇒ Object



209
210
211
212
213
214
215
216
217
# File 'lib/vmc/cli.rb', line 209

def err(msg, status = 1)
  if quiet?
    $stderr.puts(msg)
  else
    puts c(msg, :error)
  end

  exit_status status
end

#execute(cmd, argv, global = {}) ⇒ Object



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
141
142
# File 'lib/vmc/cli.rb', line 100

def execute(cmd, argv, global = {})
  if input[:help]
    invoke :help, :command => cmd.name.to_s
  else
    @command = cmd
    precondition
    super
  end
rescue Interrupt
  exit_status 130
rescue Mothership::Error
  raise
rescue UserError => e
  log_error(e)
  err e.message
rescue SystemExit
  raise
rescue CFoundry::Forbidden, CFoundry::InvalidAuthToken => e
  if !$vmc_asked_auth
    $vmc_asked_auth = true

    line
    line c("Not authenticated! Try logging in:", :warning)

    invoke :login

    retry
  end

  log_error(e)

  err "Denied: #{e.description}"

rescue Exception => e
  ensure_config_dir

  log_error(e)

  msg = e.class.name
  msg << ": #{e}" unless e.to_s.empty?
  msg << "\nFor more information, see #{VMC::CRASH_FILE}"
  err msg
end

#fail(msg) ⇒ Object

Raises:



219
220
221
# File 'lib/vmc/cli.rb', line 219

def fail(msg)
  raise UserError, msg
end

#force?Boolean

Returns:

  • (Boolean)


174
175
176
# File 'lib/vmc/cli.rb', line 174

def force?
  input[:force]
end

#invalidate_clientObject



337
338
339
340
# File 'lib/vmc/cli.rb', line 337

def invalidate_client
  @@client = nil
  client
end

#log_error(e) ⇒ Object



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
# File 'lib/vmc/cli.rb', line 144

def log_error(e)
  msg = e.class.name
  msg << ": #{e}" unless e.to_s.empty?

  crash_file = File.expand_path(VMC::CRASH_FILE)

  FileUtils.mkdir_p(File.dirname(crash_file))

  File.open(crash_file, "w") do |f|
    f.puts "Time of crash:"
    f.puts "  #{Time.now}"
    f.puts ""
    f.puts msg
    f.puts ""

    vmc_dir = File.expand_path("../../../..", __FILE__) + "/"
    e.backtrace.each do |loc|
      if loc =~ /\/gems\//
        f.puts loc.sub(/.*\/gems\//, "")
      else
        f.puts loc.sub(vmc_dir, "")
      end
    end
  end
end

#name_list(xs) ⇒ Object



229
230
231
232
233
234
235
# File 'lib/vmc/cli.rb', line 229

def name_list(xs)
  if xs.empty?
    d("none")
  else
    xs.collect { |x| c(x.name, :name) }.join(", ")
  end
end

#no_v2Object



329
330
331
# File 'lib/vmc/cli.rb', line 329

def no_v2
  fail "Not implemented for v2." if v2?
end

#one_of(*paths) ⇒ Object



258
259
260
261
262
263
264
265
# File 'lib/vmc/cli.rb', line 258

def one_of(*paths)
  paths.each do |p|
    exp = File.expand_path(p)
    return exp if File.exist? exp
  end

  File.expand_path(paths.first)
end

#preconditionObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/vmc/cli.rb', line 85

def precondition
  check_target
  check_logged_in

  return unless v2?

  unless client.current_organization
    fail "Please select an organization with 'vmc target --ask-org'."
  end

  unless client.current_space
    fail "Please select a space with 'vmc target --ask-space'."
  end
end

#quiet?Boolean

Returns:

  • (Boolean)


170
171
172
# File 'lib/vmc/cli.rb', line 170

def quiet?
  input[:quiet]
end

#remove_target_info(target = client_target) ⇒ Object



323
324
325
326
327
# File 'lib/vmc/cli.rb', line 323

def remove_target_info(target = client_target)
  ts = targets_info
  ts.delete target
  save_targets(ts)
end

#sane_target_url(url) ⇒ Object



237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/vmc/cli.rb', line 237

def sane_target_url(url)
  unless url =~ /^https?:\/\//
    begin
      TCPSocket.new(url, Net::HTTP.https_default_port)
      url = "https://#{url}"
    rescue Errno::ECONNREFUSED, SocketError, Timeout::Error
      url = "http://#{url}"
    end
  end

  url.gsub(/\/$/, "")
end

#save_target_info(info, target = client_target) ⇒ Object



317
318
319
320
321
# File 'lib/vmc/cli.rb', line 317

def save_target_info(info, target = client_target)
  ts = targets_info
  ts[target] = info
  save_targets(ts)
end

#save_targets(ts) ⇒ Object



309
310
311
312
313
314
315
# File 'lib/vmc/cli.rb', line 309

def save_targets(ts)
  ensure_config_dir

  File.open(File.expand_path(VMC::TOKENS_FILE), "w") do |io|
    YAML.dump(ts, io)
  end
end

#set_target(url) ⇒ Object



276
277
278
279
280
281
282
283
284
# File 'lib/vmc/cli.rb', line 276

def set_target(url)
  ensure_config_dir

  File.open(File.expand_path(VMC::TARGET_FILE), "w") do |f|
    f.write(sane_target_url(url))
  end

  invalidate_client
end

#table(headers, rows) ⇒ Object



223
224
225
226
227
# File 'lib/vmc/cli.rb', line 223

def table(headers, rows)
  tabular(
    !quiet? && headers.collect { |h| h && b(h) },
    *rows)
end

#target_fileObject



250
251
252
# File 'lib/vmc/cli.rb', line 250

def target_file
  one_of(VMC::TARGET_FILE, VMC::OLD_TARGET_FILE)
end

#target_info(target = client_target) ⇒ Object



299
300
301
302
303
304
305
306
307
# File 'lib/vmc/cli.rb', line 299

def target_info(target = client_target)
  info = targets_info[target]

  if info.is_a? String
    { :token => info }
  else
    info || {}
  end
end

#targets_infoObject



286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/vmc/cli.rb', line 286

def targets_info
  new_toks = File.expand_path(VMC::TOKENS_FILE)
  old_toks = File.expand_path(VMC::OLD_TOKENS_FILE)

  if File.exist? new_toks
    YAML.load_file(new_toks)
  elsif File.exist? old_toks
    MultiJson.load(File.read(old_toks))
  else
    {}
  end
end

#tokens_fileObject



254
255
256
# File 'lib/vmc/cli.rb', line 254

def tokens_file
  one_of(VMC::TOKENS_FILE, VMC::OLD_TOKENS_FILE)
end

#user_colorsObject



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/vmc/cli.rb', line 186

def user_colors
  return @user_colors if @user_colors

  colors = File.expand_path(COLORS_FILE)

  @user_colors = super.dup

  # most terminal schemes are stupid, so use cyan instead
  @user_colors.each do |k, v|
    if v == :blue
      @user_colors[k] = :cyan
    end
  end

  if File.exists?(colors)
    YAML.load_file(colors).each do |k, v|
      @user_colors[k.to_sym] = v.to_sym
    end
  end

  @user_colors
end

#v2?Boolean

Returns:

  • (Boolean)


333
334
335
# File 'lib/vmc/cli.rb', line 333

def v2?
  client.is_a?(CFoundry::V2::Client)
end

#verbose?Boolean

Returns:

  • (Boolean)


182
183
184
# File 'lib/vmc/cli.rb', line 182

def verbose?
  input[:verbose]
end