xref: /netbsd-src/external/bsd/kyua-cli/dist/cli/cmd_config.cpp (revision 6b3a42af15b5e090c339512c790dd68f3d11a9d8)
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/cmd_config.hpp"
30*6b3a42afSjmmv 
31*6b3a42afSjmmv #include <cstdlib>
32*6b3a42afSjmmv 
33*6b3a42afSjmmv #include "cli/common.ipp"
34*6b3a42afSjmmv #include "utils/config/tree.ipp"
35*6b3a42afSjmmv #include "utils/format/macros.hpp"
36*6b3a42afSjmmv 
37*6b3a42afSjmmv namespace cmdline = utils::cmdline;
38*6b3a42afSjmmv namespace config = utils::config;
39*6b3a42afSjmmv 
40*6b3a42afSjmmv using cli::cmd_config;
41*6b3a42afSjmmv 
42*6b3a42afSjmmv 
43*6b3a42afSjmmv namespace {
44*6b3a42afSjmmv 
45*6b3a42afSjmmv 
46*6b3a42afSjmmv /// Prints all configuration variables.
47*6b3a42afSjmmv ///
48*6b3a42afSjmmv /// \param ui Object to interact with the I/O of the program.
49*6b3a42afSjmmv /// \param properties The key/value map representing all the configuration
50*6b3a42afSjmmv ///     variables.
51*6b3a42afSjmmv ///
52*6b3a42afSjmmv /// \return 0 for success.
53*6b3a42afSjmmv static int
print_all(cmdline::ui * ui,const config::properties_map & properties)54*6b3a42afSjmmv print_all(cmdline::ui* ui, const config::properties_map& properties)
55*6b3a42afSjmmv {
56*6b3a42afSjmmv     for (config::properties_map::const_iterator iter = properties.begin();
57*6b3a42afSjmmv          iter != properties.end(); iter++)
58*6b3a42afSjmmv         ui->out(F("%s = %s") % (*iter).first % (*iter).second);
59*6b3a42afSjmmv     return EXIT_SUCCESS;
60*6b3a42afSjmmv }
61*6b3a42afSjmmv 
62*6b3a42afSjmmv 
63*6b3a42afSjmmv /// Prints the configuration variables that the user requests.
64*6b3a42afSjmmv ///
65*6b3a42afSjmmv /// \param ui Object to interact with the I/O of the program.
66*6b3a42afSjmmv /// \param properties The key/value map representing all the configuration
67*6b3a42afSjmmv ///     variables.
68*6b3a42afSjmmv /// \param filters The names of the configuration variables to print.
69*6b3a42afSjmmv ///
70*6b3a42afSjmmv /// \return 0 if all specified filters are valid; 1 otherwise.
71*6b3a42afSjmmv static int
print_some(cmdline::ui * ui,const config::properties_map & properties,const cmdline::args_vector & filters)72*6b3a42afSjmmv print_some(cmdline::ui* ui, const config::properties_map& properties,
73*6b3a42afSjmmv            const cmdline::args_vector& filters)
74*6b3a42afSjmmv {
75*6b3a42afSjmmv     bool ok = true;
76*6b3a42afSjmmv 
77*6b3a42afSjmmv     for (cmdline::args_vector::const_iterator iter = filters.begin();
78*6b3a42afSjmmv          iter != filters.end(); iter++) {
79*6b3a42afSjmmv         const config::properties_map::const_iterator match =
80*6b3a42afSjmmv             properties.find(*iter);
81*6b3a42afSjmmv         if (match == properties.end()) {
82*6b3a42afSjmmv             cmdline::print_warning(ui, F("'%s' is not defined") % *iter);
83*6b3a42afSjmmv             ok = false;
84*6b3a42afSjmmv         } else
85*6b3a42afSjmmv             ui->out(F("%s = %s") % (*match).first % (*match).second);
86*6b3a42afSjmmv     }
87*6b3a42afSjmmv 
88*6b3a42afSjmmv     return ok ? EXIT_SUCCESS : EXIT_FAILURE;
89*6b3a42afSjmmv }
90*6b3a42afSjmmv 
91*6b3a42afSjmmv 
92*6b3a42afSjmmv }  // anonymous namespace
93*6b3a42afSjmmv 
94*6b3a42afSjmmv 
95*6b3a42afSjmmv /// Default constructor for cmd_config.
cmd_config(void)96*6b3a42afSjmmv cmd_config::cmd_config(void) : cli_command(
97*6b3a42afSjmmv     "config", "[variable1 .. variableN]", 0, -1,
98*6b3a42afSjmmv     "Inspects the values of configuration variables")
99*6b3a42afSjmmv {
100*6b3a42afSjmmv }
101*6b3a42afSjmmv 
102*6b3a42afSjmmv 
103*6b3a42afSjmmv /// Entry point for the "config" subcommand.
104*6b3a42afSjmmv ///
105*6b3a42afSjmmv /// \param ui Object to interact with the I/O of the program.
106*6b3a42afSjmmv /// \param cmdline Representation of the command line to the subcommand.
107*6b3a42afSjmmv /// \param user_config The runtime configuration of the program.
108*6b3a42afSjmmv ///
109*6b3a42afSjmmv /// \return 0 if everything is OK, 1 if any of the necessary documents cannot be
110*6b3a42afSjmmv /// opened.
111*6b3a42afSjmmv int
run(cmdline::ui * ui,const cmdline::parsed_cmdline & cmdline,const config::tree & user_config)112*6b3a42afSjmmv cmd_config::run(cmdline::ui* ui, const cmdline::parsed_cmdline& cmdline,
113*6b3a42afSjmmv                const config::tree& user_config)
114*6b3a42afSjmmv {
115*6b3a42afSjmmv     const config::properties_map properties = user_config.all_properties();
116*6b3a42afSjmmv     if (cmdline.arguments().empty())
117*6b3a42afSjmmv         return print_all(ui, properties);
118*6b3a42afSjmmv     else
119*6b3a42afSjmmv         return print_some(ui, properties, cmdline.arguments());
120*6b3a42afSjmmv }
121