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

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
58
59
60
61
62
63
64
# File 'lib/imap/backup/cli/helpers.rb', line 12

def self.included(base)
  base.class_eval do
    def self.accounts_option
      method_option(
        "accounts",
        type: :string,
        desc: "a comma-separated list of accounts (defaults to all configured accounts)",
        aliases: ["-a"]
      )
    end

    def self.config_option
      method_option(
        "config",
        type: :string,
        desc: "supply the configuration file path (default: ~/.imap-backup/config.json)",
        aliases: ["-c"]
      )
    end

    def self.format_option
      method_option(
        "format",
        type: :string,
        desc: "the output type, 'text' for plain text or 'json'",
        aliases: ["-f"]
      )
    end

    def self.quiet_option
      method_option(
        "quiet",
        type: :boolean,
        desc: "silence all output",
        aliases: ["-q"]
      )
    end

    def self.verbose_option
      method_option(
        "verbose",
        type: :boolean,
        desc:
          "increase the amount of logging. " \
          "Without this option, the program gives minimal output. " \
          "Using this option once gives more detailed output. " \
          "Whereas, using this option twice also shows all IMAP network calls",
        aliases: ["-v"],
        repeatable: true
      )
    end
  end
end

Instance Method Details

#account(config, email) ⇒ Object



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

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

  
end

#load_config(**options) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/imap/backup/cli/helpers.rb', line 82

def load_config(**options)
  path = options[:config]
  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

#optionsObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/imap/backup/cli/helpers.rb', line 66

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) ⇒ Object



102
103
104
105
106
107
108
109
# File 'lib/imap/backup/cli/helpers.rb', line 102

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