• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mendersoftware / mender / 1012473002

21 Sep 2023 02:17PM UTC coverage: 78.107% (-0.3%) from 78.44%
1012473002

push

gitlab-ci

lluiscampos
chore: log stdout output on errors when parsing UM yes/no commands

Signed-off-by: Lluis Campos <lluis.campos@northern.tech>

2 of 2 new or added lines in 1 file covered. (100.0%)

6468 of 8281 relevant lines covered (78.11%)

11106.28 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

82.98
/mender-update/cli/cli.cpp
1
// Copyright 2023 Northern.tech AS
2
//
3
//    Licensed under the Apache License, Version 2.0 (the "License");
4
//    you may not use this file except in compliance with the License.
5
//    You may obtain a copy of the License at
6
//
7
//        http://www.apache.org/licenses/LICENSE-2.0
8
//
9
//    Unless required by applicable law or agreed to in writing, software
10
//    distributed under the License is distributed on an "AS IS" BASIS,
11
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
//    See the License for the specific language governing permissions and
13
//    limitations under the License.
14

15
#include <mender-update/cli/cli.hpp>
16

17
#include <iostream>
18

19
#include <common/conf.hpp>
20

21
namespace mender {
22
namespace update {
23
namespace cli {
24

25
namespace conf = mender::common::conf;
26

27
const int NoUpdateInProgressExitStatus = 2;
28
const int RebootExitStatus = 4;
29

30
ExpectedActionPtr ParseUpdateArguments(
96✔
31
        vector<string>::const_iterator start, vector<string>::const_iterator end) {
32
        if (start == end) {
96✔
33
                return expected::unexpected(conf::MakeError(conf::InvalidOptionsError, "Need an action"));
2✔
34
        }
35

36
        if (start[0] == "show-artifact") {
95✔
37
                unordered_set<string> options {};
8✔
38
                conf::CmdlineOptionsIterator iter(start + 1, end, options, options);
8✔
39
                auto arg = iter.Next();
8✔
40
                if (!arg) {
4✔
41
                        return expected::unexpected(arg.error());
4✔
42
                }
43

44
                return make_shared<ShowArtifactAction>();
4✔
45
        } else if (start[0] == "show-provides") {
91✔
46
                unordered_set<string> options {};
68✔
47
                conf::CmdlineOptionsIterator iter(start + 1, end, options, options);
68✔
48
                auto arg = iter.Next();
68✔
49
                if (!arg) {
34✔
50
                        return expected::unexpected(arg.error());
4✔
51
                }
52

53
                return make_shared<ShowProvidesAction>();
64✔
54
        } else if (start[0] == "install") {
57✔
55
                unordered_set<string> options {};
94✔
56
                conf::CmdlineOptionsIterator iter(start + 1, end, options, {"--reboot-exit-code"});
235✔
57
                iter.SetArgumentsMode(conf::ArgumentsMode::AcceptBareArguments);
47✔
58

59
                string filename;
94✔
60
                bool reboot_exit_code = false;
47✔
61
                while (true) {
62
                        auto arg = iter.Next();
93✔
63
                        if (!arg) {
93✔
64
                                return expected::unexpected(arg.error());
2✔
65
                        }
66

67
                        auto value = arg.value();
92✔
68
                        if (value.option == "--reboot-exit-code") {
92✔
69
                                reboot_exit_code = true;
1✔
70
                                continue;
1✔
71
                        } else if (value.option != "") {
91✔
72
                                return expected::unexpected(
×
73
                                        conf::MakeError(conf::InvalidOptionsError, "No such option: " + value.option));
×
74
                        }
75

76
                        if (value.value != "") {
91✔
77
                                if (filename != "") {
46✔
78
                                        return expected::unexpected(conf::MakeError(
1✔
79
                                                conf::InvalidOptionsError, "Too many arguments: " + value.value));
2✔
80
                                } else {
81
                                        filename = value.value;
45✔
82
                                }
83
                        } else {
84
                                if (filename == "") {
45✔
85
                                        return expected::unexpected(
1✔
86
                                                conf::MakeError(conf::InvalidOptionsError, "Need a path to an artifact"));
2✔
87
                                } else {
88
                                        break;
44✔
89
                                }
90
                        }
91
                }
46✔
92

93
                return make_shared<InstallAction>(filename, reboot_exit_code);
88✔
94
        } else if (start[0] == "commit") {
10✔
95
                unordered_set<string> options {};
8✔
96
                conf::CmdlineOptionsIterator iter(start + 1, end, options, options);
8✔
97
                auto arg = iter.Next();
8✔
98
                if (!arg) {
4✔
99
                        return expected::unexpected(arg.error());
×
100
                }
101

102
                return make_shared<CommitAction>();
8✔
103
        } else if (start[0] == "rollback") {
6✔
104
                unordered_set<string> options {};
12✔
105
                conf::CmdlineOptionsIterator iter(start + 1, end, options, options);
12✔
106
                auto arg = iter.Next();
12✔
107
                if (!arg) {
6✔
108
                        return expected::unexpected(arg.error());
×
109
                }
110

111
                return make_shared<RollbackAction>();
12✔
112
        } else if (start[0] == "daemon") {
×
113
                unordered_set<string> options {};
×
114
                conf::CmdlineOptionsIterator iter(start + 1, end, options, options);
×
115
                auto arg = iter.Next();
×
116
                if (!arg) {
×
117
                        return expected::unexpected(arg.error());
×
118
                }
119

120
                return make_shared<DaemonAction>();
×
121
        } else {
122
                return expected::unexpected(
×
123
                        conf::MakeError(conf::InvalidOptionsError, "No such action: " + start[0]));
×
124
        }
125
}
126

127
static error::Error DoMain(
96✔
128
        const vector<string> &args,
129
        function<void(mender::update::context::MenderContext &ctx)> test_hook) {
130
        mender::common::conf::MenderConfig config;
192✔
131

132
        auto args_pos = config.ProcessCmdlineArgs(args.begin(), args.end());
192✔
133
        if (!args_pos) {
96✔
134
                return args_pos.error();
×
135
        }
136

137
        auto action = ParseUpdateArguments(args.begin() + args_pos.value(), args.end());
192✔
138
        if (!action) {
96✔
139
                return action.error();
8✔
140
        }
141

142
        mender::update::context::MenderContext main_context(config);
176✔
143

144
        test_hook(main_context);
88✔
145

146
        auto err = main_context.Initialize();
176✔
147
        if (error::NoError != err) {
88✔
148
                return err;
×
149
        }
150

151
        return action.value()->Execute(main_context);
88✔
152
}
153

154
int Main(
96✔
155
        const vector<string> &args,
156
        function<void(mender::update::context::MenderContext &ctx)> test_hook) {
157
        auto err = DoMain(args, test_hook);
192✔
158

159
        if (err.code == context::MakeError(context::NoUpdateInProgressError, "").code) {
96✔
160
                return NoUpdateInProgressExitStatus;
2✔
161
        } else if (err.code == context::MakeError(context::RebootRequiredError, "").code) {
94✔
162
                return RebootExitStatus;
1✔
163
        } else if (err != error::NoError) {
93✔
164
                if (err.code == error::MakeError(error::ExitWithSuccessError, "").code) {
26✔
165
                        return 0;
×
166
                } else if (err.code != error::MakeError(error::ExitWithFailureError, "").code) {
26✔
167
                        cerr << "Could not fulfill request: " + err.String() << endl;
20✔
168
                }
169
                return 1;
26✔
170
        }
171

172
        return 0;
67✔
173
}
174

175
} // namespace cli
176
} // namespace update
177
} // namespace mender
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc