class DoSnapshot::CLI

CLI is here

Public Class Methods

new(*args) click to toggle source

Overriding Thor method for custom initialization

Calls superclass method
# File lib/do_snapshot/cli.rb, line 22
def initialize(*args)
  super

  setup_config
  set_logger
  set_mailer

  # Check for keys via options
  %w( digital_ocean_access_token ).each do |key|
    ENV[key.upcase] = options[key] if options.include? key
  end
end

Public Instance Methods

backtrace(e) click to toggle source
# File lib/do_snapshot/cli.rb, line 225
def backtrace(e)
  e.backtrace.each do |t|
    logger.error t
  end
end
command() click to toggle source
# File lib/do_snapshot/cli.rb, line 187
def command
  @command ||= Command.new(options, command_filter)
end
command_filter() click to toggle source
# File lib/do_snapshot/cli.rb, line 195
def command_filter
  %w( log smtp mail trace digital_ocean_access_token )
end
error_simple(e) click to toggle source
# File lib/do_snapshot/cli.rb, line 174
def error_simple(e)
  logger.error e.message
  send_error
  fail e
end
error_with_backtrace(e) click to toggle source
# File lib/do_snapshot/cli.rb, line 180
def error_with_backtrace(e)
  logger.error e.message
  backtrace(e) if options.include? 'trace'
  send_error
  fail e
end
send_error() click to toggle source
# File lib/do_snapshot/cli.rb, line 213
def send_error
  return unless DoSnapshot.mailer.respond_to?(:opts)

  DoSnapshot.mailer.opts[:subject] = 'Digital Ocean: Error.'
  DoSnapshot.mailer.opts[:body] = 'Please check your droplets.'
  mailer.notify
end
set_logger() click to toggle source
# File lib/do_snapshot/cli.rb, line 221
def set_logger
  DoSnapshot.logger = Log.new(shell: shell)
end
set_mailer() click to toggle source
# File lib/do_snapshot/cli.rb, line 209
def set_mailer
  DoSnapshot.mailer = DoSnapshot.config.mailer
end
setup_config() click to toggle source
# File lib/do_snapshot/cli.rb, line 199
def setup_config # rubocop:disable Metrics/AbcSize
  DoSnapshot.configure do |config|
    config.logger = ::Logger.new(options['log']) if options['log']
    config.logger_level = Logger::DEBUG if config.verbose
    config.verbose = options['trace']
    config.quiet = options['quiet']
    config.mailer = Mail.new(opts: options['mail'], smtp: options['smtp']) if options['mail']
  end
end
snap() click to toggle source
# File lib/do_snapshot/cli.rb, line 159
def snap
  command.snap
rescue DoSnapshot::NoTokenError, DoSnapshot::NoKeysError => e
  error_simple(e)
rescue => e
  command.fail_power_off(e) if [SnapshotCreateError, DropletShutdownError].include?(e.class)
  error_with_backtrace(e)
end
update_command() click to toggle source
# File lib/do_snapshot/cli.rb, line 191
def update_command
  command.load_options(options, command_filter)
end
version() click to toggle source
# File lib/do_snapshot/cli.rb, line 169
def version
  puts DoSnapshot::VERSION
end