This repository has been archived by the owner on May 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 61
/
main.cc
81 lines (70 loc) · 2.82 KB
/
main.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <unistd.h>
#include <iostream>
#include <boost/filesystem.hpp>
#include <boost/program_options.hpp>
#include "get.h"
#include "libaktualizr/config.h"
#include "logging/logging.h"
#include "utilities/aktualizr_version.h"
namespace bpo = boost::program_options;
void check_info_options(const bpo::options_description &description, const bpo::variables_map &vm) {
if (vm.count("help") != 0 || (vm.count("url") == 0 && vm.count("version") == 0)) {
std::cout << description << '\n';
exit(EXIT_SUCCESS);
}
if (vm.count("version") != 0) {
std::cout << "Current aktualizr-get version is: " << aktualizr_version() << "\n";
exit(EXIT_SUCCESS);
}
}
bpo::variables_map parse_options(int argc, char **argv) {
bpo::options_description description(
"A tool similar to wget that will do an HTTP get on the given URL using the device's configured credentials.");
// clang-format off
// Try to keep these options in the same order as Config::updateFromCommandLine().
// The first three are commandline only.
description.add_options()
("help,h", "print usage")
("version,v", "Current aktualizr-get version")
("config,c", bpo::value<std::vector<boost::filesystem::path> >()->composing(), "configuration file or directory, by default /var/sota")
("header,H", bpo::value<std::vector<std::string> >()->composing(), "Additional headers to pass")
("loglevel", bpo::value<int>(), "set log level 0-5 (trace, debug, info, warning, error, fatal)")
("url,u", bpo::value<std::string>(), "url to get, mandatory");
// clang-format on
bpo::variables_map vm;
std::vector<std::string> unregistered_options;
try {
bpo::basic_parsed_options<char> parsed_options = bpo::command_line_parser(argc, argv).options(description).run();
bpo::store(parsed_options, vm);
check_info_options(description, vm);
bpo::notify(vm);
} catch (const bpo::required_option &ex) {
// print the error and append the default commandline option description
std::cout << ex.what() << std::endl << description;
exit(EXIT_FAILURE);
} catch (const bpo::error &ex) {
std::cout << ex.what() << std::endl;
std::cout << description;
exit(EXIT_FAILURE);
}
return vm;
}
int main(int argc, char *argv[]) {
logger_init(isatty(1) == 1);
logger_set_threshold(boost::log::trivial::info);
bpo::variables_map commandline_map = parse_options(argc, argv);
int r = EXIT_FAILURE;
try {
Config config(commandline_map);
std::vector<std::string> headers;
if (commandline_map.count("header") == 1) {
headers = commandline_map["header"].as<std::vector<std::string>>();
}
std::string body = aktualizrGet(config, commandline_map["url"].as<std::string>(), headers);
std::cout << body;
r = EXIT_SUCCESS;
} catch (const std::exception &ex) {
LOG_ERROR << ex.what();
}
return r;
}