CLI: Add commands to handle attachments

* Add commands to manipulate entry attachments from the CLI
* Closes #4462

* Add the following commands:
  attachment-export: Exports the content of an attachment to a specified file.

  attachment-import: Imports the attachment into an entry. An existing attachment with the same name may be overwritten if the -f option is specified.

  attachment-rm: Removes the named attachment from an entry.

* Add --show-attachments  to the show command
This commit is contained in:
Andre Blanke 2021-11-03 23:29:44 -04:00 committed by Jonathan White
parent 7811f10dba
commit 7d37f65ad0
16 changed files with 697 additions and 3 deletions

View file

@ -19,6 +19,7 @@
#include "Utils.h"
#include "core/Group.h"
#include "core/Tools.h"
#include <QCommandLineParser>
@ -31,6 +32,9 @@ const QCommandLineOption Show::ProtectedAttributesOption =
<< "show-protected",
QObject::tr("Show the protected attributes in clear text."));
const QCommandLineOption Show::AttachmentsOption =
QCommandLineOption(QStringList() << "show-attachments", QObject::tr("Show the attachments of the entry."));
const QCommandLineOption Show::AttributesOption = QCommandLineOption(
QStringList() << "a"
<< "attributes",
@ -47,6 +51,7 @@ Show::Show()
options.append(Show::TotpOption);
options.append(Show::AttributesOption);
options.append(Show::ProtectedAttributesOption);
options.append(Show::AttachmentsOption);
positionalArguments.append({QString("entry"), QObject::tr("Name of the entry to show."), QString("")});
}
@ -104,6 +109,25 @@ int Show::executeWithDatabase(QSharedPointer<Database> database, QSharedPointer<
}
}
if (parser->isSet(Show::AttachmentsOption)) {
// Separate attachment output from attributes output via a newline.
out << endl;
EntryAttachments* attachments = entry->attachments();
if (attachments->isEmpty()) {
out << QObject::tr("No attachments present.") << endl;
} else {
out << QObject::tr("Attachments:") << endl;
// Iterate over the attachments and output their names and size line-by-line, indented.
for (const QString& attachmentName : attachments->keys()) {
// TODO: use QLocale::formattedDataSize when >= Qt 5.10
QString attachmentSize = Tools::humanReadableFileSize(attachments->value(attachmentName).size(), 1);
out << " " << attachmentName << " (" << attachmentSize << ")" << endl;
}
}
}
if (showTotp) {
out << entry->totp() << endl;
}