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

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

Constant Summary collapse

MAX_SUBJECT =
60

Instance Method Summary collapse

Methods included from Helpers

#account, #connection, #each_connection, included, #load_config, #options

Instance Method Details

#accountsObject



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/imap/backup/cli/local.rb', line 11

def accounts
  config = load_config(**options)
  names = config.accounts.map(&:username)
  case options[:format]
  when "json"
    list = names.map { |n| {username: n} }
    Kernel.puts list.to_json
  else
    names.each { |n| Kernel.puts n }
  end
end

#checkObject



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

def check
  config = load_config(**options)
  results = each_connection(config, emails).map do |connection|
    folders = connection.local_folders
    folder_results = folders.map do |serializer|
      serializer.check_integrity!
      {name: serializer.folder, result: "OK"}
    rescue Serializer::FolderIntegrityError => e
      message = e.to_s
      if options[:delete_corrupt]
        serializer.delete
        message << " and has been deleted"
      end

      {
        name: serializer.folder,
        result: message
      }
    end
    {account: connection..username, folders: folder_results}
  end

  case options[:format]
  when "json"
    print_check_results_as_json(results)
  else
    print_check_results_as_text(results)
  end
end

#folders(email) ⇒ Object



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

def folders(email)
  config = load_config(**options)
  connection = connection(config, email)

  folders = connection.local_folders
  case options[:format]
  when "json"
    list = folders.map { |_s, f| {name: f.name} }
    Kernel.puts list.to_json
  else
    folders.each do |_s, f|
      Kernel.puts %("#{f.name}")
    end
  end
end

#list(email, folder_name) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/imap/backup/cli/local.rb', line 86

def list(email, folder_name)
  config = load_config(**options)
  connection = connection(config, email)

  serializer, _folder = connection.local_folders.find do |(_s, f)|
    f.name == folder_name
  end
  raise "Folder '#{folder_name}' not found" if !serializer

  case options[:format]
  when "json"
    list_emails_as_json serializer
  else
    list_emails_as_text serializer
  end
end

#show(email, folder_name, uids) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/imap/backup/cli/local.rb', line 111

def show(email, folder_name, uids)
  config = load_config(**options)
  connection = connection(config, email)

  serializer, _folder = connection.local_folders.find do |(_s, f)|
    f.name == folder_name
  end
  raise "Folder '#{folder_name}' not found" if !serializer

  uid_list = uids.split(",")

  case options[:format]
  when "json"
    show_emails_as_json serializer, uid_list
  else
    show_emails_as_text serializer, uid_list
  end
end