<commit_before>require 'gli'
require "opsicle/user_profile"
require "opsicle/opsworks_adapter"
require "opsicle/ec2_adapter"
require "opsicle/manageable_layer"
require "opsicle/manageable_instance"
require "opsicle/manageable_stack"

require "pry"

module Opsicle
  class DeleteInstance

    def initialize(environment)
      @client = Client.new(environment)
      @ec2_adapter = Ec2Adapter.new(@client)
      @opsworks_adapter = OpsworksAdapter.new(@client)
      stack_id = @client.config.opsworks_config[:stack_id]
      @stack = ManageableStack.new(stack_id, @opsworks_adapter)
      @cli = HighLine.new
    end

    def execute(options={})
      puts "Stack ID = #{@stack.id}"
      layer = select_layer
      instances_to_delete = select_instances(layer)
      instances_to_delete.each do |instance|
        begin
          @opsworks_adapter.delete_instance(instance.instance_id)
          puts "Successfully deleted #{instance.hostname}"
        rescue
          puts "Failed to delete #{instance.hostname}"
        end
      end
    end

    def deleteable_instances(layer)
      @stack.deleteable_instances(layer)
    end

    def select_layer
      puts "\nLayers:\n"
      ops_layers = @opsworks_adapter.layers(@stack.id)

      layers = []
      ops_layers.each do |layer|
        layers << ManageableLayer.new(layer.name, layer.layer_id, @stack, @opsworks_adapter.client, @ec2_adapter.client, @cli)
      end

      layers.each_with_index { |layer, index| puts "#{index.to_i + 1}) #{layer.name}" }
      layer_index = @cli.ask("Layer?\n", Integer) { |q| q.in = 1..layers.length.to_i } - 1
      layers[layer_index]
    end

    def select_instances(layer)
      instances = deleteable_instances(layer)
      return_array = []
      if instances.empty?
        puts "There are no deletable instances."
      else
        puts "\nDeleteable Instances:\n"
        instances.each_with_index { |instance, index| puts "#{index.to_i + 1}) #{instance.status} - #{instance.hostname}" }
        instance_indices_string = @cli.ask("Which instances would you like to delete? (enter as a comma separated list)\n", String)
        instance_indices_list = instance_indices_string.split(/,\s*/)

        if instance_indices_list.include?("0")
          raise StandardError, "Any instances to delete must be indicated with numbers > 0."
        end

        instance_indices_list.map! { |instance_index| instance_index.to_i - 1 }
        instance_indices_list.each do |index|
          return_array << instances[index]
        end
      end
      return_array
    end
  end
end
<commit_msg>Get rid of pry<commit_after>require 'gli'
require "opsicle/user_profile"
require "opsicle/opsworks_adapter"
require "opsicle/ec2_adapter"
require "opsicle/manageable_layer"
require "opsicle/manageable_instance"
require "opsicle/manageable_stack"

module Opsicle
  class DeleteInstance

    def initialize(environment)
      @client = Client.new(environment)
      @ec2_adapter = Ec2Adapter.new(@client)
      @opsworks_adapter = OpsworksAdapter.new(@client)
      stack_id = @client.config.opsworks_config[:stack_id]
      @stack = ManageableStack.new(stack_id, @opsworks_adapter)
      @cli = HighLine.new
    end

    def execute(options={})
      puts "Stack ID = #{@stack.id}"
      layer = select_layer
      instances_to_delete = select_instances(layer)
      instances_to_delete.each do |instance|
        begin
          @opsworks_adapter.delete_instance(instance.instance_id)
          puts "Successfully deleted #{instance.hostname}"
        rescue
          puts "Failed to delete #{instance.hostname}"
        end
      end
    end

    def deleteable_instances(layer)
      @stack.deleteable_instances(layer)
    end

    def select_layer
      puts "\nLayers:\n"
      ops_layers = @opsworks_adapter.layers(@stack.id)

      layers = []
      ops_layers.each do |layer|
        layers << ManageableLayer.new(layer.name, layer.layer_id, @stack, @opsworks_adapter.client, @ec2_adapter.client, @cli)
      end

      layers.each_with_index { |layer, index| puts "#{index.to_i + 1}) #{layer.name}" }
      layer_index = @cli.ask("Layer?\n", Integer) { |q| q.in = 1..layers.length.to_i } - 1
      layers[layer_index]
    end

    def select_instances(layer)
      instances = deleteable_instances(layer)
      return_array = []
      if instances.empty?
        puts "There are no deletable instances."
      else
        puts "\nDeleteable Instances:\n"
        instances.each_with_index { |instance, index| puts "#{index.to_i + 1}) #{instance.status} - #{instance.hostname}" }
        instance_indices_string = @cli.ask("Which instances would you like to delete? (enter as a comma separated list)\n", String)
        instance_indices_list = instance_indices_string.split(/,\s*/)

        if instance_indices_list.include?("0")
          raise StandardError, "Any instances to delete must be indicated with numbers > 0."
        end

        instance_indices_list.map! { |instance_index| instance_index.to_i - 1 }
        instance_indices_list.each do |index|
          return_array << instances[index]
        end
      end
      return_array
    end
  end
end

{'fraction_non_alphanumeric': 0.10387596899224806, 'fraction_numerical': 0.004263565891472868, 'mean_word_length': 3.0038789759503493, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2478912', 'n_tokens_mistral': 1590, 'n_tokens_neox': 1591, 'n_words': 349}