Module: Aws::Cfn::Compiler::Save

Included in:
Base
Defined in:
lib/aws/cfn/compiler/mixins/save.rb

Instance Method Summary collapse

Instance Method Details

#save_inifile(dir, file, section, parameters, brick = nil) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/aws/cfn/compiler/mixins/save.rb', line 7

def save_inifile(dir, file, section, parameters, brick=nil)
  brick ||= "INI #{section}"
  path = File.join(File.expand_path(dir), file)
  filn = if @config[:expandedpaths]
           path
         else
           File.join(dir, file)
         end
  logStep "Saving #{brick} to #{filn} "

  if i_am_maintainer(path) or @config[:force]
    begin
      ini = IniFile.new

      # noinspection RubyStringKeysInHashInspection
      parms = {
          'region' => 'us-east-1'
      }
      parameters.map{ |p| parms[p['ParameterKey']] = p['ParameterValue'] }
      ini['<StackName>'] = parms
      ini.write( filename: path )
      @logger.info "  saved #{filn}."
    rescue
      abort! "!!! Could not write file #{path}: #{$!}"
    end
  else
    @logger.warn "  Did not overwrite #{filn}."
  end
end

#save_section(dir, file, format, section, hash, join = '/', brick = nil) ⇒ Object



37
38
39
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
73
74
75
76
77
78
79
80
# File 'lib/aws/cfn/compiler/mixins/save.rb', line 37

def save_section(dir, file, format, section, hash, join='/', brick=nil)
  brick ||= "brick #{hash.keys[0]}"
  path = File.join(File.expand_path(dir), file)
  filn = if @config[:expandedpaths]
           path
         else
           File.join(dir, file)
         end
  logStep "Saving #{brick} to #{filn} "
  # noinspection RubyAssignmentExpressionInConditionalInspection
  if match = file.match(%r'\.([^\.]*)$')
    if (section != '') and (not formats_compatible?(format, match[1]))
      msg = "The file extension (#{match[1]}) does not match the chosen output format (#{format})!"
      if @config[:force]
        logger.warn msg
      else
        abort! "#{msg}\n\tIf you are SURE this is what you want then use the --force option!"
      end
    end
  end

  if i_am_maintainer(path) or @config[:force]
    begin
      File.open path, File::CREAT|File::TRUNC|File::RDWR, 0644 do |f|
        case format
          when /ruby|rb|yaml|yml/
            f.write maintainer_comment('')
            f.write hash.to_yaml line_width: 1024, indentation: 4, canonical: false
          when /json|js/
            # You wish ... f.write maintainer_comment('')
            f.write JSON.pretty_generate(hash, { indent: "\t", space: ' '})
          else
            abort! "Internal: Unsupported format #{format}. Should have noticed this earlier!"
        end
        f.close
      end
      @logger.info "  saved #{filn}."
    rescue
      abort! "!!! Could not write file #{path}: \nException: #{$!}\nParent directory exists? #{File.directory?(File.dirname(path))}\n"
    end
  else
    @logger.warn "  Did not overwrite #{filn}."
  end
end