Module: Imap::Backup::CLI::Helpers

Included in:
Imap::Backup::CLI, Backup, Local, Local::Check, Migrate, Mirror, Remote, Restore, Setup, Single, Stats, Transfer, Utils
Defined in:
lib/imap/backup/cli/helpers.rb

Overview

Provides helper methods for CLI classes

Constant Summary collapse

NAMESPACE_CONFIGURATION_DESCRIPTION =

Returns a description of the namespace configuration.

Returns:

  • (String)

    a description of the namespace configuration

"Some IMAP servers use namespaces (i.e. prefixes like \"INBOX\"),\nwhile others concatenate the names of subfolders\nwith a charater (\"delimiter\") other than \"/\".\n\nIn these cases there are two choices.\n\nYou can use the `--automatic-namespaces` option.\nThis will query the source and detination servers for their\nnamespace configuration and will adapt paths accordingly.\nThis option requires that both the source and destination\nservers are available and work with the provided parameters\nand authentication.\n\nIf automatic configuration does not work as desired, there are the\n`--source-prefix=`, `--source-delimiter=`,\n`--destination-prefix=` and `--destination-delimiter=` parameters.\nTo check what values you should use, check the output of the\n`imap-backup remote namespaces EMAIL` command.\n".freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



17
18
19
20
# File 'lib/imap/backup/cli/helpers.rb', line 17

def self.included(base)
  options = CLI::Options.new(base: base)
  options.define_options
end

Instance Method Details

#account(config, email) ⇒ Account

Returns the Account information for the email address.

Returns:

  • (Account)

    the Account information for the email address

Raises:

  • (RuntimeError)

    if the account does not exist



97
98
99
100
101
102
# File 'lib/imap/backup/cli/helpers.rb', line 97

def (config, email)
   = config.accounts.find { |a| a.username == email }
  raise "#{email} is not a configured account" if !

  
end

#load_config(**options) ⇒ Configuration

Loads the application configuration

Returns:

Raises:

  • (ConfigurationNotFound)

    if the configuration file does not exist

  • (RuntimeError)

    if both config and erb_configuration are provided

  • (RuntimeError)

    if ERB template has syntax errors

  • (RuntimeError)

    if ERB template renders invalid JSON



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/imap/backup/cli/helpers.rb', line 70

def load_config(**options)
  config_path = options[:config]
  erb_config_path = options[:erb_configuration]

  # Check mutual exclusivity
  if config_path && erb_config_path
    raise "Cannot specify both --config and --erb-configuration options"
  end

  # Handle ERB configuration
  return load_erb_config(erb_config_path, options) if erb_config_path

  # Handle regular JSON configuration
  path = config_path
  require_exists = options.key?(:require_exists) ? options[:require_exists] : true
  if require_exists
    exists = Configuration.exist?(path: path)
    if !exists
      expected = path || Configuration.default_pathname
      raise ConfigurationNotFound, "Configuration file '#{expected}' not found"
    end
  end
  Configuration.new(path: path)
end

#optionsHash

Processes command-line parameters

Returns:

  • (Hash)

    the supplied command-line parameters with with hyphens in keys replaced by underscores and the keys converted to Symbols



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/imap/backup/cli/helpers.rb', line 48

def options
  @symbolized_options ||= # rubocop:disable Naming/MemoizedInstanceVariableName
    begin
      options = super
      options.each.with_object({}) do |(k, v), acc|
        key =
          if k.is_a?(String)
            k.gsub("-", "_").intern
          else
            k
          end
        acc[key] = v
      end
    end
end

#requested_accounts(config) ⇒ Array<Account>

If email addresses have been specified returns the Account configurations for them. If non have been specified, returns all account configurations

Returns:

  • (Array<Account>)

    If email addresses have been specified returns the Account configurations for them. If non have been specified, returns all account configurations



107
108
109
110
111
112
113
114
# File 'lib/imap/backup/cli/helpers.rb', line 107

def requested_accounts(config)
  emails = (options[:accounts] || "").split(",")
  if emails.any?
    config.accounts.filter { |a| emails.include?(a.username) }
  else
    config.accounts
  end
end