From 8e231dfa95545a084eb36e1ca38a46a29658717b Mon Sep 17 00:00:00 2001 From: Aleksa Sarai Date: Sun, 17 Dec 2017 11:14:39 +1100 Subject: [PATCH] cli: show: add --attributes flag (#1289) In order for scripting to be much simpler with `keepassxc-cli show`, provide a simple --attributesk API which effectively is just a CLI interface for entry->attributes()->value(...). This allows for more extensibility and prevents changes in our output formatting from breaking existing users of keepassxc-cli (if they use --attributes). Signed-off-by: Aleksa Sarai --- src/cli/Show.cpp | 37 +++++++++++++++++++++++++++++-------- src/cli/Show.h | 2 +- src/cli/keepassxc-cli.1 | 8 ++++++++ 3 files changed, 38 insertions(+), 9 deletions(-) diff --git a/src/cli/Show.cpp b/src/cli/Show.cpp index d137f74df..43a9a8376 100644 --- a/src/cli/Show.cpp +++ b/src/cli/Show.cpp @@ -49,6 +49,13 @@ int Show::execute(QStringList arguments) 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); @@ -63,10 +70,10 @@ int Show::execute(QStringList arguments) return EXIT_FAILURE; } - return this->showEntry(db, args.at(1)); + return this->showEntry(db, parser.values(attributes), args.at(1)); } -int Show::showEntry(Database* database, QString entryPath) +int Show::showEntry(Database* database, QStringList attributes, QString entryPath) { QTextStream inputTextStream(stdin, QIODevice::ReadOnly); @@ -78,10 +85,24 @@ int Show::showEntry(Database* database, QString entryPath) return EXIT_FAILURE; } - 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; + // 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->attributes()->value(attribute) << endl; + } + return sawUnknownAttribute ? EXIT_FAILURE : EXIT_SUCCESS; } diff --git a/src/cli/Show.h b/src/cli/Show.h index f3bc55e60..f2caefdbf 100644 --- a/src/cli/Show.h +++ b/src/cli/Show.h @@ -26,7 +26,7 @@ public: Show(); ~Show(); int execute(QStringList arguments); - int showEntry(Database* database, QString entryPath); + int showEntry(Database* database, QStringList attributes, QString entryPath); }; #endif // KEEPASSXC_SHOW_H diff --git a/src/cli/keepassxc-cli.1 b/src/cli/keepassxc-cli.1 index 37127abc7..a9145b27e 100644 --- a/src/cli/keepassxc-cli.1 +++ b/src/cli/keepassxc-cli.1 @@ -96,6 +96,14 @@ Specify the title of the entry. Perform advanced analysis on the password. +.SS "Show options" + +.IP "-a, --attributes ..." +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. + + .SH REPORTING BUGS Bugs and feature requests can be reported on GitHub at https://github.com/keepassxreboot/keepassxc/issues.