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