mirror of
https://github.com/keepassxreboot/keepassxc.git
synced 2025-07-24 15:25:31 -04:00
CLI: Export database as CSV
* Changed `Extract` to `Export` to support additional formats * Allow database expot as CSV. Added a `--format` option to the `Export` command for that, which defaults to xml, so the current behavior is unchanged. *The `CsvExporter` had to be refactored a bit, but nothing major. It can now print to a file or return a string.
This commit is contained in:
parent
547c246e88
commit
77fcde875e
11 changed files with 157 additions and 89 deletions
|
@ -35,21 +35,22 @@ bool CsvExporter::exportDatabase(const QString& filename, const QSharedPointer<c
|
|||
|
||||
bool CsvExporter::exportDatabase(QIODevice* device, const QSharedPointer<const Database>& db)
|
||||
{
|
||||
QString header;
|
||||
addColumn(header, "Group");
|
||||
addColumn(header, "Title");
|
||||
addColumn(header, "Username");
|
||||
addColumn(header, "Password");
|
||||
addColumn(header, "URL");
|
||||
addColumn(header, "Notes");
|
||||
header.append("\n");
|
||||
|
||||
if (device->write(header.toUtf8()) == -1) {
|
||||
if (device->write(exportHeader().toUtf8()) == -1) {
|
||||
m_error = device->errorString();
|
||||
return false;
|
||||
}
|
||||
|
||||
return writeGroup(device, db->rootGroup());
|
||||
if (device->write(exportGroup(db->rootGroup()).toUtf8()) == -1) {
|
||||
m_error = device->errorString();
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
QString CsvExporter::exportDatabase(const QSharedPointer<const Database>& db)
|
||||
{
|
||||
return exportHeader() + exportGroup(db->rootGroup());
|
||||
}
|
||||
|
||||
QString CsvExporter::errorString() const
|
||||
|
@ -57,8 +58,21 @@ QString CsvExporter::errorString() const
|
|||
return m_error;
|
||||
}
|
||||
|
||||
bool CsvExporter::writeGroup(QIODevice* device, const Group* group, QString groupPath)
|
||||
QString CsvExporter::exportHeader()
|
||||
{
|
||||
QString header;
|
||||
addColumn(header, "Group");
|
||||
addColumn(header, "Title");
|
||||
addColumn(header, "Username");
|
||||
addColumn(header, "Password");
|
||||
addColumn(header, "URL");
|
||||
addColumn(header, "Notes");
|
||||
return header + QString("\n");
|
||||
}
|
||||
|
||||
QString CsvExporter::exportGroup(const Group* group, QString groupPath)
|
||||
{
|
||||
QString response;
|
||||
if (!groupPath.isEmpty()) {
|
||||
groupPath.append("/");
|
||||
}
|
||||
|
@ -76,21 +90,15 @@ bool CsvExporter::writeGroup(QIODevice* device, const Group* group, QString grou
|
|||
addColumn(line, entry->notes());
|
||||
|
||||
line.append("\n");
|
||||
|
||||
if (device->write(line.toUtf8()) == -1) {
|
||||
m_error = device->errorString();
|
||||
return false;
|
||||
}
|
||||
response.append(line);
|
||||
}
|
||||
|
||||
const QList<Group*>& children = group->children();
|
||||
for (const Group* child : children) {
|
||||
if (!writeGroup(device, child, groupPath)) {
|
||||
return false;
|
||||
}
|
||||
response.append(exportGroup(child, groupPath));
|
||||
}
|
||||
|
||||
return true;
|
||||
return response;
|
||||
}
|
||||
|
||||
void CsvExporter::addColumn(QString& str, const QString& column)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue