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