1*6b3a42afSjmmv // Copyright 2011 Google Inc.
2*6b3a42afSjmmv // All rights reserved.
3*6b3a42afSjmmv //
4*6b3a42afSjmmv // Redistribution and use in source and binary forms, with or without
5*6b3a42afSjmmv // modification, are permitted provided that the following conditions are
6*6b3a42afSjmmv // met:
7*6b3a42afSjmmv //
8*6b3a42afSjmmv // * Redistributions of source code must retain the above copyright
9*6b3a42afSjmmv // notice, this list of conditions and the following disclaimer.
10*6b3a42afSjmmv // * Redistributions in binary form must reproduce the above copyright
11*6b3a42afSjmmv // notice, this list of conditions and the following disclaimer in the
12*6b3a42afSjmmv // documentation and/or other materials provided with the distribution.
13*6b3a42afSjmmv // * Neither the name of Google Inc. nor the names of its contributors
14*6b3a42afSjmmv // may be used to endorse or promote products derived from this software
15*6b3a42afSjmmv // without specific prior written permission.
16*6b3a42afSjmmv //
17*6b3a42afSjmmv // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18*6b3a42afSjmmv // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19*6b3a42afSjmmv // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20*6b3a42afSjmmv // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21*6b3a42afSjmmv // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22*6b3a42afSjmmv // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23*6b3a42afSjmmv // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24*6b3a42afSjmmv // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25*6b3a42afSjmmv // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26*6b3a42afSjmmv // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27*6b3a42afSjmmv // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*6b3a42afSjmmv
29*6b3a42afSjmmv #include "cli/config.hpp"
30*6b3a42afSjmmv
31*6b3a42afSjmmv #include "cli/common.hpp"
32*6b3a42afSjmmv #include "engine/config.hpp"
33*6b3a42afSjmmv #include "engine/exceptions.hpp"
34*6b3a42afSjmmv #include "utils/cmdline/parser.ipp"
35*6b3a42afSjmmv #include "utils/config/tree.ipp"
36*6b3a42afSjmmv #include "utils/format/macros.hpp"
37*6b3a42afSjmmv #include "utils/fs/exceptions.hpp"
38*6b3a42afSjmmv #include "utils/fs/operations.hpp"
39*6b3a42afSjmmv #include "utils/fs/path.hpp"
40*6b3a42afSjmmv #include "utils/env.hpp"
41*6b3a42afSjmmv #include "utils/logging/macros.hpp"
42*6b3a42afSjmmv #include "utils/optional.ipp"
43*6b3a42afSjmmv
44*6b3a42afSjmmv namespace cmdline = utils::cmdline;
45*6b3a42afSjmmv namespace config = utils::config;
46*6b3a42afSjmmv namespace fs = utils::fs;
47*6b3a42afSjmmv
48*6b3a42afSjmmv using utils::optional;
49*6b3a42afSjmmv
50*6b3a42afSjmmv
51*6b3a42afSjmmv namespace {
52*6b3a42afSjmmv
53*6b3a42afSjmmv
54*6b3a42afSjmmv /// Basename of the configuration file.
55*6b3a42afSjmmv static const char* config_basename = "kyua.conf";
56*6b3a42afSjmmv
57*6b3a42afSjmmv
58*6b3a42afSjmmv /// Magic string to disable loading of configuration files.
59*6b3a42afSjmmv static const char* none_config = "none";
60*6b3a42afSjmmv
61*6b3a42afSjmmv
62*6b3a42afSjmmv /// Textual description of the default configuration files.
63*6b3a42afSjmmv ///
64*6b3a42afSjmmv /// This is just an auxiliary string required to define the option below, which
65*6b3a42afSjmmv /// requires a pointer to a static C string.
66*6b3a42afSjmmv ///
67*6b3a42afSjmmv /// \todo If the user overrides the KYUA_CONFDIR environment variable, we don't
68*6b3a42afSjmmv /// reflect this fact here. We don't want to query the variable during program
69*6b3a42afSjmmv /// initialization due to the side-effects it may have. Therefore, fixing this
70*6b3a42afSjmmv /// is tricky as it may require a whole rethink of this module.
71*6b3a42afSjmmv static const std::string config_lookup_names =
72*6b3a42afSjmmv (fs::path("~/.kyua") / config_basename).str() + " or " +
73*6b3a42afSjmmv (fs::path(KYUA_CONFDIR) / config_basename).str();
74*6b3a42afSjmmv
75*6b3a42afSjmmv
76*6b3a42afSjmmv /// Loads the configuration file for this session, if any.
77*6b3a42afSjmmv ///
78*6b3a42afSjmmv /// This is a helper function that does not apply user-specified overrides. See
79*6b3a42afSjmmv /// the documentation for cli::load_config() for more details.
80*6b3a42afSjmmv ///
81*6b3a42afSjmmv /// \param cmdline The parsed command line.
82*6b3a42afSjmmv ///
83*6b3a42afSjmmv /// \return The loaded configuration file, or the configuration defaults if the
84*6b3a42afSjmmv /// loading is disabled.
85*6b3a42afSjmmv ///
86*6b3a42afSjmmv /// \throw engine::error If the parsing of the configuration file fails.
87*6b3a42afSjmmv /// TODO(jmmv): I'm not sure if this is the raised exception. And even if
88*6b3a42afSjmmv /// it is, we should make it more accurate.
89*6b3a42afSjmmv config::tree
load_config_file(const cmdline::parsed_cmdline & cmdline)90*6b3a42afSjmmv load_config_file(const cmdline::parsed_cmdline& cmdline)
91*6b3a42afSjmmv {
92*6b3a42afSjmmv // TODO(jmmv): We should really be able to use cmdline.has_option here to
93*6b3a42afSjmmv // detect whether the option was provided or not instead of checking against
94*6b3a42afSjmmv // the default value.
95*6b3a42afSjmmv const fs::path filename = cmdline.get_option< cmdline::path_option >(
96*6b3a42afSjmmv cli::config_option.long_name());
97*6b3a42afSjmmv if (filename.str() == none_config) {
98*6b3a42afSjmmv LD("Configuration loading disabled; using defaults");
99*6b3a42afSjmmv return engine::default_config();
100*6b3a42afSjmmv } else if (filename.str() != cli::config_option.default_value())
101*6b3a42afSjmmv return engine::load_config(filename);
102*6b3a42afSjmmv
103*6b3a42afSjmmv const optional< fs::path > home = cli::get_home();
104*6b3a42afSjmmv if (home) {
105*6b3a42afSjmmv const fs::path path = home.get() / ".kyua" / config_basename;
106*6b3a42afSjmmv try {
107*6b3a42afSjmmv if (fs::exists(path))
108*6b3a42afSjmmv return engine::load_config(path);
109*6b3a42afSjmmv } catch (const fs::error& e) {
110*6b3a42afSjmmv // Fall through. If we fail to load the user-specific configuration
111*6b3a42afSjmmv // file because it cannot be openend, we try to load the system-wide
112*6b3a42afSjmmv // one.
113*6b3a42afSjmmv LW(F("Failed to load user-specific configuration file '%s': %s") %
114*6b3a42afSjmmv path % e.what());
115*6b3a42afSjmmv }
116*6b3a42afSjmmv }
117*6b3a42afSjmmv
118*6b3a42afSjmmv const fs::path confdir(utils::getenv_with_default(
119*6b3a42afSjmmv "KYUA_CONFDIR", KYUA_CONFDIR));
120*6b3a42afSjmmv
121*6b3a42afSjmmv const fs::path path = confdir / config_basename;
122*6b3a42afSjmmv if (fs::exists(path)) {
123*6b3a42afSjmmv return engine::load_config(path);
124*6b3a42afSjmmv } else {
125*6b3a42afSjmmv return engine::default_config();
126*6b3a42afSjmmv }
127*6b3a42afSjmmv }
128*6b3a42afSjmmv
129*6b3a42afSjmmv
130*6b3a42afSjmmv /// Loads the configuration file for this session, if any.
131*6b3a42afSjmmv ///
132*6b3a42afSjmmv /// This is a helper function for cli::load_config() that attempts to load the
133*6b3a42afSjmmv /// configuration unconditionally.
134*6b3a42afSjmmv ///
135*6b3a42afSjmmv /// \param cmdline The parsed command line.
136*6b3a42afSjmmv ///
137*6b3a42afSjmmv /// \return The loaded configuration file data.
138*6b3a42afSjmmv ///
139*6b3a42afSjmmv /// \throw engine::error If the parsing of the configuration file fails.
140*6b3a42afSjmmv static config::tree
load_required_config(const cmdline::parsed_cmdline & cmdline)141*6b3a42afSjmmv load_required_config(const cmdline::parsed_cmdline& cmdline)
142*6b3a42afSjmmv {
143*6b3a42afSjmmv config::tree user_config = load_config_file(cmdline);
144*6b3a42afSjmmv
145*6b3a42afSjmmv if (cmdline.has_option(cli::variable_option.long_name())) {
146*6b3a42afSjmmv typedef std::pair< std::string, std::string > override_pair;
147*6b3a42afSjmmv
148*6b3a42afSjmmv const std::vector< override_pair >& overrides =
149*6b3a42afSjmmv cmdline.get_multi_option< cmdline::property_option >(
150*6b3a42afSjmmv cli::variable_option.long_name());
151*6b3a42afSjmmv
152*6b3a42afSjmmv for (std::vector< override_pair >::const_iterator
153*6b3a42afSjmmv iter = overrides.begin(); iter != overrides.end(); iter++) {
154*6b3a42afSjmmv try {
155*6b3a42afSjmmv user_config.set_string((*iter).first, (*iter).second);
156*6b3a42afSjmmv } catch (const config::error& e) {
157*6b3a42afSjmmv // TODO(jmmv): Raising this type from here is obviously the
158*6b3a42afSjmmv // wrong thing to do.
159*6b3a42afSjmmv throw engine::error(e.what());
160*6b3a42afSjmmv }
161*6b3a42afSjmmv }
162*6b3a42afSjmmv }
163*6b3a42afSjmmv
164*6b3a42afSjmmv return user_config;
165*6b3a42afSjmmv }
166*6b3a42afSjmmv
167*6b3a42afSjmmv
168*6b3a42afSjmmv } // anonymous namespace
169*6b3a42afSjmmv
170*6b3a42afSjmmv
171*6b3a42afSjmmv /// Standard definition of the option to specify a configuration file.
172*6b3a42afSjmmv ///
173*6b3a42afSjmmv /// You must use load_config() to load a configuration file while honoring the
174*6b3a42afSjmmv /// value of this flag.
175*6b3a42afSjmmv const cmdline::path_option cli::config_option(
176*6b3a42afSjmmv 'c', "config",
177*6b3a42afSjmmv (std::string("Path to the configuration file; '") + none_config +
178*6b3a42afSjmmv "' to disable loading").c_str(),
179*6b3a42afSjmmv "file", config_lookup_names.c_str());
180*6b3a42afSjmmv
181*6b3a42afSjmmv
182*6b3a42afSjmmv /// Standard definition of the option to specify a configuration variable.
183*6b3a42afSjmmv const cmdline::property_option cli::variable_option(
184*6b3a42afSjmmv 'v', "variable",
185*6b3a42afSjmmv "Overrides a particular configuration variable",
186*6b3a42afSjmmv "K=V");
187*6b3a42afSjmmv
188*6b3a42afSjmmv
189*6b3a42afSjmmv /// Loads the configuration file for this session, if any.
190*6b3a42afSjmmv ///
191*6b3a42afSjmmv /// The algorithm implemented here is as follows:
192*6b3a42afSjmmv /// 1) If ~/.kyua/kyua.conf exists, load it.
193*6b3a42afSjmmv /// 2) Otherwise, if sysconfdir/kyua.conf exists, load it.
194*6b3a42afSjmmv /// 3) Otherwise, use the built-in settings.
195*6b3a42afSjmmv /// 4) Lastly, apply any user-provided overrides.
196*6b3a42afSjmmv ///
197*6b3a42afSjmmv /// \param cmdline The parsed command line.
198*6b3a42afSjmmv /// \param required Whether the loading of the configuration file must succeed.
199*6b3a42afSjmmv /// Some commands should run regardless, and therefore we need to set this
200*6b3a42afSjmmv /// to false for those commands.
201*6b3a42afSjmmv ///
202*6b3a42afSjmmv /// \return The loaded configuration file data. If required was set to false,
203*6b3a42afSjmmv /// this might be the default configuration data if the requested file could not
204*6b3a42afSjmmv /// be properly loaded.
205*6b3a42afSjmmv ///
206*6b3a42afSjmmv /// \throw engine::error If the parsing of the configuration file fails.
207*6b3a42afSjmmv config::tree
load_config(const cmdline::parsed_cmdline & cmdline,const bool required)208*6b3a42afSjmmv cli::load_config(const cmdline::parsed_cmdline& cmdline,
209*6b3a42afSjmmv const bool required)
210*6b3a42afSjmmv {
211*6b3a42afSjmmv try {
212*6b3a42afSjmmv return load_required_config(cmdline);
213*6b3a42afSjmmv } catch (const engine::error& e) {
214*6b3a42afSjmmv if (required) {
215*6b3a42afSjmmv throw;
216*6b3a42afSjmmv } else {
217*6b3a42afSjmmv LW(F("Ignoring failure to load configuration because the requested "
218*6b3a42afSjmmv "command should not fail: %s") % e.what());
219*6b3a42afSjmmv return engine::default_config();
220*6b3a42afSjmmv }
221*6b3a42afSjmmv }
222*6b3a42afSjmmv }
223