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

mendersoftware / mender / 1039476627

17 Oct 2023 10:49AM UTC coverage: 79.701% (-0.6%) from 80.278%
1039476627

push

gitlab-ci

oleorhagen
fix(mender-auth): Remember to pass in the needed params

Just add the missing identity script, and private key params.

Ticket: MEN-6671
Changelog: None

Signed-off-by: Ole Petter <ole.orhagen@northern.tech>

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

6557 of 8227 relevant lines covered (79.7%)

9764.5 hits per line

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

71.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
#include <common/error.hpp>
21
#include <common/expected.hpp>
22

23
namespace mender {
24
namespace update {
25
namespace cli {
26

27
namespace conf = mender::common::conf;
28
namespace error = mender::common::error;
29
namespace expected = mender::common::expected;
30

31
const int NoUpdateInProgressExitStatus = 2;
32
const int RebootExitStatus = 4;
33

34
ExpectedActionPtr ParseUpdateArguments(
96✔
35
        vector<string>::const_iterator start, vector<string>::const_iterator end) {
36
        if (start == end) {
96✔
37
                return expected::unexpected(conf::MakeError(conf::InvalidOptionsError, "Need an action"));
3✔
38
        }
39

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

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

57
                return make_shared<ShowProvidesAction>();
64✔
58
        } else if (start[0] == "install") {
57✔
59
                unordered_set<string> options {};
47✔
60
                conf::CmdlineOptionsIterator iter(start + 1, end, options, {"--reboot-exit-code"});
282✔
61
                iter.SetArgumentsMode(conf::ArgumentsMode::AcceptBareArguments);
62

63
                string filename;
64
                bool reboot_exit_code = false;
47✔
65
                while (true) {
66
                        auto arg = iter.Next();
93✔
67
                        if (!arg) {
93✔
68
                                return expected::unexpected(arg.error());
2✔
69
                        }
70

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

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

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

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

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

124
                return make_shared<DaemonAction>();
×
125
        } else if (start[0] == "send-inventory") {
×
126
                unordered_set<string> options {};
×
127
                conf::CmdlineOptionsIterator iter(start + 1, end, options, options);
×
128
                auto arg = iter.Next();
×
129
                if (!arg) {
×
130
                        return expected::unexpected(arg.error());
×
131
                }
132

133
                return make_shared<SendInventoryAction>();
×
134
        } else if (start[0] == "check-update") {
×
135
                unordered_set<string> options {};
×
136
                conf::CmdlineOptionsIterator iter(start + 1, end, options, options);
×
137
                auto arg = iter.Next();
×
138
                if (!arg) {
×
139
                        return expected::unexpected(arg.error());
×
140
                }
141

142
                return make_shared<CheckUpdateAction>();
×
143
        } else {
144
                return expected::unexpected(
×
145
                        conf::MakeError(conf::InvalidOptionsError, "No such action: " + start[0]));
×
146
        }
147
}
148

149
static error::Error DoMain(
98✔
150
        const vector<string> &args,
151
        function<void(mender::update::context::MenderContext &ctx)> test_hook) {
152
        mender::common::conf::MenderConfig config;
196✔
153

154
        auto args_pos = config.ProcessCmdlineArgs(args.begin(), args.end());
98✔
155
        if (!args_pos) {
98✔
156
                return args_pos.error();
2✔
157
        }
158

159
        auto action = ParseUpdateArguments(args.begin() + args_pos.value(), args.end());
96✔
160
        if (!action) {
96✔
161
                return action.error();
8✔
162
        }
163

164
        mender::update::context::MenderContext main_context(config);
88✔
165

166
        test_hook(main_context);
88✔
167

168
        auto err = main_context.Initialize();
88✔
169
        if (error::NoError != err) {
88✔
170
                return err;
×
171
        }
172

173
        return action.value()->Execute(main_context);
88✔
174
}
175

176
int Main(
98✔
177
        const vector<string> &args,
178
        function<void(mender::update::context::MenderContext &ctx)> test_hook) {
179
        auto err = DoMain(args, test_hook);
98✔
180

181
        if (err.code == context::MakeError(context::NoUpdateInProgressError, "").code) {
98✔
182
                return NoUpdateInProgressExitStatus;
183
        } else if (err.code == context::MakeError(context::RebootRequiredError, "").code) {
96✔
184
                return RebootExitStatus;
185
        } else if (err != error::NoError) {
95✔
186
                if (err.code == error::MakeError(error::ExitWithSuccessError, "").code) {
28✔
187
                        return 0;
188
                } else if (err.code != error::MakeError(error::ExitWithFailureError, "").code) {
27✔
189
                        cerr << "Could not fulfill request: " + err.String() << endl;
42✔
190
                }
191
                return 1;
27✔
192
        }
193

194
        return 0;
195
}
196

197
} // namespace cli
198
} // namespace update
199
} // 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