1 /* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /*
3  *     Copyright 2018 Couchbase, Inc
4  *
5  *   Licensed under the Apache License, Version 2.0 (the "License");
6  *   you may not use this file except in compliance with the License.
7  *   You may obtain a copy of the License at
8  *
9  *       http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *   Unless required by applicable law or agreed to in writing, software
12  *   distributed under the License is distributed on an "AS IS" BASIS,
13  *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *   See the License for the specific language governing permissions and
15  *   limitations under the License.
16  */
17 #include "config_parse.h"
18 #include "cmdline.h"
19 #include "settings.h"
20 
21 #include <logger/logger.h>
22 #include <nlohmann/json.hpp>
23 #include <platform/dirutils.h>
24 
25 /******************************************************************************
26  * Public functions
27  *****************************************************************************/
28 
load_config_file(const char *file, Settings& settings)29 void load_config_file(const char *file, Settings& settings)
30 {
31     auto content = cb::io::loadFile(file);
32     settings.reconfigure(nlohmann::json::parse(content));
33 }
34 
validate_proposed_config_changes( const char* new_cfg)35 boost::optional<nlohmann::json> validate_proposed_config_changes(
36         const char* new_cfg) {
37     nlohmann::json errors = nlohmann::json::array();
38     // Earlier we returned all of the errors, now I'm terminating on
39     // the first... Ideally all of the errors would be best, but
40     // the code is easier if we can use exceptions to abort the parsing
41     // when we hit an error. Given that this isn't something that the
42     // user would be calling every time I don't think it is a big problem..
43     try {
44         auto json = nlohmann::json::parse(new_cfg);
45         Settings new_settings(json);
46         Settings::instance().updateSettings(new_settings, false);
47         return {};
48     } catch (const std::exception& exception) {
49         errors.push_back(exception.what());
50     }
51 
52     return errors;
53 }
54 
reload_config_file()55 void reload_config_file() {
56     LOG_INFO("Reloading config file {}", get_config_file());
57 
58     try {
59         auto content = cb::io::loadFile(get_config_file());
60         Settings new_settings(nlohmann::json::parse(content));
61         Settings::instance().updateSettings(new_settings, true);
62     } catch (const std::exception& exception) {
63         LOG_WARNING(
64                 "Validation failed while reloading config file '{}'. Error: {}",
65                 get_config_file(),
66                 exception.what());
67     }
68 }
69