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 <cyphar@cyphar.com>
This commit is contained in:
Aleksa Sarai 2017-12-17 11:14:39 +11:00 committed by louib
parent d8f408ee83
commit 8e231dfa95
3 changed files with 38 additions and 9 deletions

View File

@ -49,6 +49,13 @@ int Show::execute(QStringList arguments)
QObject::tr("Key file of the database."), QObject::tr("Key file of the database."),
QObject::tr("path")); QObject::tr("path"));
parser.addOption(keyFile); 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.addPositionalArgument("entry", QObject::tr("Name of the entry to show."));
parser.process(arguments); parser.process(arguments);
@ -63,10 +70,10 @@ int Show::execute(QStringList arguments)
return EXIT_FAILURE; 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); QTextStream inputTextStream(stdin, QIODevice::ReadOnly);
@ -78,10 +85,24 @@ int Show::showEntry(Database* database, QString entryPath)
return EXIT_FAILURE; return EXIT_FAILURE;
} }
outputTextStream << " title: " << entry->title() << endl; // If no attributes specified, output the default attribute set.
outputTextStream << "username: " << entry->username() << endl; bool showAttributeNames = attributes.isEmpty();
outputTextStream << "password: " << entry->password() << endl; if (attributes.isEmpty()) {
outputTextStream << " URL: " << entry->url() << endl; attributes = EntryAttributes::DefaultAttributes;
outputTextStream << " Notes: " << entry->notes() << endl; }
return EXIT_SUCCESS;
// 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;
} }

View File

@ -26,7 +26,7 @@ public:
Show(); Show();
~Show(); ~Show();
int execute(QStringList arguments); int execute(QStringList arguments);
int showEntry(Database* database, QString entryPath); int showEntry(Database* database, QStringList attributes, QString entryPath);
}; };
#endif // KEEPASSXC_SHOW_H #endif // KEEPASSXC_SHOW_H

View File

@ -96,6 +96,14 @@ Specify the title of the entry.
Perform advanced analysis on the password. Perform advanced analysis on the password.
.SS "Show options"
.IP "-a, --attributes <attribute>..."
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 .SH REPORTING BUGS
Bugs and feature requests can be reported on GitHub at https://github.com/keepassxreboot/keepassxc/issues. Bugs and feature requests can be reported on GitHub at https://github.com/keepassxreboot/keepassxc/issues.