Cleanup CLI includes across all components

* Remove unused include files
* Move includes out of widely shared headers (reduced rebuild time)
* Consolidate code for Analyze command
This commit is contained in:
Jonathan White 2021-02-20 08:41:47 -05:00
parent dc496fd1d9
commit be3e77d721
33 changed files with 79 additions and 232 deletions

View File

@ -15,18 +15,12 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <cstdlib>
#include <stdio.h>
#include "Add.h"
#include "cli/Generate.h"
#include "cli/TextStream.h"
#include "cli/Utils.h"
#include "core/Database.h"
#include "Generate.h"
#include "Utils.h"
#include "core/Entry.h"
#include "core/Group.h"
#include "core/PasswordGenerator.h"
const QCommandLineOption Add::UsernameOption = QCommandLineOption(QStringList() << "u"
<< "username",

View File

@ -32,7 +32,6 @@ public:
static const QCommandLineOption NotesOption;
static const QCommandLineOption PasswordPromptOption;
static const QCommandLineOption GenerateOption;
static const QCommandLineOption PasswordLengthOption;
};
#endif // KEEPASSXC_ADD_H

View File

@ -15,14 +15,9 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <cstdlib>
#include <stdio.h>
#include "AddGroup.h"
#include "cli/TextStream.h"
#include "cli/Utils.h"
#include "core/Database.h"
#include "Utils.h"
#include "core/Entry.h"
#include "core/Group.h"

View File

@ -16,16 +16,14 @@
*/
#include "Analyze.h"
#include "cli/Utils.h"
#include "Utils.h"
#include "core/Group.h"
#include "core/HibpOffline.h"
#include "core/Tools.h"
#include <QCommandLineParser>
#include <QFile>
#include <QString>
#include "cli/TextStream.h"
#include "core/Group.h"
#include "core/Tools.h"
const QCommandLineOption Analyze::HIBPDatabaseOption = QCommandLineOption(
{"H", "hibp"},
@ -84,23 +82,21 @@ int Analyze::executeWithDatabase(QSharedPointer<Database> database, QSharedPoint
}
}
for (auto& finding : findings) {
printHibpFinding(finding.first, finding.second, out);
for (const auto& finding : findings) {
const auto entry = finding.first;
auto count = finding.second;
QString path = entry->title();
for (auto g = entry->group(); g && g != g->database()->rootGroup(); g = g->parentGroup()) {
path.prepend("/").prepend(g->name());
}
if (count > 0) {
out << QObject::tr("Password for '%1' has been leaked %2 time(s)!", "", count).arg(path).arg(count) << endl;
} else {
out << QObject::tr("Password for '%1' has been leaked!", "", count).arg(path) << endl;
}
}
return EXIT_SUCCESS;
}
void Analyze::printHibpFinding(const Entry* entry, int count, QTextStream& out)
{
QString path = entry->title();
for (auto g = entry->group(); g && g != g->database()->rootGroup(); g = g->parentGroup()) {
path.prepend("/").prepend(g->name());
}
if (count > 0) {
out << QObject::tr("Password for '%1' has been leaked %2 time(s)!", "", count).arg(path).arg(count) << endl;
} else {
out << QObject::tr("Password for '%1' has been leaked!", "", count).arg(path) << endl;
}
}

View File

@ -28,9 +28,6 @@ public:
static const QCommandLineOption HIBPDatabaseOption;
static const QCommandLineOption OkonOption;
private:
void printHibpFinding(const Entry* entry, int count, QTextStream& out);
};
#endif // KEEPASSXC_HIBP_H

View File

@ -15,18 +15,12 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <chrono>
#include <cstdlib>
#include <thread>
#include "Clip.h"
#include "cli/TextStream.h"
#include "cli/Utils.h"
#include "core/Database.h"
#include "Utils.h"
#include "core/Entry.h"
#include "core/Global.h"
#include "core/Group.h"
#include "core/Tools.h"
const QCommandLineOption Clip::AttributeOption = QCommandLineOption(
QStringList() << "a"
@ -155,7 +149,7 @@ int Clip::executeWithDatabase(QSharedPointer<Database> database, QSharedPointer<
out << '\r' << QString(lastLine.size(), ' ') << '\r';
lastLine = QObject::tr("Clearing the clipboard in %1 second(s)…", "", timeoutSeconds).arg(timeoutSeconds);
out << lastLine << flush;
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
Tools::sleep(1000);
--timeoutSeconds;
}
Utils::clipText("");

View File

@ -17,13 +17,6 @@
#include "Close.h"
#include <QCommandLineParser>
#include <QtGlobal>
#include "DatabaseCommand.h"
#include "TextStream.h"
#include "Utils.h"
Close::Close()
{
name = QString("close");

View File

@ -15,13 +15,6 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <cstdio>
#include <cstdlib>
#include <utility>
#include <QFileInfo>
#include <QMap>
#include "Command.h"
#include "Add.h"
@ -47,9 +40,11 @@
#include "Remove.h"
#include "RemoveGroup.h"
#include "Show.h"
#include "TextStream.h"
#include "Utils.h"
#include <QFileInfo>
#include <QRegularExpression>
const QCommandLineOption Command::HelpOption = QCommandLineOption(QStringList()
#ifdef Q_OS_WIN
<< QStringLiteral("?")

View File

@ -21,7 +21,6 @@
#include <QCommandLineOption>
#include <QCommandLineParser>
#include <QList>
#include <QObject>
#include <QString>
#include <QStringList>

View File

@ -15,21 +15,12 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <cstdlib>
#include <stdio.h>
#include "Create.h"
#include "Utils.h"
#include "keys/FileKey.h"
#include <QFileInfo>
#include <QString>
#include <QTextStream>
#include "Create.h"
#include "Utils.h"
#include "core/Database.h"
#include "keys/CompositeKey.h"
#include "keys/FileKey.h"
#include "keys/Key.h"
const QCommandLineOption Create::DecryptionTimeOption =
QCommandLineOption(QStringList() << "t"

View File

@ -16,7 +16,6 @@
*/
#include "DatabaseCommand.h"
#include "Utils.h"
DatabaseCommand::DatabaseCommand()

View File

@ -18,11 +18,7 @@
#ifndef KEEPASSXC_DATABASECOMMAND_H
#define KEEPASSXC_DATABASECOMMAND_H
#include <QCommandLineOption>
#include "Command.h"
#include "Utils.h"
#include "core/Database.h"
class DatabaseCommand : public Command
{

View File

@ -15,13 +15,9 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <cstdlib>
#include <stdio.h>
#include "Diceware.h"
#include "Utils.h"
#include "cli/TextStream.h"
#include "core/PassphraseGenerator.h"
const QCommandLineOption Diceware::WordCountOption =

View File

@ -15,19 +15,13 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <cstdlib>
#include <stdio.h>
#include "Edit.h"
#include "cli/Add.h"
#include "cli/Generate.h"
#include "cli/TextStream.h"
#include "cli/Utils.h"
#include "core/Database.h"
#include "Add.h"
#include "Generate.h"
#include "Utils.h"
#include "core/Entry.h"
#include "core/Group.h"
#include "core/PasswordGenerator.h"
const QCommandLineOption Edit::TitleOption = QCommandLineOption(QStringList() << "t"
<< "title",

View File

@ -16,21 +16,11 @@
*/
#include "Estimate.h"
#include "cli/Utils.h"
#include "cli/TextStream.h"
#include "Utils.h"
#include "core/PasswordHealth.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <zxcvbn.h>
/* For pre-compiled headers under windows */
#ifdef _WIN32
#ifndef __MINGW32__
#include "stdafx.h"
#endif
#endif
#include <zxcvbn.h>
const QCommandLineOption Estimate::AdvancedOption =
QCommandLineOption(QStringList() << "a"

View File

@ -17,10 +17,6 @@
#include "Exit.h"
#include <QCommandLineParser>
#include <QObject>
#include <QtGlobal>
Exit::Exit(const QString& name)
{
this->name = name;

View File

@ -15,14 +15,10 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <cstdlib>
#include <stdio.h>
#include "Export.h"
#include "cli/TextStream.h"
#include "cli/Utils.h"
#include "core/Database.h"
#include "TextStream.h"
#include "Utils.h"
#include "format/CsvExporter.h"
const QCommandLineOption Export::FormatOption = QCommandLineOption(

View File

@ -15,13 +15,10 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <cstdlib>
#include <stdio.h>
#include "Generate.h"
#include "cli/TextStream.h"
#include "cli/Utils.h"
#include "TextStream.h"
#include "Utils.h"
const QCommandLineOption Generate::PasswordLengthOption =
QCommandLineOption(QStringList() << "L"

View File

@ -17,8 +17,6 @@
#include "Help.h"
#include "Command.h"
#include "TextStream.h"
#include "Utils.h"
Help::Help()

View File

@ -15,22 +15,12 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <cstdlib>
#include <stdio.h>
#include <QFileInfo>
#include <QString>
#include <QTextStream>
#include "Create.h"
#include "Import.h"
#include "cli/TextStream.h"
#include "cli/Utils.h"
#include "core/Database.h"
#include "keys/CompositeKey.h"
#include "keys/FileKey.h"
#include "keys/Key.h"
#include "Create.h"
#include "Utils.h"
#include <QFileInfo>
/**
* Create a database file from an XML export of another database.

View File

@ -14,16 +14,12 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <cstdlib>
#include <stdio.h>
#include "Info.h"
#include "Utils.h"
#include "core/Database.h"
#include "Utils.h"
#include "core/Global.h"
#include "core/Metadata.h"
#include "format/KeePass2.h"
Info::Info()
{

View File

@ -15,15 +15,9 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <cstdlib>
#include <stdio.h>
#include "List.h"
#include "cli/Utils.h"
#include "cli/TextStream.h"
#include "core/Database.h"
#include "core/Entry.h"
#include "Utils.h"
#include "core/Group.h"
const QCommandLineOption List::RecursiveOption =

View File

@ -15,18 +15,12 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <cstdlib>
#include <stdio.h>
#include "Locate.h"
#include <QStringList>
#include "cli/TextStream.h"
#include "cli/Utils.h"
#include "core/Database.h"
#include "core/Entry.h"
#include "core/Global.h"
#include "TextStream.h"
#include "Utils.h"
#include "core/Group.h"
Locate::Locate()

View File

@ -15,13 +15,10 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <cstdlib>
#include "Merge.h"
#include "cli/TextStream.h"
#include "cli/Utils.h"
#include "core/Database.h"
#include "TextStream.h"
#include "Utils.h"
#include "core/Merger.h"
const QCommandLineOption Merge::SameCredentialsOption =

View File

@ -15,14 +15,10 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <cstdlib>
#include <stdio.h>
#include "Move.h"
#include "cli/TextStream.h"
#include "cli/Utils.h"
#include "core/Database.h"
#include "TextStream.h"
#include "Utils.h"
#include "core/Entry.h"
#include "core/Group.h"

View File

@ -17,12 +17,6 @@
#include "Open.h"
#include <QCommandLineParser>
#include "DatabaseCommand.h"
#include "TextStream.h"
#include "Utils.h"
Open::Open()
{
name = QString("open");

View File

@ -15,18 +15,12 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <cstdlib>
#include <stdio.h>
#include "Remove.h"
#include "cli/TextStream.h"
#include "cli/Utils.h"
#include "core/Database.h"
#include "core/Entry.h"
#include "TextStream.h"
#include "Utils.h"
#include "core/Group.h"
#include "core/Metadata.h"
#include "core/Tools.h"
Remove::Remove()
{

View File

@ -15,18 +15,12 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <cstdlib>
#include <stdio.h>
#include "RemoveGroup.h"
#include "cli/TextStream.h"
#include "cli/Utils.h"
#include "core/Database.h"
#include "core/Entry.h"
#include "TextStream.h"
#include "Utils.h"
#include "core/Group.h"
#include "core/Metadata.h"
#include "core/Tools.h"
RemoveGroup::RemoveGroup()
{

View File

@ -17,18 +17,9 @@
#include "Show.h"
#include <cstdlib>
#include <stdio.h>
#include "Utils.h"
#include "cli/TextStream.h"
#include "core/Database.h"
#include "core/Entry.h"
#include "core/Global.h"
#include "core/Group.h"
#include <QLocale>
const QCommandLineOption Show::TotpOption = QCommandLineOption(QStringList() << "t"
<< "totp",
QObject::tr("Show the entry's current TOTP."));

View File

@ -17,6 +17,11 @@
#include "Utils.h"
#include "core/Database.h"
#include "core/EntryAttributes.h"
#include "keys/CompositeKey.h"
#include "keys/FileKey.h"
#include "keys/PasswordKey.h"
#ifdef WITH_XC_YUBIKEY
#include "keys/YkChallengeResponseKey.h"
#endif
@ -30,7 +35,7 @@
#include <QFileInfo>
#include <QProcess>
#include <QScopedPointer>
#include <QTextStream>
namespace Utils
{
@ -91,7 +96,7 @@ namespace Utils
}
QSharedPointer<Database> unlockDatabase(const QString& databaseFilename,
const bool isPasswordProtected,
bool isPasswordProtected,
const QString& keyFilename,
const QString& yubiKeySlot,
bool quiet)
@ -289,7 +294,7 @@ namespace Utils
QStringList failedProgramNames;
for (auto prog : clipPrograms) {
for (const auto& prog : clipPrograms) {
QScopedPointer<QProcess> clipProcess(new QProcess(nullptr));
// Skip empty parts, otherwise the program may clip the empty string

View File

@ -18,13 +18,13 @@
#ifndef KEEPASSXC_UTILS_H
#define KEEPASSXC_UTILS_H
#include "cli/TextStream.h"
#include "core/Database.h"
#include "core/EntryAttributes.h"
#include "keys/CompositeKey.h"
#include "keys/FileKey.h"
#include "keys/PasswordKey.h"
#include <QtCore/qglobal.h>
#include <QTextStream>
class CompositeKey;
class Database;
class EntryAttributes;
class FileKey;
class PasswordKey;
namespace Utils
{
@ -41,7 +41,7 @@ namespace Utils
QSharedPointer<PasswordKey> getConfirmedPassword();
int clipText(const QString& text);
QSharedPointer<Database> unlockDatabase(const QString& databaseFilename,
const bool isPasswordProtected = true,
bool isPasswordProtected = true,
const QString& keyFilename = {},
const QString& yubiKeySlot = {},
bool quiet = false);

View File

@ -15,20 +15,14 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <cstdlib>
#include <memory>
#include <QCommandLineParser>
#include <QCoreApplication>
#include <QDir>
#include <QScopedPointer>
#include <QFileInfo>
#include <QStringList>
#include "cli/TextStream.h"
#include <cli/Command.h>
#include "Command.h"
#include "DatabaseCommand.h"
#include "Open.h"
#include "TextStream.h"
#include "Utils.h"
#include "config-keepassx.h"
#include "core/Bootstrap.h"
@ -165,10 +159,9 @@ void enterInteractiveMode(const QStringList& arguments)
break;
}
cmd->currentDatabase = currentDatabase;
cmd->currentDatabase.swap(currentDatabase);
cmd->execute(args);
currentDatabase = cmd->currentDatabase;
cmd->currentDatabase.reset();
currentDatabase.swap(cmd->currentDatabase);
}
if (currentDatabase) {
@ -179,7 +172,7 @@ void enterInteractiveMode(const QStringList& arguments)
int main(int argc, char** argv)
{
if (!Crypto::init()) {
qFatal("Fatal error while testing the cryptographic functions:\n%s", qPrintable(Crypto::errorString()));
qWarning("Fatal error while testing the cryptographic functions:\n%s", qPrintable(Crypto::errorString()));
return EXIT_FAILURE;
}

View File

@ -21,18 +21,17 @@
#include "core/Bootstrap.h"
#include "core/Config.h"
#include "core/Entry.h"
#include "core/Global.h"
#include "core/Group.h"
#include "core/Metadata.h"
#include "core/Tools.h"
#include "crypto/Crypto.h"
#include "keys/FileKey.h"
#include "keys/drivers/YubiKey.h"
#include "cli/Add.h"
#include "cli/AddGroup.h"
#include "cli/Analyze.h"
#include "cli/Clip.h"
#include "cli/Command.h"
#include "cli/Create.h"
#include "cli/Diceware.h"
#include "cli/Edit.h"
@ -53,16 +52,11 @@
#include "cli/Utils.h"
#include <QClipboard>
#include <QFuture>
#include <QSet>
#include <QSignalSpy>
#include <QTextStream>
#include <QtConcurrent>
QTEST_MAIN(TestCli)
QSharedPointer<Database> globalCurrentDatabase;
void TestCli::initTestCase()
{
QVERIFY(Crypto::init());