Class: Chef::Knife::CookbookMetadata

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

Constant Summary

Constants inherited from Chef::Knife

CHEF_ORGANIZATION_MANAGEMENT, KNIFE_ROOT, OFFICIAL_PLUGINS, OPSCODE_HOSTED_CHEF_ACCESS_CONTROL, VERSION

Instance Attribute Summary

Attributes inherited from Chef::Knife

#name_args, #ui

Instance Method Summary collapse

Methods inherited from Chef::Knife

#api_key, #apply_computed_config, category, chef_config_dir, common_name, #config_file_defaults, #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, #root_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

Constructor Details

This class inherits a constructor from Chef::Knife

Instance Method Details

#generate_metadata(cookbook) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/chef/knife/cookbook_metadata.rb', line 62

def (cookbook)
  Array(config[:cookbook_path]).reverse_each do |path|
    file = File.expand_path(File.join(path, cookbook, "metadata.rb"))
    if File.exist?(file)
      (cookbook, file)
    else
      (path, cookbook)
    end
  end
end

#generate_metadata_from_file(cookbook, file) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/chef/knife/cookbook_metadata.rb', line 73

def (cookbook, file)
  ui.info("Generating metadata for #{cookbook} from #{file}")
  md = Chef::Cookbook::Metadata.new
  md.name(cookbook)
  md.from_file(file)
  json_file = File.join(File.dirname(file), "metadata.json")
  File.open(json_file, "w") do |f|
    f.write(Chef::JSONCompat.to_json_pretty(md))
  end
  Chef::Log.trace("Generated #{json_file}")
rescue Exceptions::ObsoleteDependencySyntax, Exceptions::InvalidVersionConstraint => e
  ui.stderr.puts "ERROR: The cookbook '#{cookbook}' contains invalid or obsolete metadata syntax."
  ui.stderr.puts "in #{file}:"
  ui.stderr.puts
  ui.stderr.puts e.message
  exit 1
end

#runObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/chef/knife/cookbook_metadata.rb', line 43

def run
  config[:cookbook_path] ||= Chef::Config[:cookbook_path]

  if config[:all]
    cl = Chef::CookbookLoader.new(config[:cookbook_path])
    cl.load_cookbooks
    cl.each_key do |cname|
      (cname.to_s)
    end
  else
    cookbook_name = @name_args[0]
    if cookbook_name.nil? || cookbook_name.empty?
      ui.error "You must specify the cookbook to generate metadata for, or use the --all option."
      exit 1
    end
    (cookbook_name)
  end
end

#validate_metadata_json(path, cookbook) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/chef/knife/cookbook_metadata.rb', line 91

def (path, cookbook)
  json_file = File.join(path, cookbook, "metadata.json")
  if File.exist?(json_file)
    Chef::Cookbook::Metadata.validate_json(IO.read(json_file))
  end
rescue Exceptions::ObsoleteDependencySyntax, Exceptions::InvalidVersionConstraint => e
  ui.stderr.puts "ERROR: The cookbook '#{cookbook}' contains invalid or obsolete metadata syntax."
  ui.stderr.puts "in #{json_file}:"
  ui.stderr.puts
  ui.stderr.puts e.message
  exit 1
end