Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

We should output to stderr on EXIT_FAILURE #2558

Merged
merged 1 commit into from
Jan 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/cli/Add.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ int Add::execute(const QStringList& arguments)

const QStringList args = parser.positionalArguments();
if (args.size() != 2) {
outputTextStream << parser.helpText().replace("keepassxc-cli", "keepassxc-cli add");
errorTextStream << parser.helpText().replace("keepassxc-cli", "keepassxc-cli add");
return EXIT_FAILURE;
}

Expand Down
12 changes: 6 additions & 6 deletions src/cli/Clip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Clip::~Clip()

int Clip::execute(const QStringList& arguments)
{
TextStream out(Utils::STDOUT);
TextStream errorTextStream(Utils::STDERR, QIODevice::WriteOnly);

QCommandLineParser parser;
parser.setApplicationDescription(description);
Expand All @@ -62,7 +62,7 @@ int Clip::execute(const QStringList& arguments)

const QStringList args = parser.positionalArguments();
if (args.size() != 2 && args.size() != 3) {
out << parser.helpText().replace("keepassxc-cli", "keepassxc-cli clip");
errorTextStream << parser.helpText().replace("keepassxc-cli", "keepassxc-cli clip");
return EXIT_FAILURE;
}

Expand All @@ -83,11 +83,11 @@ int Clip::clipEntry(QSharedPointer<Database> database,
bool clipTotp,
bool silent)
{
TextStream err(Utils::STDERR);
TextStream errorTextStream(Utils::STDERR);

int timeoutSeconds = 0;
if (!timeout.isEmpty() && !timeout.toInt()) {
err << QObject::tr("Invalid timeout value %1.").arg(timeout) << endl;
errorTextStream << QObject::tr("Invalid timeout value %1.").arg(timeout) << endl;
return EXIT_FAILURE;
} else if (!timeout.isEmpty()) {
timeoutSeconds = timeout.toInt();
Expand All @@ -96,14 +96,14 @@ int Clip::clipEntry(QSharedPointer<Database> database,
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;
errorTextStream << QObject::tr("Entry %1 not found.").arg(entryPath) << endl;
return EXIT_FAILURE;
}

QString value;
if (clipTotp) {
if (!entry->hasTotp()) {
err << QObject::tr("Entry with path %1 has no TOTP set up.").arg(entryPath) << endl;
errorTextStream << QObject::tr("Entry with path %1 has no TOTP set up.").arg(entryPath) << endl;
return EXIT_FAILURE;
}

Expand Down
10 changes: 5 additions & 5 deletions src/cli/Diceware.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ Diceware::~Diceware()

int Diceware::execute(const QStringList& arguments)
{
TextStream in(Utils::STDIN, QIODevice::ReadOnly);
TextStream out(Utils::STDOUT, QIODevice::WriteOnly);
TextStream outputTextStream(Utils::STDOUT, QIODevice::WriteOnly);
TextStream errorTextStream(Utils::STDERR, QIODevice::WriteOnly);

QCommandLineParser parser;
parser.setApplicationDescription(description);
Expand All @@ -58,7 +58,7 @@ int Diceware::execute(const QStringList& arguments)

const QStringList args = parser.positionalArguments();
if (!args.isEmpty()) {
out << parser.helpText().replace("keepassxc-cli", "keepassxc-cli diceware");
errorTextStream << parser.helpText().replace("keepassxc-cli", "keepassxc-cli diceware");
return EXIT_FAILURE;
}

Expand All @@ -78,12 +78,12 @@ int Diceware::execute(const QStringList& arguments)
}

if (!dicewareGenerator.isValid()) {
out << parser.helpText().replace("keepassxc-cli", "keepassxc-cli diceware");
outputTextStream << parser.helpText().replace("keepassxc-cli", "keepassxc-cli diceware");
return EXIT_FAILURE;
}

QString password = dicewareGenerator.generatePassphrase();
out << password << endl;
outputTextStream << password << endl;

return EXIT_SUCCESS;
}
19 changes: 9 additions & 10 deletions src/cli/Edit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,8 @@ Edit::~Edit()

int Edit::execute(const QStringList& arguments)
{
TextStream in(Utils::STDIN, QIODevice::ReadOnly);
TextStream out(Utils::STDOUT, QIODevice::WriteOnly);
TextStream err(Utils::STDERR, QIODevice::WriteOnly);
TextStream outputTextStream(Utils::STDOUT, QIODevice::WriteOnly);
TextStream errorTextStream(Utils::STDERR, QIODevice::WriteOnly);

QCommandLineParser parser;
parser.setApplicationDescription(description);
Expand Down Expand Up @@ -88,7 +87,7 @@ int Edit::execute(const QStringList& arguments)

const QStringList args = parser.positionalArguments();
if (args.size() != 2) {
out << parser.helpText().replace("keepassxc-cli", "keepassxc-cli edit");
errorTextStream << parser.helpText().replace("keepassxc-cli", "keepassxc-cli edit");
return EXIT_FAILURE;
}

Expand All @@ -105,19 +104,19 @@ int Edit::execute(const QStringList& arguments)

QString passwordLength = parser.value(length);
if (!passwordLength.isEmpty() && !passwordLength.toInt()) {
err << QObject::tr("Invalid value for password length: %1").arg(passwordLength) << endl;
errorTextStream << QObject::tr("Invalid value for password length: %1").arg(passwordLength) << endl;
return EXIT_FAILURE;
}

Entry* entry = db->rootGroup()->findEntryByPath(entryPath);
if (!entry) {
err << QObject::tr("Could not find entry with path %1.").arg(entryPath) << endl;
errorTextStream << QObject::tr("Could not find entry with path %1.").arg(entryPath) << endl;
return EXIT_FAILURE;
}

if (parser.value("username").isEmpty() && parser.value("url").isEmpty() && parser.value("title").isEmpty()
&& !parser.isSet(prompt) && !parser.isSet(generate)) {
err << QObject::tr("Not changing any field for entry %1.").arg(entryPath) << endl;
errorTextStream << QObject::tr("Not changing any field for entry %1.").arg(entryPath) << endl;
return EXIT_FAILURE;
}

Expand All @@ -137,7 +136,7 @@ int Edit::execute(const QStringList& arguments)

if (parser.isSet(prompt)) {
if (!parser.isSet(Command::QuietOption)) {
out << QObject::tr("Enter new password for entry: ") << flush;
outputTextStream << QObject::tr("Enter new password for entry: ") << flush;
}
QString password = Utils::getPassword(parser.isSet(Command::QuietOption) ? Utils::DEVNULL : Utils::STDOUT);
entry->setPassword(password);
Expand All @@ -160,12 +159,12 @@ int Edit::execute(const QStringList& arguments)

QString errorMessage;
if (!db->save(databasePath, &errorMessage, true, false)) {
err << QObject::tr("Writing the database failed: %1").arg(errorMessage) << endl;
errorTextStream << QObject::tr("Writing the database failed: %1").arg(errorMessage) << endl;
return EXIT_FAILURE;
}

if (!parser.isSet(Command::QuietOption)) {
out << QObject::tr("Successfully edited entry %1.").arg(entry->title()) << endl;
outputTextStream << QObject::tr("Successfully edited entry %1.").arg(entry->title()) << endl;
}
return EXIT_SUCCESS;
}
8 changes: 4 additions & 4 deletions src/cli/Estimate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ static void estimate(const char* pwd, bool advanced)

int Estimate::execute(const QStringList& arguments)
{
TextStream in(Utils::STDIN, QIODevice::ReadOnly);
TextStream out(Utils::STDOUT, QIODevice::WriteOnly);
TextStream inputTextStream(Utils::STDIN, QIODevice::ReadOnly);
TextStream errorTextStream(Utils::STDERR, QIODevice::WriteOnly);

QCommandLineParser parser;
parser.setApplicationDescription(description);
Expand All @@ -171,15 +171,15 @@ int Estimate::execute(const QStringList& arguments)

const QStringList args = parser.positionalArguments();
if (args.size() > 1) {
out << parser.helpText().replace("keepassxc-cli", "keepassxc-cli estimate");
errorTextStream << parser.helpText().replace("keepassxc-cli", "keepassxc-cli estimate");
return EXIT_FAILURE;
}

QString password;
if (args.size() == 1) {
password = args.at(0);
} else {
password = in.readLine();
password = inputTextStream.readLine();
}

estimate(password.toLatin1(), parser.isSet(advancedOption));
Expand Down
22 changes: 11 additions & 11 deletions src/cli/Extract.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ Extract::~Extract()

int Extract::execute(const QStringList& arguments)
{
TextStream out(Utils::STDOUT, QIODevice::WriteOnly);
TextStream err(Utils::STDERR, QIODevice::WriteOnly);
TextStream outputTextStream(Utils::STDOUT, QIODevice::WriteOnly);
TextStream errorTextStream(Utils::STDERR, QIODevice::WriteOnly);

QCommandLineParser parser;
parser.setApplicationDescription(description);
Expand All @@ -56,12 +56,12 @@ int Extract::execute(const QStringList& arguments)

const QStringList args = parser.positionalArguments();
if (args.size() != 1) {
out << parser.helpText().replace("keepassxc-cli", "keepassxc-cli extract");
errorTextStream << parser.helpText().replace("keepassxc-cli", "keepassxc-cli extract");
return EXIT_FAILURE;
}

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

auto compositeKey = QSharedPointer<CompositeKey>::create();
Expand All @@ -77,12 +77,12 @@ int Extract::execute(const QStringList& arguments)
auto fileKey = QSharedPointer<FileKey>::create();
QString errorMsg;
if (!fileKey->load(keyFilePath, &errorMsg)) {
err << QObject::tr("Failed to load key file %1: %2").arg(keyFilePath, errorMsg) << endl;
errorTextStream << QObject::tr("Failed to load key file %1: %2").arg(keyFilePath, errorMsg) << endl;
return EXIT_FAILURE;
}

if (fileKey->type() != FileKey::Hashed) {
err << QObject::tr("WARNING: You are using a legacy key file format which may become\n"
errorTextStream << QObject::tr("WARNING: You are using a legacy key file format which may become\n"
"unsupported in the future.\n\n"
"Please consider generating a new key file.")
<< endl;
Expand All @@ -95,11 +95,11 @@ int Extract::execute(const QStringList& arguments)
const QString& databaseFilename = args.at(0);
QFile dbFile(databaseFilename);
if (!dbFile.exists()) {
err << QObject::tr("File %1 does not exist.").arg(databaseFilename) << endl;
errorTextStream << QObject::tr("File %1 does not exist.").arg(databaseFilename) << endl;
return EXIT_FAILURE;
}
if (!dbFile.open(QIODevice::ReadOnly)) {
err << QObject::tr("Unable to open file %1.").arg(databaseFilename) << endl;
errorTextStream << QObject::tr("Unable to open file %1.").arg(databaseFilename) << endl;
return EXIT_FAILURE;
}

Expand All @@ -112,14 +112,14 @@ int Extract::execute(const QStringList& arguments)

if (reader.hasError()) {
if (xmlData.isEmpty()) {
err << QObject::tr("Error while reading the database:\n%1").arg(reader.errorString()) << endl;
errorTextStream << QObject::tr("Error while reading the database:\n%1").arg(reader.errorString()) << endl;
} else {
err << QObject::tr("Error while parsing the database:\n%1").arg(reader.errorString()) << endl;
errorTextStream << QObject::tr("Error while parsing the database:\n%1").arg(reader.errorString()) << endl;
}
return EXIT_FAILURE;
}

out << xmlData.constData() << endl;
outputTextStream << xmlData.constData() << endl;

return EXIT_SUCCESS;
}
10 changes: 5 additions & 5 deletions src/cli/Generate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ Generate::~Generate()

int Generate::execute(const QStringList& arguments)
{
TextStream in(Utils::STDIN, QIODevice::ReadOnly);
TextStream out(Utils::STDOUT, QIODevice::WriteOnly);
TextStream outputTextStream(Utils::STDOUT, QIODevice::WriteOnly);
TextStream errorTextStream(Utils::STDERR, QIODevice::WriteOnly);

QCommandLineParser parser;
parser.setApplicationDescription(description);
Expand Down Expand Up @@ -84,7 +84,7 @@ int Generate::execute(const QStringList& arguments)

const QStringList args = parser.positionalArguments();
if (!args.isEmpty()) {
out << parser.helpText().replace("keepassxc-cli", "keepassxc-cli generate");
errorTextStream << parser.helpText().replace("keepassxc-cli", "keepassxc-cli generate");
return EXIT_FAILURE;
}

Expand Down Expand Up @@ -128,12 +128,12 @@ int Generate::execute(const QStringList& arguments)
passwordGenerator.setExcludedChars(parser.value(exclude));

if (!passwordGenerator.isValid()) {
out << parser.helpText().replace("keepassxc-cli", "keepassxc-cli generate");
errorTextStream << parser.helpText().replace("keepassxc-cli", "keepassxc-cli generate");
return EXIT_FAILURE;
}

QString password = passwordGenerator.generatePassword();
out << password << endl;
outputTextStream << password << endl;

return EXIT_SUCCESS;
}
14 changes: 7 additions & 7 deletions src/cli/List.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ List::~List()

int List::execute(const QStringList& arguments)
{
TextStream out(Utils::STDOUT);
TextStream errorTextStream(Utils::STDERR, QIODevice::WriteOnly);

QCommandLineParser parser;
parser.setApplicationDescription(description);
Expand All @@ -58,7 +58,7 @@ int List::execute(const QStringList& arguments)

const QStringList args = parser.positionalArguments();
if (args.size() != 1 && args.size() != 2) {
out << parser.helpText().replace("keepassxc-cli", "keepassxc-cli ls");
errorTextStream << parser.helpText().replace("keepassxc-cli", "keepassxc-cli ls");
return EXIT_FAILURE;
}

Expand All @@ -80,20 +80,20 @@ int List::execute(const QStringList& arguments)

int List::listGroup(Database* database, bool recursive, const QString& groupPath)
{
TextStream out(Utils::STDOUT, QIODevice::WriteOnly);
TextStream err(Utils::STDERR, QIODevice::WriteOnly);
TextStream outputTextStream(Utils::STDOUT, QIODevice::WriteOnly);
TextStream errorTextStream(Utils::STDERR, QIODevice::WriteOnly);

if (groupPath.isEmpty()) {
out << database->rootGroup()->print(recursive) << flush;
outputTextStream << database->rootGroup()->print(recursive) << flush;
return EXIT_SUCCESS;
}

Group* group = database->rootGroup()->findGroupByPath(groupPath);
if (!group) {
err << QObject::tr("Cannot find group %1.").arg(groupPath) << endl;
errorTextStream << QObject::tr("Cannot find group %1.").arg(groupPath) << endl;
return EXIT_FAILURE;
}

out << group->print(recursive) << flush;
outputTextStream << group->print(recursive) << flush;
return EXIT_SUCCESS;
}
12 changes: 6 additions & 6 deletions src/cli/Locate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Locate::~Locate()

int Locate::execute(const QStringList& arguments)
{
TextStream out(Utils::STDOUT, QIODevice::WriteOnly);
TextStream errorTextStream(Utils::STDERR, QIODevice::WriteOnly);

QCommandLineParser parser;
parser.setApplicationDescription(description);
Expand All @@ -55,7 +55,7 @@ int Locate::execute(const QStringList& arguments)

const QStringList args = parser.positionalArguments();
if (args.size() != 2) {
out << parser.helpText().replace("keepassxc-cli", "keepassxc-cli locate");
errorTextStream << parser.helpText().replace("keepassxc-cli", "keepassxc-cli locate");
return EXIT_FAILURE;
}

Expand All @@ -72,17 +72,17 @@ int Locate::execute(const QStringList& arguments)

int Locate::locateEntry(Database* database, const QString& searchTerm)
{
TextStream out(Utils::STDOUT, QIODevice::WriteOnly);
TextStream err(Utils::STDERR, QIODevice::WriteOnly);
TextStream outputTextStream(Utils::STDOUT, QIODevice::WriteOnly);
TextStream errorTextStream(Utils::STDERR, QIODevice::WriteOnly);

QStringList results = database->rootGroup()->locate(searchTerm);
if (results.isEmpty()) {
err << "No results for that search term." << endl;
errorTextStream << "No results for that search term." << endl;
return EXIT_FAILURE;
}

for (const QString& result : asConst(results)) {
out << result << endl;
outputTextStream << result << endl;
}
return EXIT_SUCCESS;
}
Loading