Sha256: 4032ba7ad2ba4dd5e02c8e930ff9fb841f54fa42108c9e38abfc2b88d7851693

Contents?: true

Size: 1.4 KB

Versions: 1

Compression:

Stored size: 1.4 KB

Contents

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
knife-undev-0.0.4 lib/knife/undev/converter.rb