Class: Imap::Backup::CLI::Backup

Inherits:
Thor
  • Object
show all
Includes:
Helpers, Thor::Actions
Defined in:
lib/imap/backup/cli/backup.rb

Overview

Runs backups of configured accounts

Constant Summary

Constants included from Helpers

Helpers::NAMESPACE_CONFIGURATION_DESCRIPTION

Instance Method Summary collapse

Methods included from Helpers

#account, included, #load_config, #requested_accounts

Constructor Details

#initialize(options) ⇒ Backup

Returns a new instance of Backup.

Parameters:

  • options (Hash)

    CLI options controlling output

  • opts (Hash)

    a customizable set of options



22
23
24
25
# File 'lib/imap/backup/cli/backup.rb', line 22

def initialize(options)
  super([])
  @options = options
end

Instance Method Details

#runvoid

This method returns an undefined value.



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
# File 'lib/imap/backup/cli/backup.rb', line 29

no_commands do
  def run
    Logger.logger.debug "Loading configuration"
    config = load_config(**options)
    exit_code = nil
    accounts = requested_accounts(config)
    # Filter to only include accounts available for backup
    accounts = accounts.select(&:available_for_backup?)
    if accounts.none?
      Logger.logger.warn "No matching accounts found to backup"
      return
    end
    Logger.logger.debug "Starting backup of #{accounts.count} accounts"
    accounts.each do ||
      backup = Account::Backup.new(account: , refresh: refresh)
      backup.run
    rescue StandardError => e
      exit_code ||= choose_exit_code(e)
      message = <<~ERROR
        Backup for account '#{.username}' failed with error #{e}
        #{e.backtrace.join("\n")}
      ERROR
      Logger.logger.error message
      next
    end
    Logger.logger.debug "Backup complete"
    exit(exit_code) if exit_code
  end
end