1*6a6c8299Sjmmv // Copyright 2011 Google Inc.
2*6a6c8299Sjmmv // All rights reserved.
3*6a6c8299Sjmmv //
4*6a6c8299Sjmmv // Redistribution and use in source and binary forms, with or without
5*6a6c8299Sjmmv // modification, are permitted provided that the following conditions are
6*6a6c8299Sjmmv // met:
7*6a6c8299Sjmmv //
8*6a6c8299Sjmmv // * Redistributions of source code must retain the above copyright
9*6a6c8299Sjmmv //   notice, this list of conditions and the following disclaimer.
10*6a6c8299Sjmmv // * Redistributions in binary form must reproduce the above copyright
11*6a6c8299Sjmmv //   notice, this list of conditions and the following disclaimer in the
12*6a6c8299Sjmmv //   documentation and/or other materials provided with the distribution.
13*6a6c8299Sjmmv // * Neither the name of Google Inc. nor the names of its contributors
14*6a6c8299Sjmmv //   may be used to endorse or promote products derived from this software
15*6a6c8299Sjmmv //   without specific prior written permission.
16*6a6c8299Sjmmv //
17*6a6c8299Sjmmv // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18*6a6c8299Sjmmv // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19*6a6c8299Sjmmv // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20*6a6c8299Sjmmv // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21*6a6c8299Sjmmv // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22*6a6c8299Sjmmv // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23*6a6c8299Sjmmv // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24*6a6c8299Sjmmv // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25*6a6c8299Sjmmv // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26*6a6c8299Sjmmv // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27*6a6c8299Sjmmv // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*6a6c8299Sjmmv 
29*6a6c8299Sjmmv #include "cli/cmd_config.hpp"
30*6a6c8299Sjmmv 
31*6a6c8299Sjmmv #include <cstdlib>
32*6a6c8299Sjmmv 
33*6a6c8299Sjmmv #include "cli/common.ipp"
34*6a6c8299Sjmmv #include "utils/config/tree.ipp"
35*6a6c8299Sjmmv #include "utils/format/macros.hpp"
36*6a6c8299Sjmmv 
37*6a6c8299Sjmmv namespace cmdline = utils::cmdline;
38*6a6c8299Sjmmv namespace config = utils::config;
39*6a6c8299Sjmmv 
40*6a6c8299Sjmmv using cli::cmd_config;
41*6a6c8299Sjmmv 
42*6a6c8299Sjmmv 
43*6a6c8299Sjmmv namespace {
44*6a6c8299Sjmmv 
45*6a6c8299Sjmmv 
46*6a6c8299Sjmmv /// Prints all configuration variables.
47*6a6c8299Sjmmv ///
48*6a6c8299Sjmmv /// \param ui Object to interact with the I/O of the program.
49*6a6c8299Sjmmv /// \param properties The key/value map representing all the configuration
50*6a6c8299Sjmmv ///     variables.
51*6a6c8299Sjmmv ///
52*6a6c8299Sjmmv /// \return 0 for success.
53*6a6c8299Sjmmv static int
print_all(cmdline::ui * ui,const config::properties_map & properties)54*6a6c8299Sjmmv print_all(cmdline::ui* ui, const config::properties_map& properties)
55*6a6c8299Sjmmv {
56*6a6c8299Sjmmv     for (config::properties_map::const_iterator iter = properties.begin();
57*6a6c8299Sjmmv          iter != properties.end(); iter++)
58*6a6c8299Sjmmv         ui->out(F("%s = %s") % (*iter).first % (*iter).second);
59*6a6c8299Sjmmv     return EXIT_SUCCESS;
60*6a6c8299Sjmmv }
61*6a6c8299Sjmmv 
62*6a6c8299Sjmmv 
63*6a6c8299Sjmmv /// Prints the configuration variables that the user requests.
64*6a6c8299Sjmmv ///
65*6a6c8299Sjmmv /// \param ui Object to interact with the I/O of the program.
66*6a6c8299Sjmmv /// \param properties The key/value map representing all the configuration
67*6a6c8299Sjmmv ///     variables.
68*6a6c8299Sjmmv /// \param filters The names of the configuration variables to print.
69*6a6c8299Sjmmv ///
70*6a6c8299Sjmmv /// \return 0 if all specified filters are valid; 1 otherwise.
71*6a6c8299Sjmmv static int
print_some(cmdline::ui * ui,const config::properties_map & properties,const cmdline::args_vector & filters)72*6a6c8299Sjmmv print_some(cmdline::ui* ui, const config::properties_map& properties,
73*6a6c8299Sjmmv            const cmdline::args_vector& filters)
74*6a6c8299Sjmmv {
75*6a6c8299Sjmmv     bool ok = true;
76*6a6c8299Sjmmv 
77*6a6c8299Sjmmv     for (cmdline::args_vector::const_iterator iter = filters.begin();
78*6a6c8299Sjmmv          iter != filters.end(); iter++) {
79*6a6c8299Sjmmv         const config::properties_map::const_iterator match =
80*6a6c8299Sjmmv             properties.find(*iter);
81*6a6c8299Sjmmv         if (match == properties.end()) {
82*6a6c8299Sjmmv             cmdline::print_warning(ui, F("'%s' is not defined") % *iter);
83*6a6c8299Sjmmv             ok = false;
84*6a6c8299Sjmmv         } else
85*6a6c8299Sjmmv             ui->out(F("%s = %s") % (*match).first % (*match).second);
86*6a6c8299Sjmmv     }
87*6a6c8299Sjmmv 
88*6a6c8299Sjmmv     return ok ? EXIT_SUCCESS : EXIT_FAILURE;
89*6a6c8299Sjmmv }
90*6a6c8299Sjmmv 
91*6a6c8299Sjmmv 
92*6a6c8299Sjmmv }  // anonymous namespace
93*6a6c8299Sjmmv 
94*6a6c8299Sjmmv 
95*6a6c8299Sjmmv /// Default constructor for cmd_config.
cmd_config(void)96*6a6c8299Sjmmv cmd_config::cmd_config(void) : cli_command(
97*6a6c8299Sjmmv     "config", "[variable1 .. variableN]", 0, -1,
98*6a6c8299Sjmmv     "Inspects the values of configuration variables")
99*6a6c8299Sjmmv {
100*6a6c8299Sjmmv }
101*6a6c8299Sjmmv 
102*6a6c8299Sjmmv 
103*6a6c8299Sjmmv /// Entry point for the "config" subcommand.
104*6a6c8299Sjmmv ///
105*6a6c8299Sjmmv /// \param ui Object to interact with the I/O of the program.
106*6a6c8299Sjmmv /// \param cmdline Representation of the command line to the subcommand.
107*6a6c8299Sjmmv /// \param user_config The runtime configuration of the program.
108*6a6c8299Sjmmv ///
109*6a6c8299Sjmmv /// \return 0 if everything is OK, 1 if any of the necessary documents cannot be
110*6a6c8299Sjmmv /// opened.
111*6a6c8299Sjmmv int
run(cmdline::ui * ui,const cmdline::parsed_cmdline & cmdline,const config::tree & user_config)112*6a6c8299Sjmmv cmd_config::run(cmdline::ui* ui, const cmdline::parsed_cmdline& cmdline,
113*6a6c8299Sjmmv                const config::tree& user_config)
114*6a6c8299Sjmmv {
115*6a6c8299Sjmmv     const config::properties_map properties = user_config.all_properties();
116*6a6c8299Sjmmv     if (cmdline.arguments().empty())
117*6a6c8299Sjmmv         return print_all(ui, properties);
118*6a6c8299Sjmmv     else
119*6a6c8299Sjmmv         return print_some(ui, properties, cmdline.arguments());
120*6a6c8299Sjmmv }
121