Sha256: 2f0fe2dcee1b428437042f02124208b32e8fcd20dff74b8826e2158ccd612523

Contents?: true

Size: 1.29 KB

Versions: 7

Compression:

Stored size: 1.29 KB

Contents

require "net/imap"
require "imap/backup/account/backup"

module Imap; end

module Imap::Backup
  class CLI::Backup < Thor
    include Thor::Actions
    include CLI::Helpers

    attr_reader :options

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

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

      def refresh
        options.key?(:refresh) ? !!options[:refresh] : false
      end

      def choose_exit_code(exception)
        case exception
        when Net::IMAP::NoResponseError, Errno::ECONNREFUSED
          111
        else
          1
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
imap-backup-13.1.0 lib/imap/backup/cli/backup.rb
imap-backup-13.0.0 lib/imap/backup/cli/backup.rb
imap-backup-12.1.0 lib/imap/backup/cli/backup.rb
imap-backup-12.0.0 lib/imap/backup/cli/backup.rb
imap-backup-11.1.0 lib/imap/backup/cli/backup.rb
imap-backup-11.1.0.rc1 lib/imap/backup/cli/backup.rb
imap-backup-11.0.1 lib/imap/backup/cli/backup.rb