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

mendersoftware / mender / 974575668

21 Aug 2023 12:04PM UTC coverage: 78.829% (-0.05%) from 78.877%
974575668

push

gitlab-ci

kacf
chore: Implement pushing of logs to the server.

Ticket: MEN-6581

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

18 of 18 new or added lines in 2 files covered. (100.0%)

5492 of 6967 relevant lines covered (78.83%)

238.75 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(
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
static error::Error DoMain(
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
                return args_pos.error();
×
135
        }
136

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

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

144
        test_hook(main_context);
70✔
145

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

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

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

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

172
        return 0;
58✔
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