CLI: Add netrc formatted option to show command. - #13387
Conversation
There was a problem hiding this comment.
Pull request overview
This PR adds a new keepassxc-cli show --format-netrc option to emit a .netrc-style representation of a single entry (URL/username/password), intended as an ephemeral alternative to exporting the entire database.
Changes:
- Added
--format-netrcoption to theshowcommand and adjusted output behavior to suppress attachments/TOTP output in netrc mode. - Implemented
.netrc-style field naming (machine,login,password) when netrc mode is enabled. - Extended
TestCli::testShow()with coverage asserting stable netrc output even when othershowoptions are also provided.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
src/cli/Show.h |
Declares the new --format-netrc CLI option. |
src/cli/Show.cpp |
Implements netrc-mode output and interaction with existing show options. |
tests/TestCli.cpp |
Adds tests validating the new netrc output behavior. |
bba073b to
106d756
Compare
|
|
||
| // First we do the special case for showNetrc. | ||
| if (showNetrcFormat) { | ||
| const QString entryUrl = entry->resolveMultiplePlaceholders(entry->attributes()->value(EntryAttributes::URLKey)); |
There was a problem hiding this comment.
I broke this out so I could parse it to get just the UrlHost but I wasn't able to get the code working for it. I see PlaceholderType::UrlHost in src/core/Entry.h and I saw UrlFormattingOption in qurl.h (in Qt) but I wasn't able to get either one to work for me.
| bool showTotp = parser->isSet(Show::TotpOption); | ||
| bool showProtectedAttributes = parser->isSet(Show::ProtectedAttributesOption); | ||
| bool showAllAttributes = parser->isSet(Show::AllAttributesOption); | ||
| bool showNetrcFormat = parser->isSet(Show::NetrcOption); | ||
| QStringList attributes = parser->values(Show::AttributesOption); |
| const QString entryUrl = | ||
| entry->resolveMultiplePlaceholders(entry->attributes()->value(EntryAttributes::URLKey)); | ||
| if (entryUrl.isEmpty()) { | ||
| out << QString("default "); | ||
| } else { | ||
| out << QString("machine ") << entryUrl << QString(" "); | ||
| } |
| QByteArray expectedNetrcOutput = QByteArray("machine http://www.somesite.com/ " | ||
| "login \"User Name\" " | ||
| "password \"Password\"\n"); |
| const QString entryUrl = | ||
| entry->resolveMultiplePlaceholders(entry->attributes()->value(EntryAttributes::URLKey)); |
There was a problem hiding this comment.
You will want to put this into a QUrl::fromUserInput then QUrl::toDisplayString with the options
QUrl::RemoveAuthority | QUrl::RemovePath | QUrl::RemoveQuery | QUrl::RemoveFragment | QUrl::StripTrailingSlash
Or force a construction using the schema + host
Fixes #13380
Testing strategy
Added tests to the TestCli::testShow() test case.
Type of change
This follows some comments on #13381 which pointed out that it's exposing the entire database for the one line that's needed, so it'd be better to be an option in 'show'. Also because it's meant to be ephemeral, not a full dump of all the data.