Class: Chef::Knife::CookbookMetadata

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

Instance Attribute Summary

Attributes inherited from Chef::Knife

#name_args, #ui

Instance Method Summary collapse

Methods inherited from Chef::Knife

#api_key, category, common_name, #configure_chef, #create_object, #delete_object, deps, #format_rest_error, guess_category, #highlight_config_error, #humanize_exception, #humanize_http_exception, inherited, #initialize, list_commands, load_commands, load_deps, msg, #noauth_rest, #parse_options, #read_config_file, reset_subcommands!, #rest, run, #run_with_pretty_exceptions, #server_url, #show_usage, snake_case_name, subcommand_category, subcommand_class_from, subcommand_loader, subcommands, subcommands_by_category, ui, unnamed?, #username

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

#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.exists?(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
90
# 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
  generated = true
  Chef::Log.debug("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


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 44

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

  if config[:all]
    cl = Chef::CookbookLoader.new(config[:cookbook_path])
    cl.each do |cname, cookbook|
      (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


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

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