lib/cloudstack-cli/commands/environment.rb in cloudstack-cli-0.7.4 vs lib/cloudstack-cli/commands/environment.rb in cloudstack-cli-0.8.0

- old
+ new

@@ -2,11 +2,11 @@ desc "list", "list cloudstack-cli environments" def list config = parse_config_file table = [%w(Name URL Default)] - table << ['-', config[:url], !config[:default]] + table << ['-', config[:url], !config[:default]] if config.key?(:url) config.each_key do |key| unless key.class == Symbol table << [key, config[key][:url], key == config[:default]] end end @@ -62,18 +62,30 @@ end desc "delete", "delete a Cloudstack connection" def delete(env) config = parse_config_file - if config.delete(env) - exit unless yes?("Do you really want to delete environment #{env}? [y/N]", :yellow) - config.delete :default if config[:default] == env - write_config_file(config) - say "OK.", :green + if env == '-' + config.delete(:url) + config.delete(:api_key) + config.delete(:secret_key) + # check if the config file is empty, delete it if true + if config.keys.select { |key| !key.is_a? Symbol}.size == 0 + exit unless yes?("Do you really want to delete environment #{env}? [y/N]", :yellow) + File.delete(options[:config_file]) + say "OK.", :green + exit + end + elsif config.delete(env) else say "Environment #{env} does not exist.", :red + exit 1 end + exit unless yes?("Do you really want to delete environment #{env}? [y/N]", :yellow) + config.delete :default if config[:default] == env + write_config_file(config) + say "OK.", :green end desc "default [ENV]", "show or set the default environment" def default(env = nil) config = parse_config_file @@ -82,10 +94,10 @@ default_env = config[:default] || '-' say "The current default environment is \"#{default_env}\"" exit 0 end - if env == '-' + if env == '-' && config.key?(:url) config.delete :default else unless config.has_key?(env) say "Environment #{env} does not exist.", :red exit 1 \ No newline at end of file