lib/imap/backup/cli/local.rb in imap-backup-9.2.0 vs lib/imap/backup/cli/local.rb in imap-backup-9.3.0
- old
+ new
@@ -18,10 +18,51 @@
else
names.each { |n| Kernel.puts n }
end
end
+ desc(
+ "check",
+ "Check the integrity of backups for all accounts (or the selected account(s))"
+ )
+ method_option(
+ "delete_corrupt",
+ type: :boolean,
+ desc: "deletes any corrupted folders - USE WITH CAUTION!"
+ )
+ config_option
+ format_option
+ 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.account.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
+
desc "folders EMAIL", "List backed up folders"
config_option
format_option
def folders(email)
config = load_config(**options)
@@ -85,10 +126,23 @@
show_emails_as_text serializer, uid_list
end
end
no_commands do
+ def print_check_results_as_json(results)
+ Kernel.puts results.to_json
+ end
+
+ def print_check_results_as_text(results)
+ results.each do |account_results|
+ Kernel.puts "Account: #{account_results[:account]}"
+ account_results[:folders].each do |folder_results|
+ Kernel.puts "\t#{folder_results[:name]}: #{folder_results[:result]}"
+ end
+ end
+ end
+
def list_emails_as_json(serializer)
emails = serializer.each_message.map do |message|
{
uid: message.uid,
date: message.date.to_s,
@@ -139,9 +193,13 @@
#{'-' * 80}
HEADER
end
Kernel.puts message.body
end
+ end
+
+ def emails
+ (options[:accounts] || "").split(",")
end
end
end
end