~ken-vandine/keeper/ual_api_change

« back to all changes in this revision

Viewing changes to src/cli/command-line.cpp

Changes to command line client and some bugs fixed.

Approved by unity-api-1-bot, Charles Kerr.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2016 Canonical, Ltd.
 
3
 *
 
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.
 
7
 *
 
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.
 
12
 *
 
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/>.
 
15
 *
 
16
 * Authors:
 
17
 *     Xavi Garcia <xavi.garcia.mena@canonical.com>
 
18
 */
 
19
#include "command-line.h"
 
20
 
 
21
#include <QCommandLineParser>
 
22
#include <QDebug>
 
23
 
 
24
#include <iostream>
 
25
 
 
26
namespace
 
27
{
 
28
    // arguments
 
29
    constexpr const char ARGUMENT_LIST_SECTIONS[] = "list-sections";
 
30
    constexpr const char ARGUMENT_BACKUP[]        = "backup";
 
31
    constexpr const char ARGUMENT_RESTORE[]       = "restore";
 
32
 
 
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";
 
37
 
 
38
    // options
 
39
    constexpr const char OPTION_STORAGE[]          = "storage";
 
40
    constexpr const char OPTION_SECTIONS[]         = "sections";
 
41
 
 
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";
 
45
}
 
46
 
 
47
CommandLineParser::CommandLineParser()
 
48
    : parser_(new QCommandLineParser)
 
49
{
 
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));
 
56
}
 
57
 
 
58
bool CommandLineParser::parse(QStringList const & arguments, QCoreApplication const & app, CommandLineParser::CommandArgs & cmd_args)
 
59
{
 
60
    parser_->parse(arguments);
 
61
    const QStringList args = parser_->positionalArguments();
 
62
    if (!check_number_of_args(args))
 
63
    {
 
64
        return false;
 
65
    }
 
66
 
 
67
    if (args.size() == 1)
 
68
    {
 
69
        // if something fails at the process call the process exists
 
70
        if (args.at(0) == ARGUMENT_LIST_SECTIONS)
 
71
        {
 
72
            return handle_list_sections(app, cmd_args);
 
73
        }
 
74
        else if (args.at(0) == ARGUMENT_BACKUP)
 
75
        {
 
76
            return handle_backup(app, cmd_args);
 
77
        }
 
78
        else if (args.at(0) == ARGUMENT_RESTORE)
 
79
        {
 
80
            return handle_restore(app, cmd_args);
 
81
        }
 
82
        else
 
83
        {
 
84
            std::cerr << "Bad argument." << std::endl;
 
85
            std::cerr << parser_->errorText().toStdString() << std::endl;
 
86
            exit(1);
 
87
        }
 
88
    }
 
89
    else
 
90
    {
 
91
        qDebug() << "More or none arguments";
 
92
        // maybe we have the version or help options
 
93
        parser_->process(app);
 
94
    }
 
95
 
 
96
    return false;
 
97
}
 
98
 
 
99
bool CommandLineParser::handle_list_sections(QCoreApplication const & app, CommandLineParser::CommandArgs & cmd_args)
 
100
{
 
101
    parser_->clearPositionalArguments();
 
102
    parser_->addPositionalArgument(ARGUMENT_LIST_SECTIONS, QCoreApplication::translate("main", ARGUMENT_LIST_SECTIONS_DESCRIPTION));
 
103
 
 
104
    parser_->addOptions({
 
105
            {{"r", OPTION_STORAGE},
 
106
                QCoreApplication::translate("main", OPTION_STORAGE_DESCRIPTION)},
 
107
        });
 
108
    parser_->process(app);
 
109
 
 
110
    // it didn't exit... we're good
 
111
    cmd_args.sections.clear();
 
112
    cmd_args.storage.clear();
 
113
    if (parser_->isSet(OPTION_STORAGE))
 
114
    {
 
115
        cmd_args.cmd = CommandLineParser::Command::LIST_REMOTE_SECTIONS;
 
116
    }
 
117
    else
 
118
    {
 
119
        cmd_args.cmd = CommandLineParser::Command::LIST_LOCAL_SECTIONS;
 
120
    }
 
121
 
 
122
    return true;
 
123
}
 
124
 
 
125
bool CommandLineParser::handle_backup(QCoreApplication const & app, CommandLineParser::CommandArgs & cmd_args)
 
126
{
 
127
    parser_->clearPositionalArguments();
 
128
    parser_->addPositionalArgument(ARGUMENT_BACKUP, QCoreApplication::translate("main", ARGUMENT_BACKUP_DESCRIPTION));
 
129
 
 
130
    parser_->addOptions({
 
131
            {{"s", OPTION_SECTIONS},
 
132
                QCoreApplication::translate("main", OPTION_SECTIONS_DESCRIPTION),
 
133
                QCoreApplication::translate("main", OPTION_SECTIONS_DESCRIPTION)
 
134
            },
 
135
        });
 
136
    parser_->process(app);
 
137
 
 
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))
 
143
    {
 
144
        std::cerr << "You need to specify some sections to run a backup." << std::endl;
 
145
        return false;
 
146
    }
 
147
    cmd_args.sections = parser_->value(OPTION_SECTIONS).split(',');
 
148
 
 
149
    return true;
 
150
}
 
151
 
 
152
bool CommandLineParser::handle_restore(QCoreApplication const & app, CommandLineParser::CommandArgs & cmd_args)
 
153
{
 
154
    parser_->clearPositionalArguments();
 
155
    parser_->addPositionalArgument(ARGUMENT_RESTORE, QCoreApplication::translate("main", ARGUMENT_RESTORE_DESCRIPTION));
 
156
 
 
157
    parser_->addOptions({
 
158
            {{"s", OPTION_SECTIONS},
 
159
                QCoreApplication::translate("main", OPTION_SECTIONS_DESCRIPTION),
 
160
                QCoreApplication::translate("main", OPTION_SECTIONS_DESCRIPTION)
 
161
            },
 
162
        });
 
163
    parser_->process(app);
 
164
 
 
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))
 
170
    {
 
171
        std::cerr << "You need to specify some sections to run a restore." << std::endl;
 
172
        return false;
 
173
    }
 
174
    cmd_args.sections = parser_->value(OPTION_SECTIONS).split(',');
 
175
 
 
176
    return true;
 
177
}
 
178
 
 
179
bool CommandLineParser::check_number_of_args(QStringList const & args)
 
180
{
 
181
    if (args.size() > 1)
 
182
    {
 
183
        std::cerr << "Please, pass only one argument." << std::endl;
 
184
        std::cerr << parser_->helpText().toStdString() << std::endl;
 
185
        return false;
 
186
    }
 
187
    return true;
 
188
}