Class: Ufo::DSL::Outputter

Inherits:
Object
  • Object
show all
Defined in:
lib/ufo/dsl/outputter.rb

Instance Method Summary collapse

Constructor Details

#initialize(name, erb_result, options = {}) ⇒ Outputter

Returns a new instance of Outputter.



4
5
6
7
8
# File 'lib/ufo/dsl/outputter.rb', line 4

def initialize(name, erb_result, options={})
  @name = name
  @erb_result = erb_result
  @options = options
end

Instance Method Details

#override_image(data) ⇒ Object



24
25
26
27
28
29
# File 'lib/ufo/dsl/outputter.rb', line 24

def override_image(data)
  return data unless @options[:image_override]
  data["containerDefinitions"].each do |container_definition|
    container_definition["image"] = @options[:image_override]
  end
end

#validate(json, path) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/ufo/dsl/outputter.rb', line 31

def validate(json, path)
  begin
    JSON.parse(json)
  rescue JSON::ParserError => e
    puts "#{e.class}: #{e.message}"
    puts "Invalid json.  Output written to #{path} for debugging".color(:red)
    File.open(path, 'w') {|f| f.write(json) }
    exit 1
  end
end

#writeObject



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/ufo/dsl/outputter.rb', line 10

def write
  output_path = "#{Ufo.root}/.ufo/output"
  FileUtils.rm_rf(output_path) if @options[:clean]
  FileUtils.mkdir(output_path) unless File.exist?(output_path)

  path = "#{output_path}/#{@name}.json".sub(/^\.\//,'')
  puts "  #{path}" unless @options[:quiet]
  validate(@erb_result, path)
  data = JSON.parse(@erb_result)
  override_image(data)
  json = JSON.pretty_generate(data)
  File.open(path, 'w') {|f| f.write(json) }
end