CLI: Use stderr for password prompt

Fixes #3398.

Convert to QTextStream for all CLI IO and greatly improve CLI tests

* Completely overhaul CLI tests to be much more streamlined and easy to read. Removed unnecessary code blocks by using existing functions.

Co-authored-by: Emma Brooks <me@pluvano.com>
This commit is contained in:
Jonathan White 2020-05-11 07:31:29 -04:00
parent 612f8d2e5b
commit 485852c9db
30 changed files with 938 additions and 1407 deletions

View file

@ -55,30 +55,29 @@ int Import::execute(const QStringList& arguments)
return EXIT_FAILURE;
}
TextStream outputTextStream(parser->isSet(Command::QuietOption) ? Utils::DEVNULL : Utils::STDOUT,
QIODevice::WriteOnly);
TextStream errorTextStream(Utils::STDERR, QIODevice::WriteOnly);
auto& out = parser->isSet(Command::QuietOption) ? Utils::DEVNULL : Utils::STDOUT;
auto& err = Utils::STDERR;
const QStringList args = parser->positionalArguments();
const QString& xmlExportPath = args.at(0);
const QString& dbPath = args.at(1);
if (QFileInfo::exists(dbPath)) {
errorTextStream << QObject::tr("File %1 already exists.").arg(dbPath) << endl;
err << QObject::tr("File %1 already exists.").arg(dbPath) << endl;
return EXIT_FAILURE;
}
auto key = QSharedPointer<CompositeKey>::create();
auto passwordKey = Utils::getPasswordFromStdin();
auto passwordKey = Utils::getConfirmedPassword();
if (passwordKey.isNull()) {
errorTextStream << QObject::tr("Failed to set database password.") << endl;
err << QObject::tr("Failed to set database password.") << endl;
return EXIT_FAILURE;
}
key->addKey(passwordKey);
if (key->isEmpty()) {
errorTextStream << QObject::tr("No key is set. Aborting database creation.") << endl;
err << QObject::tr("No key is set. Aborting database creation.") << endl;
return EXIT_FAILURE;
}
@ -88,15 +87,15 @@ int Import::execute(const QStringList& arguments)
db.setKey(key);
if (!db.import(xmlExportPath, &errorMessage)) {
errorTextStream << QObject::tr("Unable to import XML database: %1").arg(errorMessage) << endl;
err << QObject::tr("Unable to import XML database: %1").arg(errorMessage) << endl;
return EXIT_FAILURE;
}
if (!db.saveAs(dbPath, &errorMessage, true, false)) {
errorTextStream << QObject::tr("Failed to save the database: %1.").arg(errorMessage) << endl;
err << QObject::tr("Failed to save the database: %1.").arg(errorMessage) << endl;
return EXIT_FAILURE;
}
outputTextStream << QObject::tr("Successfully imported database.") << endl;
out << QObject::tr("Successfully imported database.") << endl;
return EXIT_SUCCESS;
}