require 'yaml' require 'chef/role' require 'chef/environment' module Knife module Undev class Converter def initialize(filename, klass = 'role') @filename = filename @klass = klass end def to_yml case @klass when 'env' env_to_yml when 'role' role_to_yml end end def safe_delete(hash, key) hash.delete(key) if hash[key].nil? || hash[key].empty? end def role_to_yml r = Chef::Role.new r.from_file(@filename) hash = r.to_hash.dup result_hash = Hash.new hash.delete('chef_type') hash.delete('json_class') safe_delete(hash, 'override_attributes') safe_delete(hash, 'run_list') result_hash['name'] = hash.delete('name') result_hash['description'] = hash.delete('description') result_hash['env_run_lists'] = hash.delete('env_run_lists') puts result_hash.merge(hash).to_yaml end def env_to_yml r = Chef::Environment.new r.from_file(@filename) hash = r.to_hash.dup hash.delete('chef_type') hash.delete('json_class') safe_delete(hash, 'override_attributes') safe_delete(hash, 'default_attributes') safe_delete(hash, 'cookbook_versions') safe_delete(hash, 'description') puts hash.to_yaml end end end end