Class: CfnDsl::Runner
- Inherits:
-
Object
- Object
- CfnDsl::Runner
- Defined in:
- lib/cfndsl/runner.rb
Overview
Runner class to handle commandline invocation
Class Method Summary collapse
-
.invoke! ⇒ Object
rubocop:disable Metrics/MethodLength, Metrics/AbcSize, Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity.
Class Method Details
.invoke! ⇒ Object
rubocop:disable Metrics/MethodLength, Metrics/AbcSize, Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity
12 13 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 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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/cfndsl/runner.rb', line 12 def self.invoke! = {} optparse = OptionParser.new do |opts| opts.version = CfnDsl::VERSION opts. = 'Usage: cfndsl [options] FILE' # Define the options, and what they do [:output] = '-' opts.on('-o', '--output FILE', 'Write output to file') do |file| [:output] = file end [:extras] = [] opts.on('-y', '--yaml FILE', 'Import yaml file as local variables') do |file| [:extras].push([:yaml, File.(file)]) end opts.on('-j', '--json FILE', 'Import json file as local variables') do |file| [:extras].push([:json, File.(file)]) end opts.on('-p', '--pretty', 'Pretty-format output JSON') do [:pretty] = true end opts.on('-f', '--format FORMAT', 'Specify the output format (JSON default)') do |format| raise "Format #{format} not supported" unless %w[json yaml].include? format [:outformat] = format end opts.on('-D', '--define "VARIABLE=VALUE"', 'Directly set local VARIABLE as VALUE') do |file| [:extras].push([:raw, file]) end [:verbose] = false opts.on('-v', '--verbose', 'Turn on verbose output') do [:verbose] = true end opts.on('-m', '--disable-deep-merge', 'Disable deep merging of yaml') do CfnDsl.disable_deep_merge end # TODO: Support options to add a spec/patches dir opts.on('-s', '--specification-file FILE', 'Location of Cloudformation Resource Specification file') do |file| CfnDsl.specification_file File.(file) end opts.on('-u', '--update-specification [VERSION]', 'Update the Resource Specification file to latest, or specific version') do |file| [:spec_version] = file || 'latest' [:update_spec] = true end opts.on('-g', '--generate RESOURCE_TYPE,RESOURCE_LOGICAL_NAME', 'Add resource type and logical name') do |r| [:lego] = true [:resources] = [] [:resources] << r end opts.on('-a', '--assetversion', 'Print out the specification version') do [:assetversion] = true end opts.on('-l', '--list', 'List supported resources') do require_relative 'cfnlego' puts Cfnlego.resources.sort exit end # This displays the help screen, all programs are # assumed to have this option. opts.on('-h', '--help', 'Display this screen') do puts opts exit end end optparse.parse! if [:update_spec] warn 'Updating specification file' result = CfnDsl.update_specification_file(version: [:spec_version]) warn "Specification #{result[:version]} successfully written to #{result[:file]}" end if [:assetversion] spec_file = JSON.parse File.read(CfnDsl.specification_file) warn spec_file['ResourceSpecificationVersion'] end if [:lego] require_relative 'cfnlego' puts Cfnlego.run() exit end if ARGV.empty? if [:update_spec] || [:assetversion] exit 0 else puts optparse.help exit 1 end end filename = File.(ARGV[0]) verbose = [:verbose] && $stderr verbose.puts "Using specification file #{CfnDsl.specification_file}" if verbose require_relative 'cloudformation' model = CfnDsl.eval_file_with_extras(filename, [:extras], verbose) output = $stdout if [:output] != '-' verbose.puts("Writing to #{[:output]}") if verbose output = File.open(File.([:output]), 'w') elsif verbose verbose.puts('Writing to STDOUT') end if [:outformat] == 'yaml' data = model.to_json output.puts JSON.parse(data).to_yaml else output.puts [:pretty] ? JSON.pretty_generate(model) : model.to_json end end |