diff options
author | Louis-Bertrand Varin <louisv@unito.io> | 2017-01-12 05:00:11 +0300 |
---|---|---|
committer | Louis-Bertrand Varin <louisv@unito.io> | 2017-01-12 05:00:11 +0300 |
commit | a79366f1059e55500614d94178d7d84603e962ac (patch) | |
tree | 565eaeb27c7318188aa3dd5c7053c923ee875ce1 /utils | |
parent | 2afa1f0dc8208709820655795f3e1cd5617eb36c (diff) |
Use QCommandLineParser
* Switch to using QCommandLineParser
* Implement the --same-password option
* extract `getKeyFromLine` function
Diffstat (limited to 'utils')
-rw-r--r-- | utils/merge-databases.cpp | 69 |
1 files changed, 46 insertions, 23 deletions
diff --git a/utils/merge-databases.cpp b/utils/merge-databases.cpp index 52a04e42a..d67a87672 100644 --- a/utils/merge-databases.cpp +++ b/utils/merge-databases.cpp @@ -17,6 +17,7 @@ #include <stdio.h> +#include <QCommandLineParser> #include <QCoreApplication> #include <QFile> #include <QSaveFile> @@ -31,13 +32,49 @@ #include "keys/FileKey.h" #include "keys/PasswordKey.h" +/* + * Read a key from a line of input. + * If the line references a valid file + * path, the key is loaded from file. + */ +CompositeKey readKeyFromLine(QString line) +{ + + CompositeKey key; + if (QFile::exists(line)) { + FileKey fileKey; + fileKey.load(line); + key.addKey(fileKey); + } + else { + PasswordKey password; + password.setPassword(line); + key.addKey(password); + } + return key; + +} int main(int argc, char **argv) { + QCoreApplication app(argc, argv); - if (app.arguments().size() != 3) { - qCritical("Usage: merge-databases <kdbx file1> <kdbx file2>"); + QCommandLineParser parser; + parser.setApplicationDescription(QCoreApplication::translate("main", "Merge 2 KeePassXC database files.")); + 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.")); + + parser.addHelpOption(); + parser.addOption(samePasswordOption); + parser.process(app); + + const QStringList args = parser.positionalArguments(); + if (args.size() != 2) { + parser.showHelp(); return 1; } @@ -48,33 +85,19 @@ int main(int argc, char **argv) static QTextStream inputTextStream(stdin, QIODevice::ReadOnly); QString line1 = inputTextStream.readLine(); - CompositeKey key1; - if (QFile::exists(line1)) { - FileKey fileKey; - fileKey.load(line1); - key1.addKey(fileKey); - } - else { - PasswordKey password; - password.setPassword(line1); - key1.addKey(password); - } + CompositeKey key1 = readKeyFromLine(line1); - QString line2 = inputTextStream.readLine(); CompositeKey key2; - if (QFile::exists(line2)) { - FileKey fileKey; - fileKey.load(line2); - key2.addKey(fileKey); + if (parser.isSet("same-password")) { + key2 = *key1.clone(); } else { - PasswordKey password; - password.setPassword(line2); - key2.addKey(password); + QString line2 = inputTextStream.readLine(); + key2 = readKeyFromLine(line2); } - QString databaseFilename1 = app.arguments().at(1); + QString databaseFilename1 = args.at(0); QFile dbFile1(databaseFilename1); if (!dbFile1.exists()) { qCritical("File %s does not exist.", qPrintable(databaseFilename1)); @@ -94,7 +117,7 @@ int main(int argc, char **argv) } - QString databaseFilename2 = app.arguments().at(2); + QString databaseFilename2 = args.at(1); QFile dbFile2(databaseFilename2); if (!dbFile2.exists()) { qCritical("File %s does not exist.", qPrintable(databaseFilename2)); |