~dobey/keeper/cleanup

« back to all changes in this revision

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

  • Committer: Bileto Bot
  • Author(s): Charles Kerr, Xavi Garcia Mena, Xavi Garcia
  • Date: 2017-02-13 08:57:25 UTC
  • mfrom: (126.1.21 keeper-tests-charles)
  • Revision ID: ci-train-bot@canonical.com-20170213085725-rpsp9eg86dk2zlvi
Adds integration tests for restore and restore cancellation.

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

Show diffs side-by-side

added added

removed removed

Lines of Context:
40
40
 
41
41
CommandLineClient::~CommandLineClient() = default;
42
42
 
43
 
void CommandLineClient::run_list_sections(bool remote)
 
43
void CommandLineClient::run_list_sections(bool remote, QString const & storage)
44
44
{
45
 
    QMap<QString, QVariantMap> choices_values;
 
45
    keeper::Items choices_values;
 
46
    keeper::Error error;
46
47
    if(!remote)
47
48
    {
48
 
        choices_values = keeper_client_->getBackupChoices();
 
49
        choices_values = keeper_client_->getBackupChoices(error);
 
50
        check_for_choices_error(error);
49
51
        list_backup_sections(choices_values);
50
52
    }
51
53
    else
52
54
    {
53
 
        choices_values = keeper_client_->getRestoreChoices();
 
55
        choices_values = keeper_client_->getRestoreChoices(storage, error);
 
56
        check_for_choices_error(error);
54
57
        list_restore_sections(choices_values);
55
58
    }
56
59
}
57
60
 
58
 
void CommandLineClient::run_backup(QStringList & sections)
 
61
void CommandLineClient::run_list_storage_accounts()
 
62
{
 
63
    list_storage_accounts(keeper_client_->getStorageAccounts());
 
64
}
 
65
 
 
66
void CommandLineClient::run_backup(QStringList & sections, QString const & storage)
59
67
{
60
68
    auto unhandled_sections = sections;
61
 
    auto choices_values = keeper_client_->getBackupChoices();
 
69
    keeper::Error error;
 
70
    auto choices_values = keeper_client_->getBackupChoices(error);
 
71
    check_for_choices_error(error);
62
72
    QStringList uuids;
63
 
    for(auto iter = choices_values.begin(); iter != choices_values.end() && unhandled_sections.size(); ++iter)
 
73
 
 
74
    auto uuids_choices = choices_values.get_uuids();
 
75
    for(auto iter = uuids_choices.begin(); iter != uuids_choices.end() && unhandled_sections.size(); ++iter)
64
76
    {
65
 
        const auto& values = iter.value();
66
 
        auto iter_values = values.find("type");
67
 
        if (iter_values != values.end())
 
77
        const auto& values = choices_values[(*iter)];
 
78
 
 
79
        if (values.is_valid() && values.get_type() == keeper::Item::FOLDER_VALUE)
68
80
        {
69
 
            if (iter_values.value() == "folder")
 
81
 
 
82
            auto display_name = values.get_display_name();
 
83
            auto index = unhandled_sections.indexOf(display_name);
 
84
            if (index != -1)
70
85
            {
71
 
                auto iter_display_name = values.find("display-name");
72
 
                if (iter_display_name != values.end())
73
 
                {
74
 
                    auto index = unhandled_sections.indexOf((*iter_display_name).toString());
75
 
                    if (index != -1)
76
 
                    {
77
 
                        // we have to backup this section
78
 
                        uuids << iter.key();
79
 
                        unhandled_sections.removeAt(index);
80
 
                        view_->add_task((*iter_display_name).toString(), "waiting", 0.0);
81
 
                    }
82
 
                }
 
86
                // we have to backup this section
 
87
                uuids << (*iter);
 
88
                unhandled_sections.removeAt(index);
 
89
                view_->add_task(display_name, "waiting", 0.0);
83
90
            }
84
91
        }
85
92
    }
99
106
    {
100
107
        keeper_client_->enableBackup(uuid, true);
101
108
    }
102
 
    keeper_client_->startBackup();
 
109
    keeper_client_->startBackup(storage);
103
110
    view_->start_printing_tasks();
104
111
}
105
112
 
106
 
void CommandLineClient::run_restore(QStringList & sections)
 
113
void CommandLineClient::run_restore(QStringList & sections, QString const & storage)
107
114
{
108
115
    auto unhandled_sections = sections;
109
 
    auto choices_values = keeper_client_->getRestoreChoices();
 
116
    keeper::Error error;
 
117
 
 
118
    auto choices_values = keeper_client_->getRestoreChoices(storage, error);
 
119
    check_for_choices_error(error);
110
120
    QStringList uuids;
111
 
    for(auto iter = choices_values.begin(); iter != choices_values.end(); ++iter)
 
121
 
 
122
    auto uuids_choices = choices_values.get_uuids();
 
123
    for(auto iter = uuids_choices.begin(); iter != uuids_choices.end(); ++iter)
112
124
    {
113
 
        const auto& values = iter.value();
 
125
        const auto& values = choices_values[(*iter)];
114
126
 
115
 
        QVariant choice_value;
116
 
        auto has_type = find_choice_value(values, "type", choice_value);
117
 
        if (has_type && choice_value == "folder")
 
127
        if (values.is_valid() && values.get_type() == keeper::Item::FOLDER_VALUE)
118
128
        {
119
 
            QVariant display_name;
120
 
            auto has_display_name = find_choice_value(values, "display-name", display_name);
121
 
            if (!has_display_name)
122
 
                continue;
123
 
 
124
 
            QVariant dir_name;
125
 
            auto has_dir_name = find_choice_value(values, "dir-name", dir_name);
126
 
            if (!has_dir_name)
127
 
                continue;
128
 
 
129
 
            auto section_name = QStringLiteral("%1:%2").arg(display_name.toString()).arg(dir_name.toString());
 
129
            auto display_name = values.get_display_name();
 
130
            auto dir_name = values.get_dir_name();
 
131
 
 
132
            auto section_name = QStringLiteral("%1:%2").arg(display_name).arg(dir_name);
130
133
            auto index = unhandled_sections.indexOf(section_name);
131
134
            if (index != -1)
132
135
            {
133
136
                // we have to restore this section
134
 
                uuids << iter.key();
 
137
                uuids << (*iter);
135
138
                unhandled_sections.removeAt(index);
136
 
                view_->add_task(display_name.toString(), "waiting", 0.0);
 
139
                view_->add_task(display_name, "waiting", 0.0);
137
140
            }
138
141
        }
139
142
    }
152
155
    {
153
156
        keeper_client_->enableRestore(uuid, true);
154
157
    }
155
 
    keeper_client_->startRestore();
 
158
    keeper_client_->startRestore(storage);
156
159
    view_->start_printing_tasks();
157
160
}
158
161
 
159
 
void CommandLineClient::list_backup_sections(QMap<QString, QVariantMap> const & choices_values)
 
162
void CommandLineClient::run_cancel() const
 
163
{
 
164
    keeper_client_->cancel();
 
165
}
 
166
 
 
167
void CommandLineClient::list_backup_sections(keeper::Items const & choices_values)
160
168
{
161
169
    QStringList sections;
162
170
    for(auto iter = choices_values.begin(); iter != choices_values.end(); ++iter)
163
171
    {
164
 
        const auto& values = iter.value();
165
 
 
166
 
        QVariant choice_value;
167
 
        auto has_type = find_choice_value(values, "type", choice_value);
168
 
        if (has_type && choice_value == "folder")
169
 
        {
170
 
            auto has_display_name = find_choice_value(values, "display-name", choice_value);
171
 
            if (has_display_name)
 
172
        if ((*iter).is_valid() && (*iter).get_type() == keeper::Item::FOLDER_VALUE)
 
173
        {
 
174
            sections << (*iter).get_display_name();
 
175
        }
 
176
    }
 
177
    view_->print_sections(sections);
 
178
}
 
179
 
 
180
void CommandLineClient::list_restore_sections(keeper::Items const & choices_values)
 
181
{
 
182
    QMap<QString, QList<keeper::Item>> values_per_dir;
 
183
 
 
184
    for(auto iter = choices_values.begin(); iter != choices_values.end(); ++iter)
 
185
    {
 
186
        if ((*iter).is_valid() && (*iter).get_type() == keeper::Item::FOLDER_VALUE)
 
187
        {
 
188
            auto dir_name = (*iter).get_dir_name();
 
189
            if (!dir_name.isEmpty())
172
190
            {
173
 
                 sections << choice_value.toString();
 
191
                values_per_dir[dir_name].push_back((*iter));
174
192
            }
175
193
        }
176
194
    }
177
 
    view_->print_sections(sections);
178
 
}
179
 
 
180
 
void CommandLineClient::list_restore_sections(QMap<QString, QVariantMap> const & choices_values)
181
 
{
182
 
    QMap<QString, QList<QVariantMap>> values_per_dir;
183
 
 
184
 
    for(auto iter = choices_values.begin(); iter != choices_values.end(); ++iter)
185
 
    {
186
 
        const auto& values = iter.value();
187
 
 
188
 
        QVariant choice_value;
189
 
        auto has_type = find_choice_value(values, "type", choice_value);
190
 
        if (has_type && choice_value == "folder")
191
 
        {
192
 
            auto has_dir_name = find_choice_value(values, "dir-name", choice_value);
193
 
            if (!has_dir_name)
194
 
                continue;
195
 
            values_per_dir[choice_value.toString()].push_back(values);
196
 
        }
197
 
    }
198
195
 
199
196
    QStringList sections;
200
197
    for(auto iter = values_per_dir.begin(); iter != values_per_dir.end(); ++iter)
202
199
        for(auto iter_items = (*iter).begin(); iter_items != (*iter).end(); ++iter_items)
203
200
        {
204
201
            const auto& values = (*iter_items);
205
 
 
206
 
            QVariant choice_value;
207
 
            auto has_type = find_choice_value(values, "type", choice_value);
208
 
            if (has_type && choice_value == "folder")
209
 
            {
210
 
                auto has_display_name = find_choice_value(values, "display-name", choice_value);
211
 
                if (has_display_name)
212
 
                {
213
 
                    sections << QStringLiteral("%1:%2").arg(choice_value.toString()).arg(iter.key());
214
 
                }
215
 
            }
 
202
            sections << QStringLiteral("%1:%2").arg(values.get_display_name()).arg(iter.key());
216
203
        }
217
204
        sections << "";
218
205
    }
219
206
    view_->print_sections(sections);
220
207
}
221
208
 
 
209
void CommandLineClient::list_storage_accounts(QStringList const & accounts)
 
210
{
 
211
    view_->print_sections(accounts);
 
212
}
 
213
 
222
214
void CommandLineClient::on_progress_changed()
223
215
{
224
216
    view_->progress_changed(keeper_client_->progress());
245
237
    value = (*iter);
246
238
    return true;
247
239
}
 
240
 
 
241
void CommandLineClient::check_for_choices_error(keeper::Error error)
 
242
{
 
243
    if (error != keeper::Error::OK)
 
244
    {
 
245
        // an error occurred
 
246
        auto error_message = QStringLiteral("Error obtaining keeper choices: %1").arg(view_->get_error_string(error));
 
247
        view_->print_error_message(error_message);
 
248
        return;
 
249
    }
 
250
}