Class: Cloudit::Command::Generate

Inherits:
Base
  • Object
show all
Defined in:
lib/cloudit/command/generate.rb

Constant Summary collapse

VALID_METHODS =
['help']
SECTIONS =
['Metadata', 'Parameters', 'Mappings', 'Conditions', 'Resources', 'Outputs']
DESCRIPTION =
'Build YAML files into Cloudformation template'
DEFAULT_OUT_FILE =
'out.json'
DEFAULT_MAIN_CFN_EXTENSION =
'cfn.yml'

Constants inherited from Base

Base::DEFAULT_DIRECTORY, Base::OPTION_NAME_OFFSET

Instance Method Summary collapse

Methods inherited from Base

#execute, #help, #initialize, #invalid_method, #parser, #slop_opts

Constructor Details

This class inherits a constructor from Cloudit::Command::Base

Instance Method Details

#generate_json(dir = './', min = false) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/cloudit/command/generate.rb', line 41

def generate_json(dir='./', min=false)
  hash = {}
  hash_sections = {}

  unless File.directory? dir
    $stdout.puts "cloudit: '#{dir}' must be a directory"
    return
  end

  for file in Dir.glob(Dir["#{dir}**/*.#{DEFAULT_MAIN_CFN_EXTENSION}"]) do
    yml = YAML::load_file(file)
    if yml.is_a?(Hash)
      hash.merge! yml
    end
  end

  for section in SECTIONS do
    section_file = section.downcase
    hash_sections[section] = {}

    for file in Dir.glob(Dir["#{dir}**/*.#{section_file}.yml"]) do
      yml = YAML::load_file(file)

      if yml.is_a?(Hash)
        hash_sections[section].merge! yml
      end
    end
  end

  hash.merge! hash_sections

  if min
    hash.to_json
  else
    JSON.pretty_generate hash
  end
end

#indexObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/cloudit/command/generate.rb', line 13

def index
  if @opts.help?
    $stdout.puts slop_opts
  else
    out = @opts[:output]
    dir = normalize_directory @opts[:directory]
    min = @opts[:minify]

    if File.exist? out
      if out.eql? DEFAULT_OUT_FILE
        i = 1
        while File.exist? out
          i += 1
          out = "out#{i}.json"
        end
      else
        $stdout.puts "cloudit: output file '#{out}' already exists"
        return
      end
    end

    json = generate_json dir, min
    File.new(out, 'w').write "#{json}\n"

    $stdout.puts "Template generated to #{out}"
  end
end