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

mendersoftware / mender / 950545443

pending completion
950545443

push

gitlab-ci

vpodzime
feat: Introduce deployments::DeploymentLog

A simple class that can scope logging of a specific deployment.

Ticket: MEN-6578
Changelog: none
Signed-off-by: Vratislav Podzimek <v.podzimek@mykolab.com>

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

4283 of 5387 relevant lines covered (79.51%)

159.37 hits per line

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

88.37
/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 {
113
                return expected::unexpected(
×
114
                        conf::MakeError(conf::InvalidOptionsError, "No such action: " + start[0]));
×
115
        }
116
}
117

118
int Main(
78✔
119
        const vector<string> &args,
120
        function<void(mender::update::context::MenderContext &ctx)> test_hook) {
121
        mender::common::conf::MenderConfig config;
156✔
122

123
        auto args_pos = config.ProcessCmdlineArgs(args.begin(), args.end());
156✔
124
        if (!args_pos) {
78✔
125
                cerr << "Failed to process command line options: " + args_pos.error().String() << endl;
×
126
                return 1;
×
127
        }
128

129
        auto action = ParseUpdateArguments(args.begin() + args_pos.value(), args.end());
156✔
130
        if (!action) {
78✔
131
                cerr << "Failed to process command line options: " + action.error().String() << endl;
8✔
132
                return 1;
8✔
133
        }
134

135
        mender::update::context::MenderContext main_context(config);
140✔
136

137
        test_hook(main_context);
70✔
138

139
        auto err = main_context.Initialize();
140✔
140
        if (error::NoError != err) {
70✔
141
                cerr << "Failed to intialize main context: " + err.String() << endl;
×
142
                return 1;
×
143
        }
144

145
        err = action.value()->Execute(main_context);
70✔
146

147
        if (err.code == context::MakeError(context::NoUpdateInProgressError, "").code) {
70✔
148
                return NoUpdateInProgressExitStatus;
2✔
149
        } else if (err.code == context::MakeError(context::RebootRequiredError, "").code) {
68✔
150
                return RebootExitStatus;
1✔
151
        } else if (err != error::NoError) {
67✔
152
                if (err.code != context::MakeError(context::ExitStatusOnlyError, "").code) {
9✔
153
                        cerr << "Could not fulfill request: " + err.String() << endl;
6✔
154
                }
155
                return 1;
9✔
156
        }
157

158
        return 0;
58✔
159
}
160

161
} // namespace cli
162
} // namespace update
163
} // 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