1 | /* Copyright © 2005-2007 Roger Leigh <rleigh@debian.org> |
---|
2 | * |
---|
3 | * schroot is free software: you can redistribute it and/or modify it |
---|
4 | * under the terms of the GNU General Public License as published by |
---|
5 | * the Free Software Foundation, either version 3 of the License, or |
---|
6 | * (at your option) any later version. |
---|
7 | * |
---|
8 | * schroot is distributed in the hope that it will be useful, but |
---|
9 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
11 | * General Public License for more details. |
---|
12 | * |
---|
13 | * You should have received a copy of the GNU General Public License |
---|
14 | * along with this program. If not, see |
---|
15 | * <http://www.gnu.org/licenses/>. |
---|
16 | * |
---|
17 | *********************************************************************/ |
---|
18 | |
---|
19 | #include <config.h> |
---|
20 | |
---|
21 | #include "schroot-options.h" |
---|
22 | |
---|
23 | #include <cstdlib> |
---|
24 | #include <iostream> |
---|
25 | |
---|
26 | #include <boost/format.hpp> |
---|
27 | #include <boost/program_options.hpp> |
---|
28 | |
---|
29 | using std::endl; |
---|
30 | using boost::format; |
---|
31 | using sbuild::_; |
---|
32 | namespace opt = boost::program_options; |
---|
33 | using namespace schroot; |
---|
34 | |
---|
35 | const options_base::action_type options_base::ACTION_SESSION_AUTO ("session_auto"); |
---|
36 | const options_base::action_type options_base::ACTION_SESSION_BEGIN ("session_begin"); |
---|
37 | const options_base::action_type options_base::ACTION_SESSION_RECOVER ("session_recover"); |
---|
38 | const options_base::action_type options_base::ACTION_SESSION_RUN ("session_run"); |
---|
39 | const options_base::action_type options_base::ACTION_SESSION_END ("session_end"); |
---|
40 | const options_base::action_type options_base::ACTION_LIST ("list"); |
---|
41 | const options_base::action_type options_base::ACTION_INFO ("info"); |
---|
42 | const options_base::action_type options_base::ACTION_LOCATION ("location"); |
---|
43 | const options_base::action_type options_base::ACTION_CONFIG ("config"); |
---|
44 | |
---|
45 | options_base::options_base (): |
---|
46 | schroot_base::options (), |
---|
47 | chroots(), |
---|
48 | command(), |
---|
49 | user(), |
---|
50 | preserve(false), |
---|
51 | all(false), |
---|
52 | all_chroots(false), |
---|
53 | all_sessions(false), |
---|
54 | session_name(), |
---|
55 | session_force(false), |
---|
56 | chroot(_("Chroot selection")), |
---|
57 | chrootenv(_("Chroot environment")), |
---|
58 | session_actions(_("Session actions")), |
---|
59 | session_options(_("Session options")) |
---|
60 | { |
---|
61 | } |
---|
62 | |
---|
63 | options_base::~options_base () |
---|
64 | { |
---|
65 | } |
---|
66 | |
---|
67 | void |
---|
68 | options_base::add_options () |
---|
69 | { |
---|
70 | // Chain up to add basic options. |
---|
71 | schroot_base::options::add_options(); |
---|
72 | |
---|
73 | action.add(ACTION_SESSION_AUTO); |
---|
74 | action.set_default(ACTION_SESSION_AUTO); |
---|
75 | action.add(ACTION_SESSION_BEGIN); |
---|
76 | action.add(ACTION_SESSION_RECOVER); |
---|
77 | action.add(ACTION_SESSION_RUN); |
---|
78 | action.add(ACTION_SESSION_END); |
---|
79 | action.add(ACTION_LIST); |
---|
80 | action.add(ACTION_INFO); |
---|
81 | action.add(ACTION_LOCATION); |
---|
82 | action.add(ACTION_CONFIG); |
---|
83 | |
---|
84 | actions.add_options() |
---|
85 | ("list,l", |
---|
86 | _("List available chroots")) |
---|
87 | ("info,i", |
---|
88 | _("Show information about selected chroots")) |
---|
89 | ("config", |
---|
90 | _("Dump configuration of selected chroots")); |
---|
91 | |
---|
92 | chroot.add_options() |
---|
93 | ("chroot,c", opt::value<sbuild::string_list>(&this->chroots), |
---|
94 | _("Use specified chroot")); |
---|
95 | |
---|
96 | hidden.add_options() |
---|
97 | ("command", opt::value<sbuild::string_list>(&this->command), |
---|
98 | _("Command to run")); |
---|
99 | |
---|
100 | positional.add("command", -1); |
---|
101 | } |
---|
102 | |
---|
103 | void |
---|
104 | options_base::add_option_groups () |
---|
105 | { |
---|
106 | // Chain up to add basic option groups. |
---|
107 | schroot_base::options::add_option_groups(); |
---|
108 | |
---|
109 | #ifndef BOOST_PROGRAM_OPTIONS_DESCRIPTION_OLD |
---|
110 | if (!chroot.options().empty()) |
---|
111 | #else |
---|
112 | if (!chroot.primary_keys().empty()) |
---|
113 | #endif |
---|
114 | { |
---|
115 | visible.add(chroot); |
---|
116 | global.add(chroot); |
---|
117 | } |
---|
118 | #ifndef BOOST_PROGRAM_OPTIONS_DESCRIPTION_OLD |
---|
119 | if (!chrootenv.options().empty()) |
---|
120 | #else |
---|
121 | if (!chrootenv.primary_keys().empty()) |
---|
122 | #endif |
---|
123 | { |
---|
124 | visible.add(chrootenv); |
---|
125 | global.add(chrootenv); |
---|
126 | } |
---|
127 | #ifndef BOOST_PROGRAM_OPTIONS_DESCRIPTION_OLD |
---|
128 | if (!session_actions.options().empty()) |
---|
129 | #else |
---|
130 | if (!session_actions.primary_keys().empty()) |
---|
131 | #endif |
---|
132 | { |
---|
133 | visible.add(session_actions); |
---|
134 | global.add(session_actions); |
---|
135 | } |
---|
136 | |
---|
137 | #ifndef BOOST_PROGRAM_OPTIONS_DESCRIPTION_OLD |
---|
138 | if (!session_options.options().empty()) |
---|
139 | #else |
---|
140 | if (!session_options.primary_keys().empty()) |
---|
141 | #endif |
---|
142 | { |
---|
143 | visible.add(session_options); |
---|
144 | global.add(session_options); |
---|
145 | } |
---|
146 | } |
---|
147 | |
---|
148 | void |
---|
149 | options_base::check_options () |
---|
150 | { |
---|
151 | // Chain up to check basic options. |
---|
152 | schroot_base::options::check_options(); |
---|
153 | |
---|
154 | if (vm.count("list")) |
---|
155 | this->action = ACTION_LIST; |
---|
156 | if (vm.count("info")) |
---|
157 | this->action = ACTION_INFO; |
---|
158 | if (vm.count("config")) |
---|
159 | this->action = ACTION_CONFIG; |
---|
160 | } |
---|
161 | |
---|
162 | void |
---|
163 | options_base::check_actions () |
---|
164 | { |
---|
165 | // Chain up to check basic actions. |
---|
166 | schroot_base::options::check_actions(); |
---|
167 | |
---|
168 | if (this->quiet && this->verbose) |
---|
169 | { |
---|
170 | sbuild::log_warning() |
---|
171 | << _("--quiet and --verbose may not be used at the same time") |
---|
172 | << endl; |
---|
173 | sbuild::log_info() << _("Using verbose output") << endl; |
---|
174 | } |
---|
175 | |
---|
176 | if (!this->chroots.empty() && all_used()) |
---|
177 | { |
---|
178 | sbuild::log_warning() |
---|
179 | << _("--chroot and --all may not be used at the same time") |
---|
180 | << endl; |
---|
181 | sbuild::log_info() << _("Using --chroots only") << endl; |
---|
182 | this->all = this->all_chroots = this->all_sessions = false; |
---|
183 | } |
---|
184 | |
---|
185 | /* Determine which chroots to load and use. */ |
---|
186 | if (this->action == ACTION_SESSION_AUTO) |
---|
187 | { |
---|
188 | // Only allow normal chroots |
---|
189 | this->load_chroots = true; |
---|
190 | this->load_sessions = false; |
---|
191 | this->all = this->all_sessions = false; |
---|
192 | |
---|
193 | // If no chroot was specified, fall back to the "default" chroot. |
---|
194 | if (this->chroots.empty() && all_used() == false) |
---|
195 | this->chroots.push_back("default"); |
---|
196 | } |
---|
197 | else if (this->action == ACTION_SESSION_BEGIN) |
---|
198 | { |
---|
199 | // Only allow one session chroot |
---|
200 | this->load_chroots = true; |
---|
201 | this->load_sessions = false; |
---|
202 | if (this->chroots.size() != 1 || all_used()) |
---|
203 | throw opt::validation_error |
---|
204 | (_("Exactly one chroot must be specified when beginning a session")); |
---|
205 | |
---|
206 | this->all = this->all_chroots = this->all_sessions = false; |
---|
207 | } |
---|
208 | else if (this->action == ACTION_SESSION_RECOVER || |
---|
209 | this->action == ACTION_SESSION_RUN || |
---|
210 | this->action == ACTION_SESSION_END) |
---|
211 | { |
---|
212 | // Session operations work on all chroots. |
---|
213 | this->load_chroots = this->load_sessions = true; |
---|
214 | |
---|
215 | if (!this->session_name.empty()) |
---|
216 | throw opt::validation_error |
---|
217 | (_("--session-name is not permitted for the specified action; did you mean to use --chroot?")); |
---|
218 | } |
---|
219 | else if (this->action == ACTION_HELP || |
---|
220 | this->action == ACTION_VERSION) |
---|
221 | { |
---|
222 | // Chroots don't make sense here. |
---|
223 | this->load_chroots = this->load_sessions = false; |
---|
224 | this->all = this->all_chroots = this->all_sessions = false; |
---|
225 | } |
---|
226 | else if (this->action == ACTION_LIST) |
---|
227 | { |
---|
228 | // If not specified otherwise, load normal chroots, but allow |
---|
229 | // --all options. |
---|
230 | if (!all_used()) |
---|
231 | this->load_chroots = true; |
---|
232 | if (this->all_chroots) |
---|
233 | this->load_chroots = true; |
---|
234 | if (this->all_sessions) |
---|
235 | this->load_sessions = true; |
---|
236 | if (!this->chroots.empty()) |
---|
237 | throw opt::validation_error(_("--chroot may not be used with --list")); |
---|
238 | } |
---|
239 | else if (this->action == ACTION_INFO || |
---|
240 | this->action == ACTION_LOCATION || |
---|
241 | this->action == ACTION_CONFIG) |
---|
242 | { |
---|
243 | // If not specified otherwise, load normal chroots, but allow |
---|
244 | // --all options. |
---|
245 | if (!this->chroots.empty()) // chroot specified |
---|
246 | this->load_chroots = this->load_sessions = true; |
---|
247 | else if (!all_used()) // no chroots specified |
---|
248 | { |
---|
249 | this->all_chroots = true; |
---|
250 | this->load_chroots = true; |
---|
251 | } |
---|
252 | if (this->all_chroots) |
---|
253 | this->load_chroots = true; |
---|
254 | if (this->all_sessions) |
---|
255 | this->load_sessions = true; |
---|
256 | } |
---|
257 | else |
---|
258 | { |
---|
259 | // Something went wrong |
---|
260 | this->load_chroots = this->load_sessions = false; |
---|
261 | this->all = this->all_chroots = this->all_sessions = false; |
---|
262 | throw opt::validation_error(_("Unknown action specified")); |
---|
263 | } |
---|
264 | } |
---|