Module: CfnToml

Defined in:
lib/cfn_toml.rb,
lib/cfn_toml/version.rb

Constant Summary collapse

VERSION =
'1.0.12'

Class Method Summary collapse

Class Method Details

.init(toml_filepath, cfn_filepath, stackname, profile) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/cfn_toml.rb', line 6

def self.init toml_filepath, cfn_filepath, stackname, profile
  region = `aws configure get region --profile #{profile}`
  region = 'us-east-1' if region.nil? || region == ''
  stackname ||= 'stackname'

  toml_path = File.dirname toml_filepath
  FileUtils.mkpath(toml_path) unless File.exist?(toml_path)

  File.open(toml_filepath, "w") do |f| 
    f.write "[deploy]\n"
    f.write "profile = '#{profile}'\n"
    f.write "stack_name = '#{stackname}'\n"
    f.write "region = '#{region.chomp}'\n\n"
    f.write "[parameters]\n"
    self.init_parameters f, cfn_filepath
  end
end

.init_parameters(f, cfn_filepath) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/cfn_toml.rb', line 24

def self.init_parameters f, cfn_filepath
  return unless File.exist?(cfn_filepath)
  contents = File.open(cfn_filepath).read
  hash = YAML.load(contents)
  return unless hash.key?('Parameters')

  hash['Parameters'].each do |name, values|
    if values.key?('Description')
      lines = values['Description'].split("\n")
      lines.each do |line|
        f.write "# #{line} \n"
      end
    end
    if values.key?('Default')
      f.write "##{name} = '#{values['Default']}' # #{values['Type']}\n"
    else
      f.write "#{name} = '' # #{values['Type']}\n"
    end
  end
end

.key(toml_filepath, key) ⇒ Object



45
46
47
48
49
# File 'lib/cfn_toml.rb', line 45

def self.key toml_filepath, key
  data = TomlRB.load_file(toml_filepath)
  namespace, key = key.split('.')
  data[namespace][key]
end

.params(toml_filepath, params_version, parameters_name = 'parameters') ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/cfn_toml.rb', line 51

def self.params toml_filepath, params_version, parameters_name='parameters'
  data = TomlRB.load_file(toml_filepath)
  parameters_name ||= 'parameters'
  if params_version == 'v1'
    data[parameters_name].map do |k,v|
      "ParameterKey=#{k},ParameterValue=#{v}"
    end.join(' ')
  elsif params_version == 'v2'
    data[parameters_name].map do |k,v|
      "#{k}=#{v}"
    end.join(' ')
  end
end