diff options
Diffstat (limited to 'src/cli/Merge.cpp')
-rw-r--r-- | src/cli/Merge.cpp | 87 |
1 files changed, 39 insertions, 48 deletions
diff --git a/src/cli/Merge.cpp b/src/cli/Merge.cpp index a179d69be..6b114bff3 100644 --- a/src/cli/Merge.cpp +++ b/src/cli/Merge.cpp @@ -19,79 +19,71 @@ #include "Merge.h" -#include <QApplication> #include <QCommandLineParser> -#include <QCoreApplication> -#include <QStringList> #include <QTextStream> #include "core/Database.h" -#include "gui/UnlockDatabaseDialog.h" -int Merge::execute(int argc, char** argv) +Merge::Merge() { + name = QString("merge"); + description = QObject::tr("Merge two databases."); +} - QStringList arguments; - for (int i = 0; i < argc; ++i) { - arguments << QString(argv[i]); - } +Merge::~Merge() +{ +} + +int Merge::execute(const QStringList& arguments) +{ QTextStream out(stdout); QCommandLineParser parser; - parser.setApplicationDescription(QCoreApplication::translate("main", "Merge two databases.")); - parser.addPositionalArgument("database1", - QCoreApplication::translate("main", "Path of the database to merge into.")); - parser.addPositionalArgument("database2", - QCoreApplication::translate("main", "Path of the database to merge from.")); - - QCommandLineOption samePasswordOption( - QStringList() << "s" - << "same-password", - QCoreApplication::translate("main", "Use the same password for both database files.")); - - QCommandLineOption guiPrompt( - QStringList() << "g" - << "gui-prompt", - QCoreApplication::translate("main", "Use a GUI prompt unlocking the database.")); - parser.addOption(guiPrompt); + parser.setApplicationDescription(this->description); + parser.addPositionalArgument("database1", QObject::tr("Path of the database to merge into.")); + parser.addPositionalArgument("database2", QObject::tr("Path of the database to merge from.")); + + QCommandLineOption samePasswordOption(QStringList() << "s" + << "same-credentials", + QObject::tr("Use the same credentials for both database files.")); + + QCommandLineOption keyFile(QStringList() << "k" + << "key-file", + QObject::tr("Key file of the database."), + QObject::tr("path")); + parser.addOption(keyFile); + QCommandLineOption keyFileFrom(QStringList() << "f" + << "key-file-from", + QObject::tr("Key file of the database to merge from."), + QObject::tr("path")); + parser.addOption(keyFileFrom); parser.addOption(samePasswordOption); parser.process(arguments); const QStringList args = parser.positionalArguments(); if (args.size() != 2) { - QCoreApplication app(argc, argv); - parser.showHelp(EXIT_FAILURE); + out << parser.helpText().replace("keepassxc-cli", "keepassxc-cli merge"); + return EXIT_FAILURE; } - Database* db1; - Database* db2; - - if (parser.isSet("gui-prompt")) { - QApplication app(argc, argv); - db1 = UnlockDatabaseDialog::openDatabasePrompt(args.at(0)); - if (!parser.isSet("same-password")) { - db2 = UnlockDatabaseDialog::openDatabasePrompt(args.at(1)); - } else { - db2 = Database::openDatabaseFile(args.at(1), *(db1->key().clone())); - } - } else { - QCoreApplication app(argc, argv); - db1 = Database::unlockFromStdin(args.at(0)); - if (!parser.isSet("same-password")) { - db2 = Database::unlockFromStdin(args.at(1)); - } else { - db2 = Database::openDatabaseFile(args.at(1), *(db1->key().clone())); - } - } + Database* db1 = Database::unlockFromStdin(args.at(0), parser.value(keyFile)); if (db1 == nullptr) { return EXIT_FAILURE; } + + Database* db2; + if (!parser.isSet("same-credentials")) { + db2 = Database::unlockFromStdin(args.at(1), parser.value(keyFileFrom)); + } else { + db2 = Database::openDatabaseFile(args.at(1), *(db1->key().clone())); + } if (db2 == nullptr) { return EXIT_FAILURE; } db1->merge(db2); + QString errorMessage = db1->saveToFile(args.at(0)); if (!errorMessage.isEmpty()) { qCritical("Unable to save database to file : %s", qPrintable(errorMessage)); @@ -100,5 +92,4 @@ int Merge::execute(int argc, char** argv) out << "Successfully merged the database files.\n"; return EXIT_SUCCESS; - } |