Class: Chef::Knife::CookbookDelete

Inherits:
Chef::Knife show all
Defined in:
lib/chef/knife/cookbook_delete.rb

Constant Summary

Constants inherited from Chef::Knife

DEFAULT_SUBCOMMAND_FILES

Instance Attribute Summary

Attributes inherited from Chef::Knife

#name_args

Instance Method Summary collapse

Methods inherited from Chef::Knife

#ask_question, #bulk_delete, category, #configure_chef, #confirm, #create_object, #delete_object, #edit_data, #edit_object, #file_exists_and_is_readable?, #format_for_display, #format_list_for_display, guess_category, inherited, #initialize, list_commands, load_commands, #load_from_file, #msg, msg, #output, #parse_options, #pretty_print, #rest, run, #show_usage, snake_case_name, #stdin, #stdout, subcommand_category, subcommand_class_from, subcommands, subcommands_by_category, unnamed?

Methods included from Mixin::ConvertToClassName

#convert_to_class_name, #convert_to_snake_case, #filename_to_qualified_string, #snake_case_basename

Constructor Details

This class inherits a constructor from Chef::Knife

Instance Method Details

#ask_which_versions_to_deleteObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/chef/knife/cookbook_delete.rb', line 92

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?
    Chef::Log.error("No versions specified, exiting")
    exit(1)
  end
  versions = responses.map do |response|
    if version = valid_responses[response]
      version
    else
      Chef::Log.error("#{response} is not a valid choice, skipping it")
    end
  end
  versions.compact
end

#available_versionsObject



81
82
83
84
85
86
87
88
89
90
# File 'lib/chef/knife/cookbook_delete.rb', line 81

def available_versions
  @available_versions ||= rest.get_rest("cookbooks/#{@cookbook_name}").values.flatten
rescue Net::HTTPServerException => e
  if e.to_s =~ /^404/
    Chef::Log.error("Cannot find a cookbook named #{@cookbook_name} to delete")
    nil
  else
    raise
  end
end

#delete_all_versionsObject



53
54
55
56
# File 'lib/chef/knife/cookbook_delete.rb', line 53

def delete_all_versions
  confirm("Do you really want to delete all versions of #{@cookbook_name}")
  delete_all_without_confirmation
end

#delete_all_without_confirmationObject



58
59
60
61
62
63
64
65
66
# File 'lib/chef/knife/cookbook_delete.rb', line 58

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_versionObject



47
48
49
50
51
# File 'lib/chef/knife/cookbook_delete.rb', line 47

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) ⇒ Object



117
118
119
120
121
# File 'lib/chef/knife/cookbook_delete.rb', line 117

def delete_version_without_confirmation(version)
  object = delete_request("cookbooks/#{@cookbook_name}/#{version}")
  output(format_for_display(object)) if config[:print_after]
  Chef::Log.info("Deleted cookbook[#{@cookbook_name}][#{version}]")
end

#delete_versions_without_confirmation(versions) ⇒ Object



123
124
125
126
127
128
129
130
131
132
# File 'lib/chef/knife/cookbook_delete.rb', line 123

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_versionObject



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/chef/knife/cookbook_delete.rb', line 68

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

#runObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/chef/knife/cookbook_delete.rb', line 31

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
    Chef::Log.fatal("You must provide the name of the cookbook to delete")
    exit(1)
  end
end