lib/knife/undev/converter.rb in knife-undev-0.0.6 vs lib/knife/undev/converter.rb in knife-undev-0.0.7
- old
+ new
@@ -3,17 +3,23 @@
require 'chef/environment'
module Knife
module Undev
class Converter
-
+
def initialize(filename, klass = 'role')
@filename = filename
@klass = klass
@comment_counter = 0
end
+ def stringify_keys(obj)
+ return obj.inject({}){|memo,(k,v)| memo[k.to_s] = stringify_keys(v); memo} if obj.is_a? Hash
+ return obj.inject([]){|memo,v | memo << stringify_keys(v); memo} if obj.is_a? Array
+ obj
+ end
+
def to_yml
case @klass
when 'env'
env_to_yml
when 'role'
@@ -30,11 +36,11 @@
end
def role_to_yml
r = Chef::Role.new
r.from_file(@filename)
- hash = r.to_hash.dup
+ hash = stringify_keys(r.to_hash)
result_hash = Hash.new
hash.delete('chef_type')
hash.delete('json_class')
safe_delete(hash, 'override_attributes')
safe_delete(hash, 'run_list')
@@ -44,29 +50,29 @@
result_hash['env_run_lists'] = hash.delete('env_run_lists')
comment(result_hash)
result_hash.merge(hash).to_yaml.each_line do |line|
puts '' if line.match /#comment_\d+/
next if line.match /#comment_\d+/
- next if line == '---'
+ next if line.match '^---'
puts line
end
end
def env_to_yml
r = Chef::Environment.new
r.from_file(@filename)
- hash = r.to_hash.dup
+ hash = stringify_keys(r.to_hash)
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')
hash.to_yaml.each_line do |line|
- next if line == '---'
+ next if line.match '^---'
puts line
end
end
-
+
end
end
end