Class: Chef::Knife::Delete

Inherits:
ChefFS::Knife show all
Defined in:
lib/chef/knife/delete.rb

Constant Summary

Constants inherited from Chef::Knife

CHEF_ORGANIZATION_MANAGEMENT, OFFICIAL_PLUGINS, OPSCODE_HOSTED_CHEF_ACCESS_CONTROL

Instance Attribute Summary

Attributes inherited from Chef::Knife

#name_args, #ui

Instance Method Summary collapse

Methods inherited from ChefFS::Knife

#chef_fs, #configure_chef, #create_chef_fs, #create_local_fs, deps, #discover_repo_dir, #format_path, inherited, #local_fs, #parallelize, #pattern_arg_from, #pattern_args, #pattern_args_from

Methods inherited from Chef::Knife

#api_key, #apply_computed_config, category, chef_config_dir, #cli_keys, common_name, #config_file_settings, config_loader, #config_source, #configure_chef, #create_object, #delete_object, dependency_loaders, deps, #format_rest_error, guess_category, #humanize_exception, #humanize_http_exception, inherited, #initialize, list_commands, load_commands, load_config, load_deps, #maybe_setup_fips, #merge_configs, msg, #noauth_rest, #parse_options, reset_config_loader!, reset_subcommands!, #rest, run, #run_with_pretty_exceptions, #server_url, #show_usage, snake_case_name, subcommand_category, subcommand_class_from, subcommand_files, subcommand_loader, subcommands, subcommands_by_category, #test_mandatory_field, ui, unnamed?, use_separate_defaults?, #username

Methods included from Mixin::ConvertToClassName

#constantize, #convert_to_class_name, #convert_to_snake_case, #filename_to_qualified_string, #normalize_snake_case_name, #snake_case_basename

Constructor Details

This class inherits a constructor from Chef::Knife

Instance Method Details

#delete_result(*results) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/chef/knife/delete.rb', line 94

def delete_result(*results)
  deleted_any = false
  found_any = false
  error = false
  results.each do |result|
    begin
      result.delete(config[:recurse])
      deleted_any = true
      found_any = true
    rescue Chef::ChefFS::FileSystem::NotFoundError
      # This is not an error unless *all* of them were not found
    rescue Chef::ChefFS::FileSystem::MustDeleteRecursivelyError => e
      ui.error "#{format_path_with_root(e.entry)} must be deleted recursively!  Pass -r to knife delete."
      found_any = true
      error = true
    rescue Chef::ChefFS::FileSystem::OperationNotAllowedError => e
      ui.error "#{format_path_with_root(e.entry)} #{e.reason}."
      found_any = true
      error = true
    end
  end
  if deleted_any
    output("Deleted #{format_path(results[0])}")
  elsif !found_any
    ui.error "#{format_path(results[0])}: No such file or directory"
    error = true
  end
  error
end

#format_path_with_root(entry) ⇒ Object



89
90
91
92
# File 'lib/chef/knife/delete.rb', line 89

def format_path_with_root(entry)
  root = entry.root == chef_fs ? " (remote)" : " (local)"
  "#{format_path(entry)}#{root}"
end

#runObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/chef/knife/delete.rb', line 49

def run
  if name_args.length == 0
    show_usage
    ui.fatal("You must specify at least one argument. If you want to delete everything in this directory, run \"knife delete --recurse .\"")
    exit 1
  end

  # Get the matches (recursively)
  error = false
  if config[:local]
    pattern_args.each do |pattern|
      Chef::ChefFS::FileSystem.list(local_fs, pattern).each do |result|
        if delete_result(result)
          error = true
        end
      end
    end
  elsif config[:both]
    pattern_args.each do |pattern|
      Chef::ChefFS::FileSystem.list_pairs(pattern, chef_fs, local_fs).each do |chef_result, local_result|
        if delete_result(chef_result, local_result)
          error = true
        end
      end
    end
  else # Remote only
    pattern_args.each do |pattern|
      Chef::ChefFS::FileSystem.list(chef_fs, pattern).each do |result|
        if delete_result(result)
          error = true
        end
      end
    end
  end

  if error
    exit 1
  end
end