Skip to content

Commit

Permalink
Adding --quiet option to the CLI. (#2507)
Browse files Browse the repository at this point in the history
  • Loading branch information
louib authored and droidmonkey committed Nov 28, 2018
1 parent 4e49de1 commit fff0f11
Show file tree
Hide file tree
Showing 21 changed files with 252 additions and 44 deletions.
16 changes: 12 additions & 4 deletions src/cli/Add.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ int Add::execute(const QStringList& arguments)
QCommandLineParser parser;
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."),
Expand Down Expand Up @@ -89,7 +90,10 @@ int Add::execute(const QStringList& arguments)
const QString& databasePath = args.at(0);
const QString& entryPath = args.at(1);

auto db = Database::unlockFromStdin(databasePath, parser.value(keyFile), Utils::STDOUT, Utils::STDERR);
auto db = Database::unlockFromStdin(databasePath,
parser.value(keyFile),
parser.isSet(Command::QuietOption) ? Utils::DEVNULL : Utils::STDOUT,
Utils::STDERR);
if (!db) {
return EXIT_FAILURE;
}
Expand Down Expand Up @@ -117,8 +121,10 @@ int Add::execute(const QStringList& arguments)
}

if (parser.isSet(prompt)) {
outputTextStream << QObject::tr("Enter password for new entry: ") << flush;
QString password = Utils::getPassword();
if (!parser.isSet(Command::QuietOption)) {
outputTextStream << QObject::tr("Enter password for new entry: ") << flush;
}
QString password = Utils::getPassword(parser.isSet(Command::QuietOption) ? Utils::DEVNULL : Utils::STDOUT);
entry->setPassword(password);
} else if (parser.isSet(generate)) {
PasswordGenerator passwordGenerator;
Expand All @@ -141,6 +147,8 @@ int Add::execute(const QStringList& arguments)
return EXIT_FAILURE;
}

outputTextStream << QObject::tr("Successfully added entry %1.").arg(entry->title()) << endl;
if (!parser.isSet(Command::QuietOption)) {
outputTextStream << QObject::tr("Successfully added entry %1.").arg(entry->title()) << endl;
}
return EXIT_SUCCESS;
}
16 changes: 12 additions & 4 deletions src/cli/Clip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ int Clip::execute(const QStringList& arguments)
QObject::tr("Key file of the database."),
QObject::tr("path"));
parser.addOption(keyFile);
parser.addOption(Command::QuietOption);
QCommandLineOption totp(QStringList() << "t" << "totp",
QObject::tr("Copy the current TOTP to the clipboard."));
parser.addOption(totp);
Expand All @@ -66,15 +67,22 @@ int Clip::execute(const QStringList& arguments)
return EXIT_FAILURE;
}

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

return clipEntry(db, args.at(1), args.value(2), parser.isSet(totp));
return clipEntry(db, args.at(1), args.value(2), parser.isSet(totp), parser.isSet(Command::QuietOption));
}

int Clip::clipEntry(QSharedPointer<Database> database, const QString& entryPath, const QString& timeout, bool clipTotp)
int Clip::clipEntry(QSharedPointer<Database> database,
const QString& entryPath,
const QString& timeout,
bool clipTotp,
bool silent)
{
TextStream err(Utils::STDERR);

Expand All @@ -86,7 +94,7 @@ int Clip::clipEntry(QSharedPointer<Database> database, const QString& entryPath,
timeoutSeconds = timeout.toInt();
}

TextStream outputTextStream(Utils::STDOUT, QIODevice::WriteOnly);
TextStream outputTextStream(silent ? Utils::DEVNULL : Utils::STDOUT, QIODevice::WriteOnly);
Entry* entry = database->rootGroup()->findEntryByPath(entryPath);
if (!entry) {
err << QObject::tr("Entry %1 not found.").arg(entryPath) << endl;
Expand Down
6 changes: 5 additions & 1 deletion src/cli/Clip.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ class Clip : public Command
Clip();
~Clip();
int execute(const QStringList& arguments) override;
int clipEntry(QSharedPointer<Database> database, const QString& entryPath, const QString& timeout, bool clipTotp);
int clipEntry(QSharedPointer<Database> database,
const QString& entryPath,
const QString& timeout,
bool clipTotp,
bool silent);
};

#endif // KEEPASSXC_CLIP_H
4 changes: 4 additions & 0 deletions src/cli/Command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
#include "Remove.h"
#include "Show.h"

const QCommandLineOption Command::QuietOption =
QCommandLineOption(QStringList() << "q"
<< "quiet",
QObject::tr("Silence password prompt and other secondary outputs."));
QMap<QString, Command*> commands;

Command::~Command()
Expand Down
3 changes: 3 additions & 0 deletions src/cli/Command.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#ifndef KEEPASSXC_COMMAND_H
#define KEEPASSXC_COMMAND_H

#include <QCommandLineOption>
#include <QList>
#include <QObject>
#include <QString>
Expand All @@ -36,6 +37,8 @@ class Command

static QList<Command*> getCommands();
static Command* getCommand(const QString& commandName);

static const QCommandLineOption QuietOption;
};

#endif // KEEPASSXC_COMMAND_H
2 changes: 1 addition & 1 deletion src/cli/Diceware.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@

#include <QCommandLineParser>

#include "Utils.h"
#include "cli/TextStream.h"
#include "core/PassphraseGenerator.h"
#include "Utils.h"

Diceware::Diceware()
{
Expand Down
16 changes: 12 additions & 4 deletions src/cli/Edit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ int Edit::execute(const QStringList& arguments)
QCommandLineParser parser;
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."),
Expand Down Expand Up @@ -93,7 +94,10 @@ int Edit::execute(const QStringList& arguments)
const QString& databasePath = args.at(0);
const QString& entryPath = args.at(1);

auto db = Database::unlockFromStdin(databasePath, parser.value(keyFile), Utils::STDOUT, Utils::STDERR);
auto db = Database::unlockFromStdin(databasePath,
parser.value(keyFile),
parser.isSet(Command::QuietOption) ? Utils::DEVNULL : Utils::STDOUT,
Utils::STDERR);
if (!db) {
return EXIT_FAILURE;
}
Expand Down Expand Up @@ -132,8 +136,10 @@ int Edit::execute(const QStringList& arguments)
}

if (parser.isSet(prompt)) {
out << QObject::tr("Enter new password for entry: ") << flush;
QString password = Utils::getPassword();
if (!parser.isSet(Command::QuietOption)) {
out << QObject::tr("Enter new password for entry: ") << flush;
}
QString password = Utils::getPassword(parser.isSet(Command::QuietOption) ? Utils::DEVNULL : Utils::STDOUT);
entry->setPassword(password);
} else if (parser.isSet(generate)) {
PasswordGenerator passwordGenerator;
Expand All @@ -158,6 +164,8 @@ int Edit::execute(const QStringList& arguments)
return EXIT_FAILURE;
}

out << QObject::tr("Successfully edited entry %1.").arg(entry->title()) << endl;
if (!parser.isSet(Command::QuietOption)) {
out << QObject::tr("Successfully edited entry %1.").arg(entry->title()) << endl;
}
return EXIT_SUCCESS;
}
7 changes: 5 additions & 2 deletions src/cli/Extract.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ int Extract::execute(const QStringList& arguments)
QCommandLineParser parser;
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"));
Expand All @@ -62,11 +63,13 @@ int Extract::execute(const QStringList& arguments)
return EXIT_FAILURE;
}

out << QObject::tr("Insert password to unlock %1: ").arg(args.at(0)) << flush;
if (!parser.isSet(Command::QuietOption)) {
out << QObject::tr("Insert password to unlock %1: ").arg(args.at(0)) << flush;
}

auto compositeKey = QSharedPointer<CompositeKey>::create();

QString line = Utils::getPassword();
QString line = Utils::getPassword(parser.isSet(Command::QuietOption) ? Utils::DEVNULL : Utils::STDOUT);
auto passwordKey = QSharedPointer<PasswordKey>::create();
passwordKey->setPassword(line);
compositeKey->addKey(passwordKey);
Expand Down
6 changes: 5 additions & 1 deletion src/cli/List.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ int List::execute(const QStringList& arguments)
parser.setApplicationDescription(description);
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"));
Expand All @@ -64,7 +65,10 @@ int List::execute(const QStringList& arguments)

bool recursive = parser.isSet(recursiveOption);

auto db = Database::unlockFromStdin(args.at(0), parser.value(keyFile), Utils::STDOUT, Utils::STDERR);
auto db = Database::unlockFromStdin(args.at(0),
parser.value(keyFile),
parser.isSet(Command::QuietOption) ? Utils::DEVNULL : Utils::STDOUT,
Utils::STDERR);
if (!db) {
return EXIT_FAILURE;
}
Expand Down
8 changes: 6 additions & 2 deletions src/cli/Locate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@

#include "cli/TextStream.h"
#include "cli/Utils.h"
#include "core/Global.h"
#include "core/Database.h"
#include "core/Entry.h"
#include "core/Global.h"
#include "core/Group.h"

Locate::Locate()
Expand All @@ -48,6 +48,7 @@ int Locate::execute(const QStringList& arguments)
parser.setApplicationDescription(description);
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"));
Expand All @@ -61,7 +62,10 @@ int Locate::execute(const QStringList& arguments)
return EXIT_FAILURE;
}

auto db = Database::unlockFromStdin(args.at(0), parser.value(keyFile), Utils::STDOUT, Utils::STDERR);
auto db = Database::unlockFromStdin(args.at(0),
parser.value(keyFile),
parser.isSet(Command::QuietOption) ? Utils::DEVNULL : Utils::STDOUT,
Utils::STDERR);
if (!db) {
return EXIT_FAILURE;
}
Expand Down
22 changes: 15 additions & 7 deletions src/cli/Merge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
#include <QCommandLineParser>

#include "cli/TextStream.h"
#include "cli/Utils.h"
#include "core/Database.h"
#include "core/Merger.h"
#include "cli/Utils.h"

#include <cstdlib>

Expand All @@ -45,10 +45,11 @@ int Merge::execute(const QStringList& arguments)
parser.setApplicationDescription(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."));
parser.addOption(Command::QuietOption);

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"));
Expand All @@ -58,7 +59,6 @@ int Merge::execute(const QStringList& arguments)
QObject::tr("path"));
parser.addOption(keyFileFrom);

parser.addOption(samePasswordOption);
parser.addHelpOption();
parser.process(arguments);

Expand All @@ -68,14 +68,20 @@ int Merge::execute(const QStringList& arguments)
return EXIT_FAILURE;
}

auto db1 = Database::unlockFromStdin(args.at(0), parser.value(keyFile), Utils::STDOUT, Utils::STDERR);
auto db1 = Database::unlockFromStdin(args.at(0),
parser.value(keyFile),
parser.isSet(Command::QuietOption) ? Utils::DEVNULL : Utils::STDOUT,
Utils::STDERR);
if (!db1) {
return EXIT_FAILURE;
}

QSharedPointer<Database> db2;
if (!parser.isSet("same-credentials")) {
db2 = Database::unlockFromStdin(args.at(1), parser.value(keyFileFrom), Utils::STDOUT, Utils::STDERR);
db2 = Database::unlockFromStdin(args.at(1),
parser.value(keyFileFrom),
parser.isSet(Command::QuietOption) ? Utils::DEVNULL : Utils::STDOUT,
Utils::STDERR);
} else {
db2 = QSharedPointer<Database>::create();
QString errorMessage;
Expand All @@ -94,8 +100,10 @@ int Merge::execute(const QStringList& arguments)
err << QObject::tr("Unable to save database to file : %1").arg(errorMessage) << endl;
return EXIT_FAILURE;
}
out << "Successfully merged the database files." << endl;
} else {
if (!parser.isSet(Command::QuietOption)) {
out << "Successfully merged the database files." << endl;
}
} else if (!parser.isSet(Command::QuietOption)) {
out << "Database was not modified by merge operation." << endl;
}

Expand Down
12 changes: 8 additions & 4 deletions src/cli/Remove.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ int Remove::execute(const QStringList& arguments)
QCommandLineParser parser;
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"));
Expand All @@ -63,17 +64,20 @@ int Remove::execute(const QStringList& arguments)
return EXIT_FAILURE;
}

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

return removeEntry(db.data(), args.at(0), args.at(1));
return removeEntry(db.data(), args.at(0), args.at(1), parser.isSet(Command::QuietOption));
}

int Remove::removeEntry(Database* database, const QString& databasePath, const QString& entryPath)
int Remove::removeEntry(Database* database, const QString& databasePath, const QString& entryPath, bool quiet)
{
TextStream out(Utils::STDOUT, QIODevice::WriteOnly);
TextStream out(quiet ? Utils::DEVNULL : Utils::STDOUT, QIODevice::WriteOnly);
TextStream err(Utils::STDERR, QIODevice::WriteOnly);

QPointer<Entry> entry = database->rootGroup()->findEntryByPath(entryPath);
Expand Down
2 changes: 1 addition & 1 deletion src/cli/Remove.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Remove : public Command
Remove();
~Remove();
int execute(const QStringList& arguments) override;
int removeEntry(Database* database, const QString& databasePath, const QString& entryPath);
int removeEntry(Database* database, const QString& databasePath, const QString& entryPath, bool quiet);
};

#endif // KEEPASSXC_REMOVE_H
Loading

0 comments on commit fff0f11

Please sign in to comment.