Class: Chefdore::Magic

Inherits:
Object
  • Object
show all
Defined in:
lib/chefdore/utils.rb

Class Method Summary collapse

Class Method Details

.convert(opts = {}) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/chefdore/utils.rb', line 46

def self.convert(opts = {})
  json = opts[:json] ? opts[:json] : fail("You must pass some JSON in :json opt")
  cli  = opts[:cli]  ? opts[:cli]  : fail("You must pass the CLI in :cli opt")
  klass = Chef::JSONCompat.from_json(json)

  rl = klass.run_list
  da = klass.default_attributes
  oa = klass.override_attributes

  puts "## Place the following in your cookbook in attributes/default.rb"
  convert_attr(cli: cli, value: da)
  puts

  puts "## Place the following in your cookbook in attributes/overrides.rb (alternatively, you may place these in attributes/default.rb instead)"
  convert_attr(cli: cli, value: oa, prefix: "override")
  puts

  puts "## Place the following in your cookbook in recipes/default.rb"
  convert_run_list(cli: cli, run_list: rl)
end

.convert_attr(opts = {}) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/chefdore/utils.rb', line 18

def self.convert_attr(opts = {})
  value  = opts[:value]
  prefix = opts[:prefix] ? opts[:prefix] : "default"
  path   = opts[:path]   ? opts[:path]   : []
  cli    = opts[:cli]    ? opts[:cli]    : Chefdore::Cli.new

  #puts "DEBUG :: #{prefix} -- #{path} -- #{value.inspect} (#{value.class})"

  if value.is_a?(Hash) and value.empty? and path.empty?
    nil
  elsif value.is_a?(Hash)
    # This allows us to include empty hashes if one is found
    puts "#{prefix}#{path.map{|p| "[#{p.inspect}]"}.join('')} = #{value.inspect}" if value.empty?

    value.each do |subkey, subval|
      convert_attr(opts.merge(value: subval, path: path+[subkey]))
    end
  elsif value.is_a?(Array) && cli.options[:append_arrays]
    puts "#{prefix}#{path.map{|p| "[#{p.inspect}]"}.join('')} = #{Array.new.inspect}"
    value.each do |x|
      puts "#{prefix}#{path.map{|p| "[#{p.inspect}]"}.join('')} << #{x.inspect}"
    end
  else
    puts "#{prefix}#{path.map{|p| "[#{p.inspect}]"}.join('')} = #{value.inspect}"
  end
end

.convert_run_list(opts = {}) ⇒ Object



8
9
10
11
12
13
14
15
# File 'lib/chefdore/utils.rb', line 8

def self.convert_run_list(opts = {})
  rl  = opts[:run_list] ? opts[:run_list] : []
  cli = opts[:cli]      ? opts[:cli]      : Chefdore::Cli.new

  rl.recipes.each do |x|
    puts "include_recipe #{x.inspect}"
  end
end