Module: Cfer::Cli

Defined in:
lib/cfer/cli.rb

Constant Summary collapse

CFER_CLI =
Cri::Command.define do
  name 'cfer'
  description 'Toolkit and Ruby DSL for automating infrastructure using AWS CloudFormation'
  flag nil, 'verbose', 'Runs Cfer with debug output enabled'

  optional :p, 'profile', 'The AWS profile to use from your credentials file'
  optional :r, 'region', 'The AWS region to use'

  optional nil, 'output-format', 'The output format to use when printing a stack [table|json]'

  optional nil, 'parameter', 'Sets a parameter to pass into the stack (format: `name:value`)', multiple: true
  optional nil, 'parameter-file', 'A YAML or JSON file with CloudFormation parameters to pass to the stack'
  optional nil, 'parameter-environment', 'If parameter_file is set, will merge the subkey of this into the parameter list.'

  flag :v, 'version', 'show the current version of cfer' do |value, cmd|
    puts Cfer::VERSION
    exit 0
  end

  flag  :h, 'help',  'show help for this command' do |value, cmd|
    puts cmd.help
    exit 0
  end
end
PARAM_REGEX =
/(?<name>.+?)=(?<value>.+)/

Class Method Summary collapse

Class Method Details

.extract_parameters(params, args) ⇒ Object



183
184
185
186
187
188
189
190
191
192
# File 'lib/cfer/cli.rb', line 183

def self.extract_parameters(params, args)
  args.reject do |arg|
    if match = PARAM_REGEX.match(arg)
      name = match[:name]
      value = match[:value]
      Cfer::LOGGER.debug "Extracting parameter #{name}: #{value}"
      params[name] = value
    end
  end
end

.fixup_options(opts) ⇒ Object

Convert options of the form :'some-option' into :some_option. Cfer internally uses the latter format, while Cri options must be specified as the former. This approach is better than changing the names of all the options in the CLI.



197
198
199
200
201
202
203
204
205
# File 'lib/cfer/cli.rb', line 197

def self.fixup_options(opts)
  opts.keys.map(&:to_s).each do |k|
    old_k = k.to_sym
    new_k = k.gsub('-', '_').to_sym
    val = opts[old_k]
    opts[new_k] = (Integer(val) rescue Float(val) rescue val)
    opts.delete(old_k) if old_k != new_k
  end
end

.main(args) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/cfer/cli.rb', line 150

def self.main(args)
  Cfer::LOGGER.debug "Cfer version #{Cfer::VERSION}"
  begin
    CFER_CLI.run(args)
  rescue Aws::Errors::NoSuchProfileError => e
    Cfer::LOGGER.error "#{e.message}. Specify a valid profile with the --profile option."
    exit 1
  rescue Aws::Errors::MissingRegionError => e
    Cfer::LOGGER.error "Missing region. Specify a valid AWS region with the --region option, or use the AWS_REGION environment variable."
    exit 1
  rescue Interrupt
    Cfer::LOGGER.info 'Caught interrupt. Goodbye.'
  rescue Cfer::Util::TemplateError => e
    Cfer::LOGGER.fatal "Template error: #{e.message}"
    Cfer::LOGGER.fatal Cfer::Cli.format_backtrace(e.template_backtrace) unless e.template_backtrace.empty?
    exit 1
  rescue Cfer::Util::CferError, Cfer::Util::StackDoesNotExistError => e
    Cfer::LOGGER.error "#{e.message}"
    exit 1
  rescue StandardError => e
    Cfer::LOGGER.fatal "#{e.class.name}: #{e.message}"
    Cfer::LOGGER.fatal Cfer::Cli.format_backtrace(e.backtrace) unless e.backtrace.empty?

    if Cfer::DEBUG
      Pry::rescued(e)
    else
      #Cfer::Util.bug_report(e)
    end
    exit 1
  end
end