Refactoring : Introducing Command class for CLI commands (#778)

This commit is contained in:
louib 2017-07-17 15:16:53 -04:00 committed by GitHub
parent 99e3af8ff7
commit 3b23e68540
19 changed files with 356 additions and 165 deletions

View File

@ -16,6 +16,8 @@
set(cli_SOURCES set(cli_SOURCES
Clip.cpp Clip.cpp
Clip.h Clip.h
Command.cpp
Command.h
EntropyMeter.cpp EntropyMeter.cpp
EntropyMeter.h EntropyMeter.h
Extract.cpp Extract.cpp

View File

@ -21,7 +21,6 @@
#include "Clip.h" #include "Clip.h"
#include <QApplication> #include <QApplication>
#include <QClipboard>
#include <QCommandLineParser> #include <QCommandLineParser>
#include <QStringList> #include <QStringList>
#include <QTextStream> #include <QTextStream>
@ -32,30 +31,41 @@
#include "core/Group.h" #include "core/Group.h"
#include "gui/Clipboard.h" #include "gui/Clipboard.h"
Clip::Clip()
{
this->name = QString("clip");
this->description = QObject::tr("Copy an entry's password to the clipboard.");
}
Clip::~Clip()
{
}
int Clip::execute(int argc, char** argv) int Clip::execute(int argc, char** argv)
{ {
QStringList arguments; QStringList arguments;
for (int i = 0; i < argc; ++i) { // Skipping the first argument (keepassxc).
for (int i = 1; i < argc; ++i) {
arguments << QString(argv[i]); arguments << QString(argv[i]);
} }
QTextStream out(stdout); QTextStream out(stdout);
QCommandLineParser parser; QCommandLineParser parser;
parser.setApplicationDescription(QCoreApplication::translate("main", "Copy a password to the clipboard")); parser.setApplicationDescription(this->description);
parser.addPositionalArgument("database", QCoreApplication::translate("main", "Path of the database.")); parser.addPositionalArgument("database", QObject::tr("Path of the database."));
QCommandLineOption guiPrompt( QCommandLineOption guiPrompt(QStringList() << "g"
QStringList() << "g"
<< "gui-prompt", << "gui-prompt",
QCoreApplication::translate("main", "Use a GUI prompt unlocking the database.")); QObject::tr("Use a GUI prompt unlocking the database."));
parser.addOption(guiPrompt); parser.addOption(guiPrompt);
parser.addPositionalArgument("entry", QCoreApplication::translate("main", "Name of the entry to clip.")); parser.addPositionalArgument("entry", QObject::tr("Path of the entry to clip."));
parser.process(arguments); parser.process(arguments);
const QStringList args = parser.positionalArguments(); const QStringList args = parser.positionalArguments();
if (args.size() != 2) { if (args.size() != 2) {
QCoreApplication app(argc, argv); QCoreApplication app(argc, argv);
parser.showHelp(EXIT_FAILURE); out << parser.helpText().replace("keepassxc-cli", "keepassxc-cli clip");
return EXIT_FAILURE;
} }
Database* db = nullptr; Database* db = nullptr;
@ -69,14 +79,20 @@ int Clip::execute(int argc, char** argv)
if (!db) { if (!db) {
return EXIT_FAILURE; return EXIT_FAILURE;
} }
return this->clipEntry(db, args.at(1));
}
QString entryId = args.at(1); int Clip::clipEntry(Database* database, QString entryPath)
Entry* entry = db->rootGroup()->findEntry(entryId); {
QTextStream outputTextStream(stdout, QIODevice::WriteOnly);
Entry* entry = database->rootGroup()->findEntry(entryPath);
if (!entry) { if (!entry) {
qCritical("Entry %s not found.", qPrintable(entryId)); qCritical("Entry %s not found.", qPrintable(entryPath));
return EXIT_FAILURE; return EXIT_FAILURE;
} }
Clipboard::instance()->setText(entry->password()); Clipboard::instance()->setText(entry->password());
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

View File

@ -18,10 +18,15 @@
#ifndef KEEPASSXC_CLIP_H #ifndef KEEPASSXC_CLIP_H
#define KEEPASSXC_CLIP_H #define KEEPASSXC_CLIP_H
class Clip #include "Command.h"
class Clip : public Command
{ {
public: public:
static int execute(int argc, char** argv); Clip();
~Clip();
int execute(int argc, char** argv);
int clipEntry(Database* database, QString entryPath);
}; };
#endif // KEEPASSXC_CLIP_H #endif // KEEPASSXC_CLIP_H

80
src/cli/Command.cpp Normal file
View File

@ -0,0 +1,80 @@
/*
* Copyright (C) 2017 KeePassXC Team <team@keepassxc.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 or (at your option)
* version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* 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 <QMap>
#include "Command.h"
#include "Clip.h"
#include "EntropyMeter.h"
#include "Extract.h"
#include "List.h"
#include "Merge.h"
#include "Show.h"
QMap<QString, Command*> commands;
Command::~Command()
{
}
int Command::execute(int, char**)
{
return EXIT_FAILURE;
}
QString Command::getDescriptionLine()
{
QString response = this->name;
QString space(" ");
QString spaces = space.repeated(15 - this->name.length());
response = response.append(spaces);
response = response.append(this->description);
response = response.append("\n");
return response;
}
void populateCommands()
{
if (commands.isEmpty()) {
commands.insert(QString("clip"), new Clip());
commands.insert(QString("entropy-meter"), new EntropyMeter());
commands.insert(QString("extract"), new Extract());
commands.insert(QString("ls"), new List());
commands.insert(QString("merge"), new Merge());
commands.insert(QString("show"), new Show());
}
}
Command* Command::getCommand(QString commandName)
{
populateCommands();
if (commands.contains(commandName)) {
return commands[commandName];
}
return nullptr;
}
QList<Command*> Command::getCommands()
{
populateCommands();
return commands.values();
}

41
src/cli/Command.h Normal file
View File

@ -0,0 +1,41 @@
/*
* Copyright (C) 2017 KeePassXC Team <team@keepassxc.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 or (at your option)
* version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef KEEPASSXC_COMMAND_H
#define KEEPASSXC_COMMAND_H
#include <QList>
#include <QObject>
#include <QString>
#include <QStringList>
#include "core/Database.h"
class Command
{
public:
virtual ~Command();
virtual int execute(int argc, char** argv);
QString name;
QString description;
QString getDescriptionLine();
static QList<Command*> getCommands();
static Command* getCommand(QString commandName);
};
#endif // KEEPASSXC_COMMAND_H

View File

@ -18,8 +18,8 @@
#include "EntropyMeter.h" #include "EntropyMeter.h"
#include <stdio.h> #include <stdio.h>
#include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include <zxcvbn.h> #include <zxcvbn.h>
/* For pre-compiled headers under windows */ /* For pre-compiled headers under windows */
@ -29,6 +29,16 @@
#endif #endif
#endif #endif
EntropyMeter::EntropyMeter()
{
this->name = QString("entropy-meter");
this->description = QObject::tr("Calculate password entropy.");
}
EntropyMeter::~EntropyMeter()
{
}
static void calculate(const char *pwd, int advanced) static void calculate(const char *pwd, int advanced)
{ {
double e; double e;

View File

@ -18,10 +18,14 @@
#ifndef KEEPASSXC_ENTROPYMETER_H #ifndef KEEPASSXC_ENTROPYMETER_H
#define KEEPASSXC_ENTROPYMETER_H #define KEEPASSXC_ENTROPYMETER_H
class EntropyMeter #include "Command.h"
class EntropyMeter : public Command
{ {
public: public:
static int execute(int argc, char** argv); EntropyMeter();
~EntropyMeter();
int execute(int argc, char** argv);
}; };
#endif // KEEPASSXC_ENTROPYMETER_H #endif // KEEPASSXC_ENTROPYMETER_H

View File

@ -26,25 +26,41 @@
#include <QStringList> #include <QStringList>
#include <QTextStream> #include <QTextStream>
#include "cli/PasswordInput.h"
#include "core/Database.h" #include "core/Database.h"
#include "format/KeePass2Reader.h" #include "format/KeePass2Reader.h"
#include "keys/CompositeKey.h" #include "keys/CompositeKey.h"
#include "cli/PasswordInput.h"
Extract::Extract()
{
this->name = QString("extract");
this->description = QObject::tr("Extract and print the content of a database.");
}
Extract::~Extract()
{
}
int Extract::execute(int argc, char** argv) int Extract::execute(int argc, char** argv)
{ {
QStringList arguments;
// Skipping the first argument (keepassxc).
for (int i = 1; i < argc; ++i) {
arguments << QString(argv[i]);
}
QCoreApplication app(argc, argv); QCoreApplication app(argc, argv);
QTextStream out(stdout); QTextStream out(stdout);
QCommandLineParser parser; QCommandLineParser parser;
parser.setApplicationDescription( parser.setApplicationDescription(this->description);
QCoreApplication::translate("main", "Extract and print the content of a database.")); parser.addPositionalArgument("database", QObject::tr("Path of the database to extract."));
parser.addPositionalArgument("database", QCoreApplication::translate("main", "Path of the database to extract.")); parser.process(arguments);
parser.process(app);
const QStringList args = parser.positionalArguments(); const QStringList args = parser.positionalArguments();
if (args.size() != 1) { if (args.size() != 1) {
parser.showHelp(EXIT_FAILURE); out << parser.helpText().replace("keepassxc-cli", "keepassxc-cli extract");
return EXIT_FAILURE;
} }
out << "Insert the database password\n> "; out << "Insert the database password\n> ";

View File

@ -18,10 +18,14 @@
#ifndef KEEPASSXC_EXTRACT_H #ifndef KEEPASSXC_EXTRACT_H
#define KEEPASSXC_EXTRACT_H #define KEEPASSXC_EXTRACT_H
class Extract #include "Command.h"
class Extract : public Command
{ {
public: public:
static int execute(int argc, char** argv); Extract();
~Extract();
int execute(int argc, char** argv);
}; };
#endif // KEEPASSXC_EXTRACT_H #endif // KEEPASSXC_EXTRACT_H

View File

@ -26,43 +26,47 @@
#include <QStringList> #include <QStringList>
#include <QTextStream> #include <QTextStream>
#include "gui/UnlockDatabaseDialog.h"
#include "core/Database.h" #include "core/Database.h"
#include "core/Entry.h" #include "core/Entry.h"
#include "core/Group.h" #include "core/Group.h"
#include "keys/CompositeKey.h" #include "gui/UnlockDatabaseDialog.h"
List::List()
{
this->name = QString("ls");
this->description = QObject::tr("List database entries.");
}
List::~List()
{
}
int List::execute(int argc, char** argv) int List::execute(int argc, char** argv)
{ {
QStringList arguments; QStringList arguments;
for (int i = 0; i < argc; ++i) { // Skipping the first argument (keepassxc).
for (int i = 1; i < argc; ++i) {
arguments << QString(argv[i]); arguments << QString(argv[i]);
} }
QTextStream out(stdout); QTextStream out(stdout);
QCommandLineParser parser; QCommandLineParser parser;
parser.setApplicationDescription(QCoreApplication::translate("main", "List database entries.")); parser.setApplicationDescription(this->description);
parser.addPositionalArgument("database", QCoreApplication::translate("main", "Path of the database.")); parser.addPositionalArgument("database", QObject::tr("Path of the database."));
parser.addPositionalArgument("group", parser.addPositionalArgument(
QCoreApplication::translate("main", "Path of the group to list. Default is /"), "group", QObject::tr("Path of the group to list. Default is /"), QString("[group]"));
QString("[group]")); QCommandLineOption guiPrompt(QStringList() << "g"
QCommandLineOption printUuidsOption(
QStringList() << "u"
<< "print-uuids",
QCoreApplication::translate("main", "Print the UUIDs of the entries and groups."));
parser.addOption(printUuidsOption);
QCommandLineOption guiPrompt(
QStringList() << "g"
<< "gui-prompt", << "gui-prompt",
QCoreApplication::translate("main", "Use a GUI prompt unlocking the database.")); QObject::tr("Use a GUI prompt unlocking the database."));
parser.addOption(guiPrompt); parser.addOption(guiPrompt);
parser.process(arguments); parser.process(arguments);
const QStringList args = parser.positionalArguments(); const QStringList args = parser.positionalArguments();
if (args.size() != 1 && args.size() != 2) { if (args.size() != 1 && args.size() != 2) {
QCoreApplication app(argc, argv); QCoreApplication app(argc, argv);
parser.showHelp(EXIT_FAILURE); out << parser.helpText().replace("keepassxc-cli", "keepassxc-cli ls");
return EXIT_FAILURE;
} }
Database* db = nullptr; Database* db = nullptr;
@ -78,17 +82,28 @@ int List::execute(int argc, char** argv)
return EXIT_FAILURE; return EXIT_FAILURE;
} }
Group* group = db->rootGroup();
if (args.size() == 2) { if (args.size() == 2) {
QString groupPath = args.at(1); return this->listGroup(db, args.at(1));
group = db->rootGroup()->findGroupByPath(groupPath); }
return this->listGroup(db);
}
int List::listGroup(Database* database, QString groupPath)
{
QTextStream outputTextStream(stdout, QIODevice::WriteOnly);
if (groupPath.isEmpty()) {
outputTextStream << database->rootGroup()->print();
outputTextStream.flush();
return EXIT_SUCCESS;
}
Group* group = database->rootGroup()->findGroupByPath(groupPath);
if (group == nullptr) { if (group == nullptr) {
qCritical("Cannot find group %s.", qPrintable(groupPath)); qCritical("Cannot find group %s.", qPrintable(groupPath));
return EXIT_FAILURE; return EXIT_FAILURE;
} }
}
out << group->print(parser.isSet("print-uuids")); outputTextStream << group->print();
out.flush(); outputTextStream.flush();
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

View File

@ -18,10 +18,15 @@
#ifndef KEEPASSXC_LIST_H #ifndef KEEPASSXC_LIST_H
#define KEEPASSXC_LIST_H #define KEEPASSXC_LIST_H
class List #include "Command.h"
class List : public Command
{ {
public: public:
static int execute(int argc, char** argv); List();
~List();
int execute(int argc, char** argv);
int listGroup(Database* database, QString groupPath = QString(""));
}; };
#endif // KEEPASSXC_LIST_H #endif // KEEPASSXC_LIST_H

View File

@ -28,31 +28,39 @@
#include "core/Database.h" #include "core/Database.h"
#include "gui/UnlockDatabaseDialog.h" #include "gui/UnlockDatabaseDialog.h"
Merge::Merge()
{
this->name = QString("merge");
this->description = QObject::tr("Merge two databases.");
}
Merge::~Merge()
{
}
int Merge::execute(int argc, char** argv) int Merge::execute(int argc, char** argv)
{ {
QStringList arguments; QStringList arguments;
for (int i = 0; i < argc; ++i) { // Skipping the first argument (keepassxc).
for (int i = 1; i < argc; ++i) {
arguments << QString(argv[i]); arguments << QString(argv[i]);
} }
QTextStream out(stdout); QTextStream out(stdout);
QCommandLineParser parser; QCommandLineParser parser;
parser.setApplicationDescription(QCoreApplication::translate("main", "Merge two databases.")); parser.setApplicationDescription(this->description);
parser.addPositionalArgument("database1", parser.addPositionalArgument("database1", QObject::tr("Path of the database to merge into."));
QCoreApplication::translate("main", "Path of the database to merge into.")); parser.addPositionalArgument("database2", QObject::tr("Path of the database to merge from."));
parser.addPositionalArgument("database2",
QCoreApplication::translate("main", "Path of the database to merge from."));
QCommandLineOption samePasswordOption( QCommandLineOption samePasswordOption(
QStringList() << "s" QStringList() << "s"
<< "same-password", << "same-password",
QCoreApplication::translate("main", "Use the same password for both database files.")); QObject::tr("Use the same password for both database files."));
QCommandLineOption guiPrompt( QCommandLineOption guiPrompt(QStringList() << "g"
QStringList() << "g"
<< "gui-prompt", << "gui-prompt",
QCoreApplication::translate("main", "Use a GUI prompt unlocking the database.")); QObject::tr("Use a GUI prompt unlocking the database."));
parser.addOption(guiPrompt); parser.addOption(guiPrompt);
parser.addOption(samePasswordOption); parser.addOption(samePasswordOption);
@ -61,7 +69,8 @@ int Merge::execute(int argc, char** argv)
const QStringList args = parser.positionalArguments(); const QStringList args = parser.positionalArguments();
if (args.size() != 2) { if (args.size() != 2) {
QCoreApplication app(argc, argv); QCoreApplication app(argc, argv);
parser.showHelp(EXIT_FAILURE); out << parser.helpText().replace("keepassxc-cli", "keepassxc-cli merge");
return EXIT_FAILURE;
} }
Database* db1; Database* db1;
@ -100,5 +109,4 @@ int Merge::execute(int argc, char** argv)
out << "Successfully merged the database files.\n"; out << "Successfully merged the database files.\n";
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

View File

@ -18,10 +18,14 @@
#ifndef KEEPASSXC_MERGE_H #ifndef KEEPASSXC_MERGE_H
#define KEEPASSXC_MERGE_H #define KEEPASSXC_MERGE_H
class Merge #include "Command.h"
class Merge : public Command
{ {
public: public:
static int execute(int argc, char** argv); Merge();
~Merge();
int execute(int argc, char** argv);
}; };
#endif // KEEPASSXC_MERGE_H #endif // KEEPASSXC_MERGE_H

View File

@ -28,43 +28,64 @@
#include "core/Database.h" #include "core/Database.h"
#include "core/Entry.h" #include "core/Entry.h"
#include "core/Group.h" #include "core/Group.h"
#include "keys/CompositeKey.h"
#include "cli/PasswordInput.h" Show::Show()
{
this->name = QString("show");
this->description = QObject::tr("Show an entry's information.");
}
Show::~Show()
{
}
int Show::execute(int argc, char** argv) int Show::execute(int argc, char** argv)
{ {
QStringList arguments;
// Skipping the first argument (keepassxc).
for (int i = 1; i < argc; ++i) {
arguments << QString(argv[i]);
}
QCoreApplication app(argc, argv); QCoreApplication app(argc, argv);
QTextStream out(stdout); QTextStream out(stdout);
QCommandLineParser parser; QCommandLineParser parser;
parser.setApplicationDescription(QCoreApplication::translate("main", "Show a password.")); parser.setApplicationDescription(this->description);
parser.addPositionalArgument("database", QCoreApplication::translate("main", "Path of the database.")); parser.addPositionalArgument("database", QObject::tr("Path of the database."));
parser.addPositionalArgument("entry", QCoreApplication::translate("main", "Name of the entry to show.")); parser.addPositionalArgument("entry", QObject::tr("Name of the entry to show."));
parser.process(app); parser.process(arguments);
const QStringList args = parser.positionalArguments(); const QStringList args = parser.positionalArguments();
if (args.size() != 2) { if (args.size() != 2) {
parser.showHelp(EXIT_FAILURE); out << parser.helpText().replace("keepassxc-cli", "keepassxc-cli show");
return EXIT_FAILURE;
} }
out << "Insert the database password\n> "; Database* db = Database::unlockFromStdin(args.at(0));
out.flush();
QString line = PasswordInput::getPassword();
CompositeKey key = CompositeKey::readFromLine(line);
Database* db = Database::openDatabaseFile(args.at(0), key);
if (db == nullptr) { if (db == nullptr) {
return EXIT_FAILURE; return EXIT_FAILURE;
} }
QString entryId = args.at(1); return this->showEntry(db, args.at(1));
Entry* entry = db->rootGroup()->findEntry(entryId); }
int Show::showEntry(Database* database, QString entryPath)
{
QTextStream inputTextStream(stdin, QIODevice::ReadOnly);
QTextStream outputTextStream(stdout, QIODevice::WriteOnly);
Entry* entry = database->rootGroup()->findEntry(entryPath);
if (!entry) { if (!entry) {
qCritical("Entry %s not found.", qPrintable(entryId)); qCritical("Could not find entry with path %s.", qPrintable(entryPath));
return EXIT_FAILURE; return EXIT_FAILURE;
} }
out << entry->password() << "\n"; outputTextStream << " title: " << entry->title() << endl;
outputTextStream << "username: " << entry->username() << endl;
outputTextStream << "password: " << entry->password() << endl;
outputTextStream << " URL: " << entry->url() << endl;
outputTextStream << " Notes: " << entry->notes() << endl;
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

View File

@ -18,10 +18,15 @@
#ifndef KEEPASSXC_SHOW_H #ifndef KEEPASSXC_SHOW_H
#define KEEPASSXC_SHOW_H #define KEEPASSXC_SHOW_H
class Show #include "Command.h"
class Show : public Command
{ {
public: public:
static int execute(int argc, char** argv); Show();
~Show();
int execute(int argc, char** argv);
int showEntry(Database* database, QString entryPath);
}; };
#endif // KEEPASSXC_SHOW_H #endif // KEEPASSXC_SHOW_H

View File

@ -22,12 +22,7 @@
#include <QStringList> #include <QStringList>
#include <QTextStream> #include <QTextStream>
#include <cli/Clip.h> #include <cli/Command.h>
#include <cli/EntropyMeter.h>
#include <cli/Extract.h>
#include <cli/List.h>
#include <cli/Merge.h>
#include <cli/Show.h>
#include "config-keepassx.h" #include "config-keepassx.h"
#include "core/Tools.h" #include "core/Tools.h"
@ -48,6 +43,7 @@ int main(int argc, char** argv)
return EXIT_FAILURE; return EXIT_FAILURE;
} }
QTextStream out(stdout);
QStringList arguments; QStringList arguments;
for (int i = 0; i < argc; ++i) { for (int i = 0; i < argc; ++i) {
arguments << QString(argv[i]); arguments << QString(argv[i]);
@ -55,16 +51,13 @@ int main(int argc, char** argv)
QCommandLineParser parser; QCommandLineParser parser;
QString description("KeePassXC command line interface."); QString description("KeePassXC command line interface.");
description = description.append(QString("\n\nAvailable commands:")); description = description.append(QObject::tr("\n\nAvailable commands:\n"));
description = description.append(QString("\n clip\t\tCopy a password to the clipboard.")); for (Command* command : Command::getCommands()) {
description = description.append(QString("\n extract\tExtract and print the content of a database.")); description = description.append(command->getDescriptionLine());
description = description.append(QString("\n entropy-meter\tCalculate password entropy.")); }
description = description.append(QString("\n list\t\tList database entries.")); parser.setApplicationDescription(description);
description = description.append(QString("\n merge\t\tMerge two databases."));
description = description.append(QString("\n show\t\tShow a password."));
parser.setApplicationDescription(QCoreApplication::translate("main", qPrintable(description)));
parser.addPositionalArgument("command", QCoreApplication::translate("main", "Name of the command to execute.")); parser.addPositionalArgument("command", QObject::tr("Name of the command to execute."));
parser.addHelpOption(); parser.addHelpOption();
parser.addVersionOption(); parser.addVersionOption();
@ -78,50 +71,16 @@ int main(int argc, char** argv)
app.setApplicationVersion(KEEPASSX_VERSION); app.setApplicationVersion(KEEPASSX_VERSION);
if (parser.isSet("version")) { if (parser.isSet("version")) {
// Switch to parser.showVersion() when available (QT 5.4). // Switch to parser.showVersion() when available (QT 5.4).
QTextStream out(stdout); out << KEEPASSX_VERSION << endl;
out << KEEPASSX_VERSION << "\n";
out.flush();
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
parser.showHelp(); parser.showHelp();
} }
QString commandName = parser.positionalArguments().at(0); QString commandName = parser.positionalArguments().at(0);
Command* command = Command::getCommand(commandName);
int exitCode = EXIT_FAILURE; if (command == nullptr) {
if (commandName == "clip") {
// Removing the first cli argument before dispatching.
++argv;
--argc;
argv[0] = const_cast<char*>("keepassxc-cli clip");
exitCode = Clip::execute(argc, argv);
} else if (commandName == "entropy-meter") {
++argv;
--argc;
argv[0] = const_cast<char*>("keepassxc-cli entropy-meter");
exitCode = EntropyMeter::execute(argc, argv);
} else if (commandName == "extract") {
++argv;
--argc;
argv[0] = const_cast<char*>("keepassxc-cli extract");
exitCode = Extract::execute(argc, argv);
} else if (commandName == "list") {
++argv;
--argc;
argv[0] = const_cast<char*>("keepassxc-cli list");
exitCode = List::execute(argc, argv);
} else if (commandName == "merge") {
++argv;
--argc;
argv[0] = const_cast<char*>("keepassxc-cli merge");
exitCode = Merge::execute(argc, argv);
} else if (commandName == "show") {
++argv;
--argc;
argv[0] = const_cast<char*>("keepassxc-cli show");
exitCode = Show::execute(argc, argv);
} else {
qCritical("Invalid command %s.", qPrintable(commandName)); qCritical("Invalid command %s.", qPrintable(commandName));
QCoreApplication app(argc, argv); QCoreApplication app(argc, argv);
app.setApplicationVersion(KEEPASSX_VERSION); app.setApplicationVersion(KEEPASSX_VERSION);
@ -130,6 +89,8 @@ int main(int argc, char** argv)
parser.showHelp(EXIT_FAILURE); parser.showHelp(EXIT_FAILURE);
} }
int exitCode = command->execute(argc, argv);
#if defined(WITH_ASAN) && defined(WITH_LSAN) #if defined(WITH_ASAN) && defined(WITH_LSAN)
// do leak check here to prevent massive tail of end-of-process leak errors from third-party libraries // do leak check here to prevent massive tail of end-of-process leak errors from third-party libraries
__lsan_do_leak_check(); __lsan_do_leak_check();

View File

@ -578,7 +578,7 @@ Group* Group::findGroupByPath(QString groupPath, QString basePath)
} }
QString Group::print(bool printUuids, QString baseName, int depth) QString Group::print(bool recursive, int depth)
{ {
QString response; QString response;
@ -590,21 +590,14 @@ QString Group::print(bool printUuids, QString baseName, int depth)
} }
for (Entry* entry : entries()) { for (Entry* entry : entries()) {
response += indentation + entry->title(); response += indentation + entry->title() + "\n";
if (printUuids) {
response += " " + entry->uuid().toHex();
}
response += "\n";
} }
for (Group* innerGroup : children()) { for (Group* innerGroup : children()) {
QString newBaseName = baseName + innerGroup->name() + "/"; response += indentation + innerGroup->name() + "/\n";
response += indentation + newBaseName; if (recursive) {
if (printUuids) { response += innerGroup->print(recursive, depth + 1);
response += " " + innerGroup->uuid().toHex();
} }
response += "\n";
response += innerGroup->print(printUuids, newBaseName, depth + 1);
} }
return response; return response;

View File

@ -125,7 +125,7 @@ public:
Group* clone(Entry::CloneFlags entryFlags = Entry::CloneNewUuid | Entry::CloneResetTimeInfo) const; Group* clone(Entry::CloneFlags entryFlags = Entry::CloneNewUuid | Entry::CloneResetTimeInfo) const;
void copyDataFrom(const Group* other); void copyDataFrom(const Group* other);
void merge(const Group* other); void merge(const Group* other);
QString print(bool printUuids = false, QString baseName = QString(""), int depth = 0); QString print(bool recursive = false, int depth = 0);
signals: signals:
void dataChanged(Group* group); void dataChanged(Group* group);

View File

@ -728,10 +728,6 @@ void TestGroup::testPrint()
output = db->rootGroup()->print(); output = db->rootGroup()->print();
QCOMPARE(output, QString("entry1\n")); QCOMPARE(output, QString("entry1\n"));
output = db->rootGroup()->print(true);
QCOMPARE(output, QString("entry1 " + entry1->uuid().toHex() + "\n"));
Group* group1 = new Group(); Group* group1 = new Group();
group1->setName("group1"); group1->setName("group1");
@ -746,11 +742,16 @@ void TestGroup::testPrint()
output = db->rootGroup()->print(); output = db->rootGroup()->print();
QVERIFY(output.contains(QString("entry1\n"))); QVERIFY(output.contains(QString("entry1\n")));
QVERIFY(output.contains(QString("group1/\n"))); QVERIFY(output.contains(QString("group1/\n")));
QVERIFY(output.contains(QString(" entry2\n"))); QVERIFY(!output.contains(QString(" entry2\n")));
output = db->rootGroup()->print(true); output = db->rootGroup()->print(true);
QVERIFY(output.contains(QString("entry1 " + entry1->uuid().toHex() + "\n"))); QVERIFY(output.contains(QString("entry1\n")));
QVERIFY(output.contains(QString("group1/ " + group1->uuid().toHex() + "\n"))); QVERIFY(output.contains(QString("group1/\n")));
QVERIFY(output.contains(QString(" entry2 " + entry2->uuid().toHex() + "\n"))); QVERIFY(output.contains(QString(" entry2\n")));
output = group1->print();
QVERIFY(!output.contains(QString("group1/\n")));
QVERIFY(output.contains(QString("entry2\n")));
delete db; delete db;
} }