[CLI] Add Option to show all attributes (Show command) (#8256)

* Adding --all option to Show
This commit is contained in:
louib 2022-08-20 22:38:58 -04:00 committed by GitHub
parent aa839e2619
commit 15b9e82f93
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 50 additions and 7 deletions

View file

@ -32,6 +32,9 @@ const QCommandLineOption Show::ProtectedAttributesOption =
<< "show-protected",
QObject::tr("Show the protected attributes in clear text."));
const QCommandLineOption Show::AllAttributesOption =
QCommandLineOption(QStringList() << "all", QObject::tr("Show all the attributes of the entry."));
const QCommandLineOption Show::AttachmentsOption =
QCommandLineOption(QStringList() << "show-attachments", QObject::tr("Show the attachments of the entry."));
@ -51,6 +54,7 @@ Show::Show()
options.append(Show::TotpOption);
options.append(Show::AttributesOption);
options.append(Show::ProtectedAttributesOption);
options.append(Show::AllAttributesOption);
options.append(Show::AttachmentsOption);
positionalArguments.append({QString("entry"), QObject::tr("Name of the entry to show."), QString("")});
}
@ -64,6 +68,7 @@ int Show::executeWithDatabase(QSharedPointer<Database> database, QSharedPointer<
const QString& entryPath = args.at(1);
bool showTotp = parser->isSet(Show::TotpOption);
bool showProtectedAttributes = parser->isSet(Show::ProtectedAttributesOption);
bool showAllAttributes = parser->isSet(Show::AllAttributesOption);
QStringList attributes = parser->values(Show::AttributesOption);
Entry* entry = database->rootGroup()->findEntryByPath(entryPath);
@ -77,9 +82,24 @@ int Show::executeWithDatabase(QSharedPointer<Database> database, QSharedPointer<
return EXIT_FAILURE;
}
// If no attributes specified, output the default attribute set.
bool showDefaultAttributes = attributes.isEmpty() && !showTotp;
if (showDefaultAttributes) {
bool attributesWereSpecified = true;
if (showAllAttributes) {
attributesWereSpecified = false;
attributes = EntryAttributes::DefaultAttributes;
for (QString fieldName : Utils::EntryFieldNames) {
attributes.append(fieldName);
}
// Adding the custom attributes after the default attributes so that
// the default attributes are always shown first.
for (QString attributeName : entry->attributes()->keys()) {
if (EntryAttributes::DefaultAttributes.contains(attributeName)) {
continue;
}
attributes.append(attributeName);
}
} else if (attributes.isEmpty() && !showTotp) {
// If no attributes are specified, output the default attribute set.
attributesWereSpecified = false;
attributes = EntryAttributes::DefaultAttributes;
for (QString fieldName : Utils::EntryFieldNames) {
attributes.append(fieldName);
@ -90,7 +110,7 @@ int Show::executeWithDatabase(QSharedPointer<Database> database, QSharedPointer<
bool encounteredError = false;
for (const QString& attributeName : asConst(attributes)) {
if (Utils::EntryFieldNames.contains(attributeName)) {
if (showDefaultAttributes) {
if (!attributesWereSpecified) {
out << attributeName << ": ";
}
out << Utils::getTopLevelField(entry, attributeName) << endl;
@ -110,10 +130,10 @@ int Show::executeWithDatabase(QSharedPointer<Database> database, QSharedPointer<
continue;
}
QString canonicalName = attrs[0];
if (showDefaultAttributes) {
if (!attributesWereSpecified) {
out << canonicalName << ": ";
}
if (entry->attributes()->isProtected(canonicalName) && showDefaultAttributes && !showProtectedAttributes) {
if (entry->attributes()->isProtected(canonicalName) && !attributesWereSpecified && !showProtectedAttributes) {
out << "PROTECTED" << endl;
} else {
out << entry->resolveMultiplePlaceholders(entry->attributes()->value(canonicalName)) << endl;

View file

@ -28,6 +28,7 @@ public:
int executeWithDatabase(QSharedPointer<Database> db, QSharedPointer<QCommandLineParser> parser) override;
static const QCommandLineOption TotpOption;
static const QCommandLineOption AllAttributesOption;
static const QCommandLineOption AttributesOption;
static const QCommandLineOption ProtectedAttributesOption;
static const QCommandLineOption AttachmentsOption;