Adding --quiet option to the CLI. (#2507)

This commit is contained in:
louib 2018-11-28 11:24:12 -05:00 committed by Jonathan White
parent 4e49de1afb
commit fff0f11b33
21 changed files with 252 additions and 44 deletions

View file

@ -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."),
@ -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;
}
@ -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;
@ -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;
}

View file

@ -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);
@ -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);
@ -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;

View file

@ -26,7 +26,11 @@ public:
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

View file

@ -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()

View file

@ -18,6 +18,7 @@
#ifndef KEEPASSXC_COMMAND_H
#define KEEPASSXC_COMMAND_H
#include <QCommandLineOption>
#include <QList>
#include <QObject>
#include <QString>
@ -36,6 +37,8 @@ public:
static QList<Command*> getCommands();
static Command* getCommand(const QString& commandName);
static const QCommandLineOption QuietOption;
};
#endif // KEEPASSXC_COMMAND_H

View file

@ -22,9 +22,9 @@
#include <QCommandLineParser>
#include "Utils.h"
#include "cli/TextStream.h"
#include "core/PassphraseGenerator.h"
#include "Utils.h"
Diceware::Diceware()
{

View file

@ -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."),
@ -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;
}
@ -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;
@ -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;
}

View file

@ -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"));
@ -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);

View file

@ -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"));
@ -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;
}

View file

@ -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()
@ -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"));
@ -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;
}

View file

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

View file

@ -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"));
@ -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);

View file

@ -28,7 +28,7 @@ public:
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

View file

@ -22,12 +22,12 @@
#include <QCommandLineParser>
#include "Utils.h"
#include "cli/TextStream.h"
#include "core/Database.h"
#include "core/Entry.h"
#include "core/Group.h"
#include "core/Global.h"
#include "Utils.h"
#include "core/Group.h"
Show::Show()
{
@ -50,6 +50,7 @@ int Show::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("Show the entry's current TOTP."));
parser.addOption(totp);
@ -71,7 +72,10 @@ int Show::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;
}

View file

@ -47,4 +47,4 @@ private:
void detectCodec();
};
#endif //KEEPASSXC_TEXTSTREAM_H
#endif // KEEPASSXC_TEXTSTREAM_H

View file

@ -43,6 +43,16 @@ FILE* STDERR = stderr;
*/
FILE* STDIN = stdin;
/**
* DEVNULL file handle for the CLI.
*/
#ifdef Q_OS_WIN
FILE* DEVNULL = fopen("nul", "w");
#else
FILE* DEVNULL = fopen("/dev/null", "w");
#endif
void setStdinEcho(bool enable = true)
{
#ifdef Q_OS_WIN
@ -95,9 +105,9 @@ void setNextPassword(const QString& password)
*
* @return the password
*/
QString getPassword()
QString getPassword(FILE* outputDescriptor)
{
TextStream out(STDOUT, QIODevice::WriteOnly);
TextStream out(outputDescriptor, QIODevice::WriteOnly);
// return preset password if one is set
if (!Test::nextPasswords.isEmpty()) {

View file

@ -26,9 +26,10 @@ namespace Utils
extern FILE* STDOUT;
extern FILE* STDERR;
extern FILE* STDIN;
extern FILE* DEVNULL;
void setStdinEcho(bool enable);
QString getPassword();
QString getPassword(FILE* outputDescriptor = STDOUT);
int clipText(const QString& text);
namespace Test

View file

@ -56,6 +56,9 @@ Shows the title, username, password, URL and notes of a database entry. Can also
.IP "-k, --key-file <path>"
Specifies a path to a key file for unlocking the database. In a merge operation this option is used to specify the key file path for the first database.
.IP "-q, --quiet <path>"
Silence password prompt and other secondary outputs.
.IP "-h, --help"
Displays help information.