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