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