2
* Copyright (C) 2016 Canonical, Ltd.
4
* This program is free software: you can redistribute it and/or modify it
5
* under the terms of the GNU General Public License version 3, as published
6
* by the Free Software Foundation.
8
* This program is distributed in the hope that it will be useful, but
9
* WITHOUT ANY WARRANTY; without even the implied warranties of
10
* MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11
* PURPOSE. See the GNU General Public License for more details.
13
* You should have received a copy of the GNU General Public License along
14
* with this program. If not, see <http://www.gnu.org/licenses/>.
17
* Xavi Garcia <xavi.garcia.mena@canonical.com>
19
#include "command-line.h"
21
#include <QCommandLineParser>
29
constexpr const char ARGUMENT_LIST_SECTIONS[] = "list-sections";
30
constexpr const char ARGUMENT_BACKUP[] = "backup";
31
constexpr const char ARGUMENT_RESTORE[] = "restore";
33
// argument descriptions
34
constexpr const char ARGUMENT_LIST_SECTIONS_DESCRIPTION[] = "List the sections available to backup";
35
constexpr const char ARGUMENT_BACKUP_DESCRIPTION[] = "Starts a backup";
36
constexpr const char ARGUMENT_RESTORE_DESCRIPTION[] = "Starts a restore";
39
constexpr const char OPTION_STORAGE[] = "storage";
40
constexpr const char OPTION_SECTIONS[] = "sections";
42
// option descriptions
43
constexpr const char OPTION_STORAGE_DESCRIPTION[] = "Lists the available sections stored at the storage";
44
constexpr const char OPTION_SECTIONS_DESCRIPTION[] = "Lists the sections to backup or restore";
47
CommandLineParser::CommandLineParser()
48
: parser_(new QCommandLineParser)
50
parser_->setApplicationDescription("Keeper command line client");
51
parser_->addHelpOption();
52
parser_->addVersionOption();
53
parser_->addPositionalArgument(ARGUMENT_LIST_SECTIONS, QCoreApplication::translate("main", ARGUMENT_LIST_SECTIONS_DESCRIPTION));
54
parser_->addPositionalArgument(ARGUMENT_BACKUP, QCoreApplication::translate("main", ARGUMENT_BACKUP_DESCRIPTION));
55
parser_->addPositionalArgument(ARGUMENT_RESTORE, QCoreApplication::translate("main", ARGUMENT_RESTORE_DESCRIPTION));
58
bool CommandLineParser::parse(QStringList const & arguments, QCoreApplication const & app, CommandLineParser::CommandArgs & cmd_args)
60
parser_->parse(arguments);
61
const QStringList args = parser_->positionalArguments();
62
if (!check_number_of_args(args))
69
// if something fails at the process call the process exists
70
if (args.at(0) == ARGUMENT_LIST_SECTIONS)
72
return handle_list_sections(app, cmd_args);
74
else if (args.at(0) == ARGUMENT_BACKUP)
76
return handle_backup(app, cmd_args);
78
else if (args.at(0) == ARGUMENT_RESTORE)
80
return handle_restore(app, cmd_args);
84
std::cerr << "Bad argument." << std::endl;
85
std::cerr << parser_->errorText().toStdString() << std::endl;
91
qDebug() << "More or none arguments";
92
// maybe we have the version or help options
93
parser_->process(app);
99
bool CommandLineParser::handle_list_sections(QCoreApplication const & app, CommandLineParser::CommandArgs & cmd_args)
101
parser_->clearPositionalArguments();
102
parser_->addPositionalArgument(ARGUMENT_LIST_SECTIONS, QCoreApplication::translate("main", ARGUMENT_LIST_SECTIONS_DESCRIPTION));
104
parser_->addOptions({
105
{{"r", OPTION_STORAGE},
106
QCoreApplication::translate("main", OPTION_STORAGE_DESCRIPTION)},
108
parser_->process(app);
110
// it didn't exit... we're good
111
cmd_args.sections.clear();
112
cmd_args.storage.clear();
113
if (parser_->isSet(OPTION_STORAGE))
115
cmd_args.cmd = CommandLineParser::Command::LIST_REMOTE_SECTIONS;
119
cmd_args.cmd = CommandLineParser::Command::LIST_LOCAL_SECTIONS;
125
bool CommandLineParser::handle_backup(QCoreApplication const & app, CommandLineParser::CommandArgs & cmd_args)
127
parser_->clearPositionalArguments();
128
parser_->addPositionalArgument(ARGUMENT_BACKUP, QCoreApplication::translate("main", ARGUMENT_BACKUP_DESCRIPTION));
130
parser_->addOptions({
131
{{"s", OPTION_SECTIONS},
132
QCoreApplication::translate("main", OPTION_SECTIONS_DESCRIPTION),
133
QCoreApplication::translate("main", OPTION_SECTIONS_DESCRIPTION)
136
parser_->process(app);
138
// it didn't exit... we're good
139
cmd_args.sections.clear();
140
cmd_args.storage.clear();
141
cmd_args.cmd = CommandLineParser::Command::BACKUP;
142
if (!parser_->isSet(OPTION_SECTIONS))
144
std::cerr << "You need to specify some sections to run a backup." << std::endl;
147
cmd_args.sections = parser_->value(OPTION_SECTIONS).split(',');
152
bool CommandLineParser::handle_restore(QCoreApplication const & app, CommandLineParser::CommandArgs & cmd_args)
154
parser_->clearPositionalArguments();
155
parser_->addPositionalArgument(ARGUMENT_RESTORE, QCoreApplication::translate("main", ARGUMENT_RESTORE_DESCRIPTION));
157
parser_->addOptions({
158
{{"s", OPTION_SECTIONS},
159
QCoreApplication::translate("main", OPTION_SECTIONS_DESCRIPTION),
160
QCoreApplication::translate("main", OPTION_SECTIONS_DESCRIPTION)
163
parser_->process(app);
165
// it didn't exit... we're good
166
cmd_args.sections = QStringList();
167
cmd_args.storage = QString();
168
cmd_args.cmd = CommandLineParser::Command::RESTORE;
169
if (!parser_->isSet(OPTION_SECTIONS))
171
std::cerr << "You need to specify some sections to run a restore." << std::endl;
174
cmd_args.sections = parser_->value(OPTION_SECTIONS).split(',');
179
bool CommandLineParser::check_number_of_args(QStringList const & args)
183
std::cerr << "Please, pass only one argument." << std::endl;
184
std::cerr << parser_->helpText().toStdString() << std::endl;