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