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

mendersoftware / mender / 950534094

pending completion
950534094

push

gitlab-ci

kacf
chore: Add `StartsWith` and `EndsWith` generic utility functions.

Also use the latter in `states.cpp`.

Signed-off-by: Kristian Amlie <kristian.amlie@northern.tech>

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

4931 of 6276 relevant lines covered (78.57%)

196.18 hits per line

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

81.72
/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(
78✔
31
        vector<string>::const_iterator start, vector<string>::const_iterator end) {
32
        if (start == end) {
78✔
33
                return expected::unexpected(conf::MakeError(conf::InvalidOptionsError, "Need an action"));
2✔
34
        }
35

36
        if (start[0] == "show-artifact") {
77✔
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") {
73✔
46
                unordered_set<string> options {};
60✔
47
                conf::CmdlineOptionsIterator iter(start + 1, end, options, options);
60✔
48
                auto arg = iter.Next();
60✔
49
                if (!arg) {
30✔
50
                        return expected::unexpected(arg.error());
4✔
51
                }
52

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

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

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

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

93
                return make_shared<InstallAction>(filename, reboot_exit_code);
62✔
94
        } else if (start[0] == "commit") {
9✔
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") {
5✔
104
                unordered_set<string> options {};
10✔
105
                conf::CmdlineOptionsIterator iter(start + 1, end, options, options);
10✔
106
                auto arg = iter.Next();
10✔
107
                if (!arg) {
5✔
108
                        return expected::unexpected(arg.error());
×
109
                }
110

111
                return make_shared<RollbackAction>();
10✔
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
int Main(
78✔
128
        const vector<string> &args,
129
        function<void(mender::update::context::MenderContext &ctx)> test_hook) {
130
        mender::common::conf::MenderConfig config;
156✔
131

132
        auto args_pos = config.ProcessCmdlineArgs(args.begin(), args.end());
156✔
133
        if (!args_pos) {
78✔
134
                cerr << "Failed to process command line options: " + args_pos.error().String() << endl;
×
135
                return 1;
×
136
        }
137

138
        auto action = ParseUpdateArguments(args.begin() + args_pos.value(), args.end());
156✔
139
        if (!action) {
78✔
140
                cerr << "Failed to process command line options: " + action.error().String() << endl;
8✔
141
                return 1;
8✔
142
        }
143

144
        mender::update::context::MenderContext main_context(config);
140✔
145

146
        test_hook(main_context);
70✔
147

148
        auto err = main_context.Initialize();
140✔
149
        if (error::NoError != err) {
70✔
150
                cerr << "Failed to intialize main context: " + err.String() << endl;
×
151
                return 1;
×
152
        }
153

154
        err = action.value()->Execute(main_context);
70✔
155

156
        if (err.code == context::MakeError(context::NoUpdateInProgressError, "").code) {
70✔
157
                return NoUpdateInProgressExitStatus;
2✔
158
        } else if (err.code == context::MakeError(context::RebootRequiredError, "").code) {
68✔
159
                return RebootExitStatus;
1✔
160
        } else if (err != error::NoError) {
67✔
161
                if (err.code != context::MakeError(context::ExitStatusOnlyError, "").code) {
9✔
162
                        cerr << "Could not fulfill request: " + err.String() << endl;
6✔
163
                }
164
                return 1;
9✔
165
        }
166

167
        return 0;
58✔
168
}
169

170
} // namespace cli
171
} // namespace update
172
} // 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