Add first version
[ric-plt/sdl.git] / src / cli / dumpconfigurationcommand.cpp
1 #include <ostream>
2 #include <cstdlib>
3 #include <fstream>
4 #include <string>
5 #include <iostream>
6 #include <boost/property_tree/ptree.hpp>
7 #include <boost/property_tree/json_parser.hpp>
8 #include "private/cli/commandmap.hpp"
9 #include "private/configurationpaths.hpp"
10 #include "private/configurationreader.hpp"
11
12 using namespace shareddatalayer;
13 using namespace shareddatalayer::cli;
14
15 namespace
16 {
17     bool parseConfiguration(const std::string& file)
18     {
19         boost::property_tree::ptree propertyTree;
20
21         try
22         {
23             boost::property_tree::read_json(file, propertyTree);
24         }
25         catch (const boost::property_tree::json_parser::json_parser_error& e)
26         {
27             std::ostringstream os;
28             os << "error in SDL configuration " << file << " at line " << e.line() << ": ";
29             os << e.message();
30             std::cerr << os.str().c_str() << std::endl;
31             return false;
32         }
33         return true;
34     }
35
36
37     int dumpConfigurationCommand(std::ostream& out)
38     {
39         std::string line;
40         bool status(true);
41
42         for (const auto& i : findConfigurationFiles( getDefaultConfDirectories() ))
43         {
44             std::ifstream file(i);
45
46             out << "File: " << i << std::endl;
47
48             unsigned int lineNum = 1;
49
50             if(file.is_open())
51             {
52                 bool parseStatus = parseConfiguration(i);
53
54                 if(status && !parseStatus)
55                     status = false;
56
57                 while(getline(file, line))
58                     out << lineNum++ << ": " << line << std::endl;
59                 file.close();
60             }
61         }
62
63         const auto var(DB_HOST_ENV_VAR_NAME);
64         const auto conf(getenv(var));
65         if (conf == nullptr)
66             out << var << " not set." << std::endl;
67         else
68             out << var  << ": " << conf << std::endl;
69
70         if(!status)
71         {
72             return EXIT_FAILURE;
73         }
74         return EXIT_SUCCESS;
75     }
76 }
77
78 AUTO_REGISTER_COMMAND(std::bind(dumpConfigurationCommand, std::placeholders::_1),
79                       "dump-configuration",
80                       "Dump configuration",
81                       "Find, parse and dump all shareddatalayer configuration files contents.",
82                       CommandMap::Category::UTIL, 30000);
83