# class Chef::Knife::CookbookDelete

### Attributes

cookbook_name[RW]
version[RW]

### Public Instance Methods

ask_which_versions_to_delete() click to toggle source

    # File lib/chef/knife/cookbook_delete.rb, line 100
    def ask_which_versions_to_delete
      question = "Which version(s) do you want to delete?\n"
      valid_responses = {}
      available_versions.each_with_index do |version, index|
        valid_responses[(index + 1).to_s] = version
        question << "#{index + 1}. #{@cookbook_name} #{version}\n"
      end
      valid_responses[(available_versions.size + 1).to_s] = :all
      question << "#{available_versions.size + 1}. All versions\n\n"
      responses = ask_question(question).split(',').map { |response| response.strip }

      if responses.empty?
        ui.error("No versions specified, exiting")
        exit(1)
      end
      versions = responses.map do |response|
        if version = valid_responses[response]
          version
        else
          ui.error("#{response} is not a valid choice, skipping it")
        end
      end
      versions.compact
    end

available_versions() click to toggle source

    # File lib/chef/knife/cookbook_delete.rb, line 87
    def available_versions
      @available_versions ||= rest.get_rest("cookbooks/#{@cookbook_name}").map do |name, url_and_version|
        url_and_version["versions"].map {|url_by_version| url_by_version["version"]}
      end.flatten
    rescue Net::HTTPServerException => e
      if e.to_s =~ /^404/
        ui.error("Cannot find a cookbook named #{@cookbook_name} to delete")
        nil
      else
        raise
      end
    end

delete_all_versions() click to toggle source

    # File lib/chef/knife/cookbook_delete.rb, line 59
    def delete_all_versions
      confirm("Do you really want to delete all versions of #{@cookbook_name}")
      delete_all_without_confirmation
    end

delete_all_without_confirmation() click to toggle source

    # File lib/chef/knife/cookbook_delete.rb, line 64
    def delete_all_without_confirmation
      # look up the available versions again just in case the user
      # got to the list of versions to delete and selected 'all'
      # and also a specific version
      @available_versions = nil
      Array(available_versions).each do |version|
        delete_version_without_confirmation(version)
      end
    end

delete_explicit_version() click to toggle source

    # File lib/chef/knife/cookbook_delete.rb, line 53
    def delete_explicit_version
      delete_object(Chef::CookbookVersion, "#{@cookbook_name} version #{@version}", "cookbook") do
        delete_request("cookbooks/#{@cookbook_name}/#{@version}")
      end
    end

delete_version_without_confirmation(version) click to toggle source

    # File lib/chef/knife/cookbook_delete.rb, line 125
    def delete_version_without_confirmation(version)
      object = delete_request("cookbooks/#{@cookbook_name}/#{version}")
      output(format_for_display(object)) if config[:print_after]
      ui.info("Deleted cookbook[#{@cookbook_name}][#{version}]")
    end

delete_versions_without_confirmation(versions) click to toggle source

    # File lib/chef/knife/cookbook_delete.rb, line 131
    def delete_versions_without_confirmation(versions)
      versions.each do |version|
        if version == :all
          delete_all_without_confirmation
          break
        else
          delete_version_without_confirmation(version)
        end
      end
    end

delete_without_explicit_version() click to toggle source

    # File lib/chef/knife/cookbook_delete.rb, line 74
    def delete_without_explicit_version
      if available_versions.nil?
        # we already logged an error or 2 about it, so just bail
        exit(1)
      elsif available_versions.size == 1
        @version = available_versions.first
        delete_explicit_version
      else
        versions_to_delete = ask_which_versions_to_delete
        delete_versions_without_confirmation(versions_to_delete)
      end
    end

run() click to toggle source

    # File lib/chef/knife/cookbook_delete.rb, line 37
    def run
      confirm("Files that are common to multiple cookbooks are shared, so purging the files may disable other cookbooks. Are you sure you want to purge files instead of just deleting the cookbook") if config[:purge]
      @cookbook_name, @version = name_args
      if @cookbook_name && @version
        delete_explicit_version
      elsif @cookbook_name && config[:all]
        delete_all_versions
      elsif @cookbook_name && @version.nil?
        delete_without_explicit_version
      elsif @cookbook_name.nil?
        show_usage
        ui.fatal("You must provide the name of the cookbook to delete")
        exit(1)
      end
    end