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