vmc-ng/lib/vmc/cli.rb in vmc-0.4.0.beta.20 vs vmc-ng/lib/vmc/cli.rb in vmc-0.4.0.beta.21
- old
+ new
@@ -1,6 +1,8 @@
require "yaml"
+require "socket"
+require "net/http"
require "mothership"
require "mothership/pretty"
require "mothership/progress"
@@ -60,14 +62,31 @@
else
super
end
end
+ def precondition
+ unless client.logged_in?
+ fail "Please log in with 'vmc login'."
+ end
+
+ return unless v2?
+
+ unless client.current_organization
+ fail "Please select an organization with 'vmc target -i'."
+ end
+
+ unless client.current_space
+ fail "Please select a space with 'vmc target -i'."
+ end
+ end
+
def execute(cmd, argv)
if option(:help)
invoke :help, :command => cmd.name.to_s
else
+ cmd.context.new.precondition if cmd.context <= CLI
super
end
rescue Interrupt
exit_status 130
rescue Mothership::Error
@@ -80,22 +99,31 @@
puts ""
puts c("Not authenticated! Try logging in:", :warning)
invoke :login
- @client = nil
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?
err msg
+ end
- ensure_config_dir
+ def log_error(e)
+ msg = e.class.name
+ msg << ": #{e}" unless e.to_s.empty?
File.open(File.expand_path(VMC::CRASH_FILE), "w") do |f|
f.puts "Time of crash:"
f.puts " #{Time.now}"
f.puts ""
@@ -123,27 +151,32 @@
def color_enabled?
option(:color)
end
- def err(msg, exit_status = 1)
+ def err(msg, status = 1)
if quiet?
$stderr.puts(msg)
else
puts c(msg, :error)
end
- exit_status 1
+ exit_status status
end
def fail(msg)
raise UserError, msg
end
def sane_target_url(url)
unless url =~ /^https?:\/\//
- url = "http://#{url}"
+ begin
+ TCPSocket.new(url, Net::HTTP.https_default_port)
+ url = "https://#{url}"
+ rescue SocketError, Timeout::Error
+ url = "http://#{url}"
+ end
end
url.gsub(/\/$/, "")
end
@@ -178,11 +211,11 @@
File.open(File.expand_path(VMC::TARGET_FILE), "w") do |f|
f.write(sane_target_url(url))
end
- @client = nil
+ invalidate_client
end
def targets_info
new_toks = File.expand_path(VMC::TOKENS_FILE)
old_toks = File.expand_path(VMC::OLD_TOKENS_FILE)
@@ -219,11 +252,11 @@
ts[client_target] = info
save_targets(ts)
end
def remove_target_info
- ts = target_info
+ ts = targets_info
ts.delete client_target
save_targets(ts)
end
def no_v2
@@ -232,45 +265,60 @@
def v2?
client.is_a?(CFoundry::V2::Client)
end
+ def invalidate_client
+ @@client = nil
+ client
+ end
+
def client
- return @client if @client
+ return @@client if defined?(@@client) && @@client
info = target_info
- @client =
+ @@client =
case info[:version]
when 2
CFoundry::V2::Client.new(client_target, info[:token])
when 1
CFoundry::V1::Client.new(client_target, info[:token])
else
CFoundry::Client.new(client_target, info[:token])
end
- @client.proxy = option(:proxy)
- @client.trace = option(:trace)
+ @@client.proxy = option(:proxy)
+ @@client.trace = option(:trace)
info[:version] ||=
- case @client
+ case @@client
when CFoundry::V2::Client
2
else
1
end
if org = info[:organization]
- @client.current_organization = @client.organization(org)
+ @@client.current_organization = @@client.organization(org)
end
if space = info[:space]
- @client.current_space = @client.space(space)
+ @@client.current_space = @@client.space(space)
end
save_target_info(info)
- @client
+ @@client
+ end
+
+ class << self
+ def client
+ @@client
+ end
+
+ def client=(c)
+ @@client = c
+ end
end
end
end