-
Notifications
You must be signed in to change notification settings - Fork 785
Expand file tree
/
Copy pathsuspend.cpp
More file actions
110 lines (90 loc) · 3.57 KB
/
suspend.cpp
File metadata and controls
110 lines (90 loc) · 3.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
* Copyright (C) Canonical, Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "suspend.h"
#include "common_cli.h"
#include "animated_spinner.h"
#include "common_callbacks.h"
#include <multipass/cli/argparser.h>
#include <multipass/constants.h>
#include <multipass/settings/settings.h>
namespace mp = multipass;
namespace cmd = multipass::cmd;
mp::ReturnCodeVariant cmd::Suspend::run(mp::ArgParser* parser)
{
auto ret = parse_args(parser);
if (ret != ParseCode::Ok)
{
return parser->returnCodeFrom(ret);
}
auto on_success = [](mp::SuspendReply& reply) -> ReturnCodeVariant { return ReturnCode::Ok; };
AnimatedSpinner spinner{cout, term->cout_is_live()};
auto on_failure = [this, &spinner](grpc::Status& status) -> ReturnCodeVariant {
spinner.stop();
return standard_failure_handler_for(name(), cerr, status);
};
spinner.start(instance_action_message_for(request.instance_names(), "Suspending "));
request.set_verbosity_level(parser->verbosityLevel());
return dispatch(&RpcMethod::suspend,
request,
on_success,
on_failure,
make_logging_spinner_callback<SuspendRequest, SuspendReply>(spinner, cerr));
}
std::string cmd::Suspend::name() const
{
return "suspend";
}
QString cmd::Suspend::short_help() const
{
return QStringLiteral("Suspend running instances");
}
QString cmd::Suspend::description() const
{
return QStringLiteral("Suspend the named instances, if running. Exits with\n"
"return code 0 if successful.");
}
mp::ParseCode cmd::Suspend::parse_args(mp::ArgParser* parser)
{
const auto petenv_name = MP_SETTINGS.get(petenv_key);
const auto& [description, syntax] =
petenv_name.isEmpty()
? std::make_pair(QString{"Names of instances to suspend."},
QString{"<name> [<name> ...]"})
: std::make_pair(QString{"Names of instances to suspend. If omitted, and without the "
"--all option, '%1' will be assumed."}
.arg(petenv_name),
QString{"[<name> ...]"});
parser->addPositionalArgument("name", description, syntax);
QCommandLineOption all_option("all", "Suspend all instances");
parser->addOptions({all_option});
auto status = parser->commandParse(this);
if (status != ParseCode::Ok)
return status;
auto parse_code =
check_for_name_and_all_option_conflict(parser,
cerr,
/*allow_empty=*/!petenv_name.isEmpty());
if (parse_code != ParseCode::Ok)
{
if (petenv_name.isEmpty() && parser->positionalArguments().isEmpty())
fmt::print(cerr, "Note: the primary instance is disabled.\n");
return parse_code;
}
request.mutable_instance_names()->CopyFrom(
add_instance_names(parser, /*default_name=*/petenv_name.toStdString()));
return status;
}