lib/destroy.rb in forj-1.0.7 vs lib/destroy.rb in forj-1.0.8

- old
+ new

@@ -34,64 +34,79 @@ o_cloud = Forj::CloudConnection.connect(account) o_forge = o_cloud.get(:forge, name) if o_forge[:servers].count > 0 - destroy_server(o_cloud, o_forge, options, account) + destroy_server(o_cloud, o_forge, options) else PrcLib.high_level_msg("No server(s) found on forge instance '%s'.\n", name) end end - def self.destroy_server(o_cloud, o_forge, options, account) + def self.destroy_server(o_cloud, o_forge, options) if options[:force] # Destroy all servers found o_cloud.delete(:forge) else - server_list = get_server_list(o_forge) + server_list, servers_id = get_server_list(o_forge) - o_server_number = get_server_index(server_list) + server_name = choose_server(server_list) - if o_server_number >= 0 && o_server_number < o_forge[:servers].count - # Destroy selected server - account.set(:forge_server, o_forge[:servers][o_server_number][:id]) + case server_name + when 'abort' + PrcLib.high_level_msg("No server destroyed on your demand.\n", name) + return + when 'all' + # Destroy all servers found o_cloud.delete(:forge) + return + else + # Destroy selected server + found = server_name.match(/ - (.*)$/) + if found + o_cloud.delete(:forge, :forge_server => found[1]) + else + o_cloud.delete(:forge, :forge_server => servers_id[server_name][0]) + end end - - # Destroy all servers found - o_cloud.delete(:forge) if o_server_number == server_list.index('all') - # esc - PrcLib.high_level_msg("No server destroyed on your demand.\n", - name - ) if o_server_number == server_list.index('esc') end end - def self.get_server_list(o_forge) + def self.get_server_list(forge) # Ask the user to get server(s) to destroy server_list = [] - index = 0 + servers_id = {} - o_forge[:servers].each do |server| - server_list[index] = server[:name] - index += 1 + forge[:servers].each do |_type, server| + server_name = server[:name] + if servers_id.key?(server_name) + servers_id[server_name] << server[:id] + else + servers_id[server_name] = [server[:id]] + end end + servers_id.each do |name, servers| + if servers.length > 1 + servers.each { |id| server_list << name + ' - ' + id } + else + server_list << name + end + end + server_list << 'all' - server_list << 'esc' + server_list << 'abort' - server_list + [server_list, servers_id] end - def self.get_server_index(server_list) - say('Select the index of the server you want to destroy') + def self.choose_server(server_list) + say('Please, choose what you want to destroy') value = choose do |q| q.choices(*server_list) end - o_server_number = server_list.index(value) - - o_server_number + value end end end