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'
DEFAULT_DIRECTORY =
'./'

Constants inherited from Base

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 = '.') ⇒ Object



40
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
# File 'lib/cloudit/command/generate.rb', line 40

def generate_json(dir='.')
  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

  hash.to_json
end

#indexObject



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
# File 'lib/cloudit/command/generate.rb', line 14

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

    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)
    $stdout.puts json
    File.new(out, 'w').write "#{json}\n"
  end
end