1mod admission;
2mod apps;
3mod auth;
4mod build;
5mod cli;
6mod component;
7mod cycles;
8mod diagnostic;
9mod endpoints;
10mod evidence;
11mod evidence_support;
12mod fleet;
13mod frontend;
14mod info;
15mod info_env;
16mod info_subnets;
17mod inspect;
18mod list;
19mod medic;
20mod metrics;
21mod network;
22mod observatory;
23mod output;
24mod replica;
25mod scaffold;
26mod state;
27mod status;
28mod support;
29#[cfg(test)]
30mod test_support;
31mod token;
32mod toolchain;
33
34use crate::cli::{
35 argv::trace_if_enabled,
36 clap::parse_matches,
37 globals::{
38 DISPATCH_ARGS, apply_global_environment, apply_global_icp, global_environment_conflict,
39 misplaced_global_option, unsupported_global_option,
40 },
41};
42use clap::error::ErrorKind;
43pub use cli::top_level_command;
44pub use frontend::FrontendCommandError;
45use std::ffi::OsString;
46use thiserror::Error as ThisError;
47
48const VERSION_TEXT: &str = concat!("canic ", env!("CARGO_PKG_VERSION"));
49
50#[derive(Debug, ThisError)]
55pub enum CliError {
56 #[error("admission: {0}")]
57 Admission(#[source] Box<admission::AdmissionCommandError>),
58
59 #[error(transparent)]
60 Clap(#[from] clap::Error),
61
62 #[error("build: {0}")]
63 Build(#[from] build::BuildCommandError),
64
65 #[error("component: {0}")]
66 Component(#[source] Box<component::ComponentCommandError>),
67
68 #[error("cycles: {0}")]
69 Cycles(#[source] Box<cycles::CyclesCommandError>),
70
71 #[error("auth: {0}")]
72 Auth(#[source] Box<auth::AuthCommandError>),
73
74 #[error("diagnostic: {0}")]
75 Diagnostic(#[from] diagnostic::DiagnosticCommandError),
76
77 #[error("evidence: {0}")]
78 Evidence(#[from] evidence::EvidenceCommandError),
79
80 #[error("fleet: {0}")]
81 Fleet(#[source] Box<fleet::FleetCommandError>),
82
83 #[error("frontend: {0}")]
84 Frontend(#[source] Box<frontend::FrontendCommandError>),
85
86 #[error("info: {0}")]
87 Info(#[from] info::InfoCommandError),
88
89 #[error("inspect: {0}")]
90 Inspect(#[source] Box<inspect::InspectCommandError>),
91
92 #[error("medic: {0}")]
93 Medic(#[source] Box<medic::MedicCommandError>),
94
95 #[error("network: {0}")]
96 Network(#[from] network::NetworkCommandError),
97
98 #[error("observatory: {0}")]
99 Observatory(#[from] observatory::ObservatoryCommandError),
100
101 #[error("app: {0}")]
102 Apps(#[source] Box<apps::AppCommandError>),
103
104 #[error("state: {0}")]
105 State(#[from] state::StateCommandError),
106
107 #[error("status: {0}")]
108 Status(#[from] status::StatusCommandError),
109
110 #[error("token: {0}")]
111 Token(#[source] Box<token::TokenCommandError>),
112
113 #[error("toolchain: {0}")]
114 Toolchain(#[from] toolchain::ToolchainCommandError),
115
116 #[error("scaffold: {0}")]
117 Scaffold(#[from] scaffold::ScaffoldCommandError),
118
119 #[error("replica: {0}")]
120 Replica(#[from] replica::ReplicaCommandError),
121}
122
123impl From<apps::AppCommandError> for CliError {
124 fn from(error: apps::AppCommandError) -> Self {
125 Self::Apps(Box::new(error))
126 }
127}
128
129impl From<admission::AdmissionCommandError> for CliError {
130 fn from(error: admission::AdmissionCommandError) -> Self {
131 Self::Admission(Box::new(error))
132 }
133}
134
135impl From<auth::AuthCommandError> for CliError {
136 fn from(error: auth::AuthCommandError) -> Self {
137 Self::Auth(Box::new(error))
138 }
139}
140
141impl From<cycles::CyclesCommandError> for CliError {
142 fn from(error: cycles::CyclesCommandError) -> Self {
143 Self::Cycles(Box::new(error))
144 }
145}
146
147impl From<fleet::FleetCommandError> for CliError {
148 fn from(error: fleet::FleetCommandError) -> Self {
149 Self::Fleet(Box::new(error))
150 }
151}
152
153impl From<inspect::InspectCommandError> for CliError {
154 fn from(error: inspect::InspectCommandError) -> Self {
155 Self::Inspect(Box::new(error))
156 }
157}
158
159impl From<medic::MedicCommandError> for CliError {
160 fn from(error: medic::MedicCommandError) -> Self {
161 Self::Medic(Box::new(error))
162 }
163}
164
165impl From<token::TokenCommandError> for CliError {
166 fn from(error: token::TokenCommandError) -> Self {
167 Self::Token(Box::new(error))
168 }
169}
170
171pub fn run_from_env() -> Result<(), CliError> {
173 let argv = std::env::args_os().collect::<Vec<_>>();
174 trace_if_enabled(&argv);
175 run(argv.into_iter().skip(1))
176}
177
178pub fn run<I>(args: I) -> Result<(), CliError>
180where
181 I: IntoIterator<Item = OsString>,
182{
183 let mut args = args.into_iter().collect::<Vec<_>>();
184 if args.is_empty() {
185 args.push(OsString::from("--help"));
186 }
187 if let Some(option) = misplaced_global_option(&args) {
188 let error = top_level_command().error(
189 ErrorKind::UnknownArgument,
190 format!("unexpected argument '{option}' found; put it before the command"),
191 );
192 return Err(CliError::Clap(error));
193 }
194 let matches = match parse_matches(top_level_command(), args) {
195 Ok(matches) => matches,
196 Err(error) if !error.use_stderr() => {
197 let _ = error.print();
198 return Ok(());
199 }
200 Err(error) => return Err(CliError::Clap(error)),
201 };
202 let global_icp = cli::clap::string_option(&matches, "icp");
203 let global_environment = cli::clap::string_option(&matches, "environment");
204
205 let (command, subcommand_matches) = matches
206 .subcommand()
207 .unwrap_or_else(|| unreachable!("Clap requires one top-level command"));
208 let mut tail = subcommand_matches
209 .get_many::<OsString>(DISPATCH_ARGS)
210 .map(|values| values.cloned().collect::<Vec<_>>())
211 .unwrap_or_default();
212 if let Some(option) = unsupported_global_option(
213 command,
214 &tail,
215 global_icp.as_deref(),
216 global_environment.as_deref(),
217 ) {
218 return Err(CliError::Clap(top_level_command().error(
219 ErrorKind::UnknownArgument,
220 format!("global option '{option}' is not supported by '{command}'"),
221 )));
222 }
223 if let Some(conflict) =
224 global_environment_conflict(command, &tail, global_environment.as_deref())
225 {
226 return Err(CliError::Clap(
227 top_level_command().error(ErrorKind::InvalidValue, conflict.to_string()),
228 ));
229 }
230 apply_global_icp(command, &mut tail, global_icp);
231 apply_global_environment(command, &mut tail, global_environment);
232 let tail = tail.into_iter();
233
234 match command {
235 "admission" => admission::run(tail).map_err(CliError::from),
236 "app" => apps::run(tail).map_err(CliError::from),
237 "auth" => auth::run(tail).map_err(CliError::from),
238 "build" => build::run(tail).map_err(CliError::from),
239 "component" => component::run(tail).map_err(|error| CliError::Component(Box::new(error))),
240 "cycles" => cycles::run(tail).map_err(CliError::from),
241 "diagnostic" => diagnostic::run(tail).map_err(CliError::from),
242 "evidence" => evidence::run(tail).map_err(CliError::from),
243 "fleet" => fleet::run(tail).map_err(CliError::from),
244 "frontend" => frontend::run(tail).map_err(|error| CliError::Frontend(Box::new(error))),
245 "info" => info::run(tail).map_err(CliError::from),
246 "inspect" => inspect::run(tail).map_err(CliError::from),
247 "medic" => medic::run(tail).map_err(CliError::from),
248 "network" => network::run(tail).map_err(CliError::from),
249 "observatory" => observatory::run(tail).map_err(CliError::from),
250 "replica" => replica::run(tail).map_err(CliError::from),
251 "scaffold" => scaffold::run(tail).map_err(CliError::from),
252 "state" => state::run(tail).map_err(CliError::from),
253 "status" => status::run(tail).map_err(CliError::from),
254 "token" => token::run(tail).map_err(CliError::from),
255 "toolchain" => toolchain::run(tail).map_err(CliError::from),
256 _ => unreachable!("top-level dispatch command only defines known commands"),
257 }
258}
259
260#[must_use]
261pub const fn version_text() -> &'static str {
262 VERSION_TEXT
263}
264
265#[must_use]
266pub fn render_cli_error(error: &CliError) -> String {
267 match error {
268 CliError::Fleet(err)
269 if matches!(err.as_ref(), fleet::FleetCommandError::JsonReported { .. }) =>
270 {
271 err.to_string()
272 }
273 CliError::Build(build::BuildCommandError::Clap(err)) | CliError::Clap(err) => {
274 err.to_string().trim_end().to_string()
275 }
276 CliError::Inspect(err) if err.suppress_stderr() => String::new(),
277 CliError::Medic(err) if err.suppress_stderr() => String::new(),
278 CliError::State(err) if err.suppress_stderr() => String::new(),
279 _ => error.to_string(),
280 }
281}
282
283#[must_use]
284pub fn cli_error_exit_code(err: &CliError) -> i32 {
285 match err {
286 CliError::Auth(err) => i32::from(err.exit_code()),
287 CliError::Build(err) => err.exit_code(),
288 CliError::Clap(err) => err.exit_code(),
289 CliError::Info(err) => i32::from(err.exit_code()),
290 CliError::Inspect(err) => i32::from(err.exit_code()),
291 CliError::Medic(err) => i32::from(err.exit_code()),
292 CliError::State(err) => i32::from(err.exit_code()),
293 _ => 1,
294 }
295}
296
297#[cfg(test)]
298mod tests;