Sha256: cc1ec7d9e23b86068502805be59ae238930b0d14d53752f043feb8a1c605cad8

Contents?: true

Size: 1.64 KB

Versions: 11

Compression:

Stored size: 1.64 KB

Contents

#!/usr/bin/env ruby
require 'json'
require 'pp'
require 'active_support/inflector'

template = JSON.parse(STDIN.read)

def indent(str, prefix = '  ')
  prefix + str.gsub("\n", "\n#{prefix}")
end

def serialize_arg(arg, indent=0)
  ret = ""
  PP.pp(arg, ret)
  ret.strip
end

def serialize_args(*args)
  options = args.last.is_a?(Hash) ? args.pop : {}
  args_str = []

  args.each do |arg|
    args_str << serialize_arg(arg)
  end

  opts_str = []
  options.each do |k, v|
    opts_str << "#{serialize_arg(k)}: #{serialize_arg(v)}"
  end
  args_str << indent(opts_str.join(",\n"), '  ') unless opts_str.empty?

  args_str.join(', ').strip
end

description = template.delete('Description')
parameters = template.delete('Parameters') || {}
resources = template.delete('Resources') || {}
outputs = template.delete('Outputs') || {}
version = template.delete('AWSTemplateFormatVersion')

if description
  puts "description #{serialize_arg(description)}"
  puts ""
end

puts "##############"
puts "# Parameters #"
puts "##############"

parameters.each do |k, param|
  puts "parameter #{serialize_args(k, param)}"
  puts ""
end

puts "#############"
puts "# Resources #"
puts "#############"

resources.each do |k, attrs|
  properties = attrs.delete('Properties') || {}
  type = attrs.delete('Type')

  puts "resource #{serialize_args(k, type, attrs)} do"
  properties.each do |k, v|
    puts indent("#{k.underscore} #{serialize_args(v)}", '  ')
  end
  puts "end"
  puts ""
end

outputs.each do |k, out|
  value = out.delete('Value')

  puts "output #{serialize_args(k, value, out)}"
end

puts ""

template.each do |k, v|
  puts "self[#{serialize_arg(k)}] = #{serialize_arg(v)}"
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
cfer-1.0.0 bin/json-to-cfer
cfer-0.8.0 bin/json-to-cfer
cfer-0.7.0 bin/json-to-cfer
cfer-0.6.2 bin/json-to-cfer
cfer-0.6.1 bin/json-to-cfer
cfer-0.6.0 bin/json-to-cfer
cfer-0.5.0 bin/json-to-cfer
cfer-0.5.0.pre.rc4 bin/json-to-cfer
cfer-0.5.0.pre.rc3 bin/json-to-cfer
cfer-0.5.0.pre.rc2 bin/json-to-cfer
cfer-0.5.0.pre.rc1 bin/json-to-cfer