Skip to content

Commit

Permalink
Global const for KeyFileOption (CLI) (keepassxreboot#2505)
Browse files Browse the repository at this point in the history
  • Loading branch information
louib authored and droidmonkey committed Nov 28, 2018
1 parent 1a27215 commit 49b82ea
Show file tree
Hide file tree
Showing 11 changed files with 34 additions and 55 deletions.
8 changes: 2 additions & 6 deletions src/cli/Add.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,7 @@ int Add::execute(const QStringList& arguments)
parser.setApplicationDescription(description);
parser.addPositionalArgument("database", QObject::tr("Path of the database."));
parser.addOption(Command::QuietOption);

QCommandLineOption keyFile(QStringList() << "k" << "key-file",
QObject::tr("Key file of the database."),
QObject::tr("path"));
parser.addOption(keyFile);
parser.addOption(Command::KeyFileOption);

QCommandLineOption username(QStringList() << "u" << "username",
QObject::tr("Username for the entry."),
Expand Down Expand Up @@ -91,7 +87,7 @@ int Add::execute(const QStringList& arguments)
const QString& entryPath = args.at(1);

auto db = Database::unlockFromStdin(databasePath,
parser.value(keyFile),
parser.value(Command::KeyFileOption),
parser.isSet(Command::QuietOption) ? Utils::DEVNULL : Utils::STDOUT,
Utils::STDERR);
if (!db) {
Expand Down
8 changes: 3 additions & 5 deletions src/cli/Clip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,9 @@ int Clip::execute(const QStringList& arguments)
QCommandLineParser parser;
parser.setApplicationDescription(description);
parser.addPositionalArgument("database", QObject::tr("Path of the database."));
QCommandLineOption keyFile(QStringList() << "k" << "key-file",
QObject::tr("Key file of the database."),
QObject::tr("path"));
parser.addOption(keyFile);
parser.addOption(Command::QuietOption);
parser.addOption(Command::KeyFileOption);

QCommandLineOption totp(QStringList() << "t" << "totp",
QObject::tr("Copy the current TOTP to the clipboard."));
parser.addOption(totp);
Expand All @@ -68,7 +66,7 @@ int Clip::execute(const QStringList& arguments)
}

auto db = Database::unlockFromStdin(args.at(0),
parser.value(keyFile),
parser.value(Command::KeyFileOption),
parser.isSet(Command::QuietOption) ? Utils::DEVNULL : Utils::STDOUT,
Utils::STDERR);
if (!db) {
Expand Down
7 changes: 7 additions & 0 deletions src/cli/Command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ const QCommandLineOption Command::QuietOption =
QCommandLineOption(QStringList() << "q"
<< "quiet",
QObject::tr("Silence password prompt and other secondary outputs."));

const QCommandLineOption Command::KeyFileOption =
QCommandLineOption(QStringList() << "k"
<< "key-file",
QObject::tr("Key file of the database."),
QObject::tr("path"));

QMap<QString, Command*> commands;

Command::~Command()
Expand Down
1 change: 1 addition & 0 deletions src/cli/Command.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class Command
static Command* getCommand(const QString& commandName);

static const QCommandLineOption QuietOption;
static const QCommandLineOption KeyFileOption;
};

#endif // KEEPASSXC_COMMAND_H
8 changes: 2 additions & 6 deletions src/cli/Edit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,7 @@ int Edit::execute(const QStringList& arguments)
parser.setApplicationDescription(description);
parser.addPositionalArgument("database", QObject::tr("Path of the database."));
parser.addOption(Command::QuietOption);

QCommandLineOption keyFile(QStringList() << "k" << "key-file",
QObject::tr("Key file of the database."),
QObject::tr("path"));
parser.addOption(keyFile);
parser.addOption(Command::KeyFileOption);

QCommandLineOption username(QStringList() << "u" << "username",
QObject::tr("Username for the entry."),
Expand Down Expand Up @@ -95,7 +91,7 @@ int Edit::execute(const QStringList& arguments)
const QString& entryPath = args.at(1);

auto db = Database::unlockFromStdin(databasePath,
parser.value(keyFile),
parser.value(Command::KeyFileOption),
parser.isSet(Command::QuietOption) ? Utils::DEVNULL : Utils::STDOUT,
Utils::STDERR);
if (!db) {
Expand Down
7 changes: 2 additions & 5 deletions src/cli/Extract.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,7 @@ int Extract::execute(const QStringList& arguments)
parser.setApplicationDescription(description);
parser.addPositionalArgument("database", QObject::tr("Path of the database to extract."));
parser.addOption(Command::QuietOption);
QCommandLineOption keyFile(QStringList() << "k" << "key-file",
QObject::tr("Key file of the database."),
QObject::tr("path"));
parser.addOption(keyFile);
parser.addOption(Command::KeyFileOption);
parser.addHelpOption();
parser.process(arguments);

Expand All @@ -74,7 +71,7 @@ int Extract::execute(const QStringList& arguments)
passwordKey->setPassword(line);
compositeKey->addKey(passwordKey);

QString keyFilePath = parser.value(keyFile);
QString keyFilePath = parser.value(Command::KeyFileOption);
if (!keyFilePath.isEmpty()) {
// LCOV_EXCL_START
auto fileKey = QSharedPointer<FileKey>::create();
Expand Down
8 changes: 3 additions & 5 deletions src/cli/List.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,8 @@ int List::execute(const QStringList& arguments)
parser.addPositionalArgument("database", QObject::tr("Path of the database."));
parser.addPositionalArgument("group", QObject::tr("Path of the group to list. Default is /"), "[group]");
parser.addOption(Command::QuietOption);
QCommandLineOption keyFile(QStringList() << "k" << "key-file",
QObject::tr("Key file of the database."),
QObject::tr("path"));
parser.addOption(keyFile);
parser.addOption(Command::KeyFileOption);

QCommandLineOption recursiveOption(QStringList() << "R" << "recursive",
QObject::tr("Recursively list the elements of the group."));
parser.addOption(recursiveOption);
Expand All @@ -66,7 +64,7 @@ int List::execute(const QStringList& arguments)
bool recursive = parser.isSet(recursiveOption);

auto db = Database::unlockFromStdin(args.at(0),
parser.value(keyFile),
parser.value(Command::KeyFileOption),
parser.isSet(Command::QuietOption) ? Utils::DEVNULL : Utils::STDOUT,
Utils::STDERR);
if (!db) {
Expand Down
7 changes: 2 additions & 5 deletions src/cli/Locate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,7 @@ int Locate::execute(const QStringList& arguments)
parser.addPositionalArgument("database", QObject::tr("Path of the database."));
parser.addPositionalArgument("term", QObject::tr("Search term."));
parser.addOption(Command::QuietOption);
QCommandLineOption keyFile(QStringList() << "k" << "key-file",
QObject::tr("Key file of the database."),
QObject::tr("path"));
parser.addOption(keyFile);
parser.addOption(Command::KeyFileOption);
parser.addHelpOption();
parser.process(arguments);

Expand All @@ -63,7 +60,7 @@ int Locate::execute(const QStringList& arguments)
}

auto db = Database::unlockFromStdin(args.at(0),
parser.value(keyFile),
parser.value(Command::KeyFileOption),
parser.isSet(Command::QuietOption) ? Utils::DEVNULL : Utils::STDOUT,
Utils::STDERR);
if (!db) {
Expand Down
21 changes: 8 additions & 13 deletions src/cli/Merge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,12 @@ int Merge::execute(const QStringList& arguments)
QCommandLineOption samePasswordOption(QStringList() << "s" << "same-credentials",
QObject::tr("Use the same credentials for both database files."));
parser.addOption(samePasswordOption);
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(Command::KeyFileOption);

QCommandLineOption keyFileFromOption(QStringList() << "f" << "key-file-from",
QObject::tr("Key file of the database to merge from."),
QObject::tr("path"));
parser.addOption(keyFileFromOption);

parser.addHelpOption();
parser.process(arguments);
Expand All @@ -69,7 +67,7 @@ int Merge::execute(const QStringList& arguments)
}

auto db1 = Database::unlockFromStdin(args.at(0),
parser.value(keyFile),
parser.value(Command::KeyFileOption),
parser.isSet(Command::QuietOption) ? Utils::DEVNULL : Utils::STDOUT,
Utils::STDERR);
if (!db1) {
Expand All @@ -78,10 +76,7 @@ int Merge::execute(const QStringList& arguments)

QSharedPointer<Database> db2;
if (!parser.isSet("same-credentials")) {
db2 = Database::unlockFromStdin(args.at(1),
parser.value(keyFileFrom),
parser.isSet(Command::QuietOption) ? Utils::DEVNULL : Utils::STDOUT,
Utils::STDERR);
db2 = Database::unlockFromStdin(args.at(1), parser.value(keyFileFromOption), Utils::STDOUT, Utils::STDERR);
} else {
db2 = QSharedPointer<Database>::create();
QString errorMessage;
Expand Down
7 changes: 2 additions & 5 deletions src/cli/Remove.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,7 @@ int Remove::execute(const QStringList& arguments)
parser.setApplicationDescription(QCoreApplication::tr("main", "Remove an entry from the database."));
parser.addPositionalArgument("database", QCoreApplication::tr("main", "Path of the database."));
parser.addOption(Command::QuietOption);
QCommandLineOption keyFile(QStringList() << "k" << "key-file",
QObject::tr("Key file of the database."),
QObject::tr("path"));
parser.addOption(keyFile);
parser.addOption(Command::KeyFileOption);
parser.addPositionalArgument("entry", QCoreApplication::tr("main", "Path of the entry to remove."));
parser.addHelpOption();
parser.process(arguments);
Expand All @@ -65,7 +62,7 @@ int Remove::execute(const QStringList& arguments)
}

auto db = Database::unlockFromStdin(args.at(0),
parser.value(keyFile),
parser.value(Command::KeyFileOption),
parser.isSet(Command::QuietOption) ? Utils::DEVNULL : Utils::STDOUT,
Utils::STDERR);
if (!db) {
Expand Down
7 changes: 2 additions & 5 deletions src/cli/Show.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,8 @@ int Show::execute(const QStringList& arguments)
QCommandLineParser parser;
parser.setApplicationDescription(description);
parser.addPositionalArgument("database", QObject::tr("Path of the database."));
QCommandLineOption keyFile(QStringList() << "k" << "key-file",
QObject::tr("Key file of the database."),
QObject::tr("path"));
parser.addOption(keyFile);
parser.addOption(Command::QuietOption);
parser.addOption(Command::KeyFileOption);
QCommandLineOption totp(QStringList() << "t" << "totp",
QObject::tr("Show the entry's current TOTP."));
parser.addOption(totp);
Expand All @@ -73,7 +70,7 @@ int Show::execute(const QStringList& arguments)
}

auto db = Database::unlockFromStdin(args.at(0),
parser.value(keyFile),
parser.value(Command::KeyFileOption),
parser.isSet(Command::QuietOption) ? Utils::DEVNULL : Utils::STDOUT,
Utils::STDERR);
if (!db) {
Expand Down

0 comments on commit 49b82ea

Please sign in to comment.