source: nscp/service/settings_client.hpp @ 184d575

0.4.00.4.10.4.2
Last change on this file since 184d575 was 753ea6d, checked in by Michael Medin <michael@…>, 3 years ago

improved the scheduled module and re-added the settings command line toy (so now you can migrate settings again)
Schedule module is 80% finished but notification channel subsystem is still missing.

  • Property mode set to 100644
File size: 4.8 KB
Line 
1#pragma once
2
3
4class NSClientT;
5namespace nsclient {
6        class settings_client {
7                NSClient* core_;
8        public:
9                settings_client(NSClient* core) : core_(core) {
10
11                }
12                void parse(std::wstring sarg, int argc, wchar_t* argv[]) {
13                        core_->enableDebug(true);
14                        if (!core_->initCore(false)) {
15                                std::wcout << _T("Service *NOT* started!") << std::endl;
16                                return;
17                        }
18                        std::wcout << _T("Using settings from: ") << settings_manager::get_core()->get_settings_type_desc() << std::endl;
19
20                        try {
21                                if (sarg == _T("migrate")) {
22                                        if (argc == 0) {
23                                                error_msg(_T("In correct syntax: nsclient++ -settings migrate <to>"));
24                                                return;
25                                        }
26                                        std::wstring to = argv[0];
27                                        debug_msg(_T("Migrating to: ") + to);
28                                        try {
29                                                settings_manager::get_core()->migrate_to(Settings::SettingsCore::string_to_type(to));
30                                        } catch (SettingsException e) {
31                                                error_msg(_T("Failed to migrate settings: ") + e.getError());
32                                        }
33                                } else if (sarg == _T("generate")) {
34                                        if (argc == 0) {
35                                                error_msg(_T("In correct syntax: nsclient++ -settings generate <what>"));
36                                                error_msg(_T("     where <what> is one of ths following:"));
37                                                error_msg(_T("      trac"));
38                                                error_msg(_T("      default"));
39                                                error_msg(_T("      <type>"));
40                                                return;
41                                        }
42                                        std::wstring arg1 = argv[0];
43                                        if (arg1 == _T("default")) {
44                                                try {
45                                                        core_->load_all_plugins(NSCAPI::dontStart);
46                                                        settings_manager::get_core()->update_defaults();
47                                                        settings_manager::get_core()->get()->save();
48                                                } catch (SettingsException e) {
49                                                        error_msg(_T("Failed to migrate settings: ") + e.getError());
50                                                }
51                                        } else if (arg1 == _T("trac")) {
52                                                try {
53                                                        core_->load_all_plugins(NSCAPI::dontStart);
54
55                                                        Settings::string_list s = settings_manager::get_core()->get_reg_sections();
56                                                        for (Settings::string_list::const_iterator cit = s.begin(); cit != s.end(); ++cit) {
57                                                                std::wcout << _T("== ") << (*cit) << _T(" ==") << std::endl;
58                                                                Settings::string_list k = settings_manager::get_core()->get_reg_keys(*cit);
59                                                                bool first = true;
60                                                                for (Settings::string_list::const_iterator citk = k.begin(); citk != k.end(); ++citk) {
61                                                                        Settings::SettingsCore::key_description desc = settings_manager::get_core()->get_registred_key(*cit, *citk);
62                                                                        if (!desc.advanced) {
63                                                                                if (first)
64                                                                                        std::wcout << _T("'''Normal settings'''") << std::endl;
65                                                                                first = false;
66                                                                                std::wcout << _T("||") << (*citk) << _T("||") << desc.defValue << _T("||") << desc.title << _T(": ") << desc.description
67                                                                                        << std::endl;
68                                                                        }
69                                                                }
70                                                                first = true;
71                                                                for (Settings::string_list::const_iterator citk = k.begin(); citk != k.end(); ++citk) {
72                                                                        Settings::SettingsCore::key_description desc = settings_manager::get_core()->get_registred_key(*cit, *citk);
73                                                                        if (desc.advanced) {
74                                                                                if (first)
75                                                                                        std::wcout << _T("'''Advanced settings'''") << std::endl;
76                                                                                first = false;
77                                                                                std::wcout << _T("||") << (*citk) << _T("||") << desc.defValue << _T("||") << desc.title << _T(": ") << desc.description
78                                                                                        << std::endl;
79                                                                        }
80                                                                }
81                                                        }
82                                                } catch (SettingsException e) {
83                                                        error_msg(_T("Failed to migrate settings: ") + e.getError());
84                                                }
85                                        } else {
86                                                try {
87                                                        Settings::SettingsCore::settings_type type = settings_manager::get_core()->string_to_type(arg1);
88                                                        core_->load_all_plugins(NSCAPI::dontStart);
89                                                        settings_manager::get_core()->update_defaults();
90                                                        settings_manager::get_core()->get(type)->save();
91                                                } catch (SettingsException e) {
92                                                        error_msg(_T("Failed to migrate settings: ") + e.getError());
93                                                }
94                                        }
95                                } else {
96                                        std::wcout << _T("Unknown keyword: ") << sarg << std::endl;
97                                        help();
98                                }
99
100
101                        } catch (SettingsException e) {
102                                error_msg(_T("Failed to initialize settings: ") + e.getError());
103                        } catch (...) {
104                                error_msg(_T("FATAL ERROR IN SETTINGS SUBSYTEM"));
105                        }
106
107                        core_->exitCore(true);
108                }
109
110                void error_msg(std::wstring msg) {
111                        core_->reportMessage(NSCAPI::error, __FILEW__, __LINE__, msg.c_str());
112                }
113                void debug_msg(std::wstring msg) {
114                        core_->reportMessage(NSCAPI::debug, __FILEW__, __LINE__, msg.c_str());
115                }
116
117                void help() {
118                        std::wcout << _T("Usage: nsclient++ -settings <key>") << std::endl;
119                        std::wcout << _T("In correct syntax: nsclient++ -settings <keyword>") << std::endl;
120                        std::wcout << _T(" <keyword> : ") << std::endl;
121                        std::wcout << _T("   migrate - migrate to a new setings subsystem") << std::endl;
122                        std::wcout << _T("   copy    - copy settings from one subsystem to another") << std::endl;
123                        std::wcout << _T("   set     - Set a setting system as the default store") << std::endl;
124                }
125
126        };
127}
Note: See TracBrowser for help on using the repository browser.