diff options
Diffstat (limited to 'src/cli/Show.cpp')
-rw-r--r-- | src/cli/Show.cpp | 84 |
1 files changed, 61 insertions, 23 deletions
diff --git a/src/cli/Show.cpp b/src/cli/Show.cpp index c212a9d61..54561b1f7 100644 --- a/src/cli/Show.cpp +++ b/src/cli/Show.cpp @@ -21,50 +21,88 @@ #include "Show.h" #include <QCommandLineParser> -#include <QCoreApplication> -#include <QStringList> #include <QTextStream> #include "core/Database.h" #include "core/Entry.h" #include "core/Group.h" -#include "keys/CompositeKey.h" -#include "cli/PasswordInput.h" -int Show::execute(int argc, char** argv) +Show::Show() +{ + name = QString("show"); + description = QObject::tr("Show an entry's information."); +} + +Show::~Show() +{ +} + +int Show::execute(const QStringList& arguments) { - QCoreApplication app(argc, argv); QTextStream out(stdout); QCommandLineParser parser; - parser.setApplicationDescription(QCoreApplication::translate("main", "Show a password.")); - parser.addPositionalArgument("database", QCoreApplication::translate("main", "Path of the database.")); - parser.addPositionalArgument("entry", QCoreApplication::translate("main", "Name of the entry to show.")); - parser.process(app); + parser.setApplicationDescription(this->description); + parser.addPositionalArgument("database", QObject::tr("Path of the database.")); + QCommandLineOption keyFile(QStringList() << "k" + << "key-file", + QObject::tr("Key file of the database."), + QObject::tr("path")); + parser.addOption(keyFile); + QCommandLineOption attributes(QStringList() << "a" + << "attributes", + QObject::tr("Names of the attributes to show. " + "This option can be specified more than once, with each attribute shown one-per-line in the given order. " + "If no attributes are specified, a summary of the default attributes is given."), + QObject::tr("attribute")); + parser.addOption(attributes); + parser.addPositionalArgument("entry", QObject::tr("Name of the entry to show.")); + parser.process(arguments); const QStringList args = parser.positionalArguments(); 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> "; - out.flush(); - - QString line = PasswordInput::getPassword(); - CompositeKey key = CompositeKey::readFromLine(line); - - Database* db = Database::openDatabaseFile(args.at(0), key); + Database* db = Database::unlockFromStdin(args.at(0), parser.value(keyFile)); if (db == nullptr) { return EXIT_FAILURE; } - QString entryId = args.at(1); - Entry* entry = db->rootGroup()->findEntry(entryId); + return this->showEntry(db, parser.values(attributes), args.at(1)); +} + +int Show::showEntry(Database* database, QStringList attributes, QString entryPath) +{ + + QTextStream inputTextStream(stdin, QIODevice::ReadOnly); + QTextStream outputTextStream(stdout, QIODevice::WriteOnly); + + Entry* entry = database->rootGroup()->findEntry(entryPath); if (!entry) { - qCritical("Entry %s not found.", qPrintable(entryId)); + qCritical("Could not find entry with path %s.", qPrintable(entryPath)); return EXIT_FAILURE; } - out << entry->password() << "\n"; - return EXIT_SUCCESS; + // If no attributes specified, output the default attribute set. + bool showAttributeNames = attributes.isEmpty(); + if (attributes.isEmpty()) { + attributes = EntryAttributes::DefaultAttributes; + } + + // Iterate over the attributes and output them line-by-line. + bool sawUnknownAttribute = false; + for (QString attribute : attributes) { + if (!entry->attributes()->contains(attribute)) { + sawUnknownAttribute = true; + qCritical("ERROR: unknown attribute '%s'.", qPrintable(attribute)); + continue; + } + if (showAttributeNames) { + outputTextStream << attribute << ": "; + } + outputTextStream << entry->resolveMultiplePlaceholders(entry->attributes()->value(attribute)) << endl; + } + return sawUnknownAttribute ? EXIT_FAILURE : EXIT_SUCCESS; } |