class CfnDslPipeline::CliOptions

Command Line Options processing

Constants

USAGE

Attributes

cfndsl_extras[RW]
op[RW]
output[RW]
pipeline[RW]
template[RW]

Public Class Methods

new() click to toggle source
# File lib/cli_options.rb, line 12
def initialize
  @output = './'
  @cfndsl_extras = []
  @pipeline = CfnDslPipeline::Options.new
  parse && validate
end

Private Instance Methods

fatal(msg) click to toggle source

rubocop:enable Metrics/AbcSize rubocop:enable Metrics/MethodLength rubocop:enable Metrics/BlockLength

# File lib/cli_options.rb, line 103
def fatal(msg)
  puts msg
  puts @op
  exit 1
end
parse() click to toggle source

rubocop:disable Metrics/AbcSize rubocop:disable Metrics/MethodLength rubocop:disable Metrics/BlockLength

# File lib/cli_options.rb, line 24
def parse
  @op = OptionParser.new do |opts|
    opts.banner = USAGE

    opts.on('-o', '--output dir', 'Optional output directory. Default is current directory') do |dir|
      @output = dir
    end

    opts.on('-b', '--bucket', 'Optional Existing S3 bucket for cost estimation and large template syntax validation') do |bucket|
      pipeline.validation_bucket = bucket
    end

    opts.on('-p', '--params', 'Create cloudformation deploy compatible params file') do
      pipeline.dump_deploy_params = true
    end

    opts.on('-s', '--syntax', 'Enable syntax check') do
      pipeline.validate_syntax = true
    end

    opts.on('--syntax-report', 'Save template syntax report') do
      pipeline.save_syntax_report = true
    end

    opts.on('-a', '--audit', 'Enable cfn_nag audit') do
      pipeline.validate_cfn_nag = false
    end

    opts.on('--audit-rule-dir', 'cfn_nag audit custom rules directory') do |rule_dir|
      pipeline.cfn_nag[:rule_directory] = rule_dir
    end

    opts.on('--audit-report', 'Save cfn_nag audit report') do
      pipeline.save_audit_report = true
    end

    opts.on('--audit-debug', 'Enable cfn_nag rule debug output') do
      pipeline.debug_audit = true
    end

    opts.on('-e', '--estimate-costs', 'Generate URL for AWS simple cost calculator') do
      pipeline.estimate_cost = true
    end

    opts.on('-r', '--aws-region', 'AWS region to use. Default: ap-southeast-2') do |region|
      pipeline.aws_region = region
    end

    opts.on_tail('-h', '--help', 'show this message') do
      puts opts
      exit
    end

    opts.on_tail('-d', '--debug', 'show pipeline debug messages') do
      pipeline.debug = true
      exit
    end

    opts.on_tail('-v', '--version', 'Show version') do
      puts CfnDsl::Pipeline::VERSION
      exit
    end
  end
  @op.parse!

  # first non-dash parameter is the mandatory input file
  @template = ARGV.pop
  # rubocop:disable Style/MultilineIfModifier
  ARGV.each do |arg|
    @cfndsl_extras << [:yaml, arg]
  end unless ARGV.empty?
  # rubocop:enable Style/MultilineIfModifier

  pipeline
end
validate() click to toggle source

rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/PerceivedComplexity

# File lib/cli_options.rb, line 111
def validate
  # Exit on invalid option combinations
  fatal 'Error: Input template file does not exist.' unless @template && File.file?(@template)

  if @pipeline.save_syntax_report
    fatal 'Error: save syntax report is set, but syntax validation was not enabled.' unless @pipeline.validate_syntax && !@pipeline.save_syntax_report
  end
  # rubocop:disable  Style/GuardClause
  if @pipeline.cfn_nag.rule_directory || @pipeline.debug_audit || @pipeline.save_audit_report
    fatal 'Error: Audit options set, but audit was not enabled' unless @pipeline.validate_cfn_nag
    fatal 'Error: cfn_nag rule directory does not exist' unless File.directory?(@pipeline.cfn_nag.rule_directory)
  end
  # rubocop:enable  Style/GuardClause
end