2
* Copyright (C) 2019 KeePassXC Team <team@keepassxc.org>
4
* This program is free software: you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation, either version 2 or (at your option)
7
* version 3 of the License.
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program. If not, see <http://www.gnu.org/licenses/>.
20
#include "TextStream.h"
22
#include "format/CsvExporter.h"
24
#include <QCommandLineParser>
26
const QCommandLineOption Export::FormatOption = QCommandLineOption(
29
QObject::tr("Format to use when exporting. Available choices are 'xml' or 'csv'. Defaults to 'xml'."),
30
QStringLiteral("xml|csv"));
34
name = QStringLiteral("export");
35
options.append(Export::FormatOption);
36
description = QObject::tr("Exports the content of a database to standard output in the specified format.");
39
int Export::executeWithDatabase(QSharedPointer<Database> database, QSharedPointer<QCommandLineParser> parser)
41
TextStream out(Utils::STDOUT.device());
42
auto& err = Utils::STDERR;
44
QString format = parser->value(Export::FormatOption);
45
if (format.isEmpty() || format.startsWith(QStringLiteral("xml"), Qt::CaseInsensitive)) {
48
if (!database->extract(xmlData, &errorMessage)) {
49
err << QObject::tr("Unable to export database to XML: %1").arg(errorMessage) << endl;
52
out.write(xmlData.constData());
53
} else if (format.startsWith(QStringLiteral("csv"), Qt::CaseInsensitive)) {
54
CsvExporter csvExporter;
55
out << csvExporter.exportDatabase(database);
57
err << QObject::tr("Unsupported format %1").arg(format) << endl;