1macro_rules! _print {
2 (@$color:ident, $ptr:ident, $($args:tt)+) => {{
3 use ::std::io::Write;
4
5 let mut ptr = $crate::printer::$ptr();
6 ptr.set_color($crate::printer::Color::$color);
7 let _ = ::std::write!(ptr, $($args)+);
8 ptr.reset();
9 }};
10 ($ptr:ident, $($args:tt)+) => {{
11 use ::std::io::Write;
12
13 let _ = ::std::write!($crate::printer::$ptr(), $($args)+);
14 }};
15}
16
17macro_rules! cli_print {
19 (@$color:ident, $($args:tt)+) => {{
20 if $crate::log::log_level() <= &$crate::log::LogLevel::Info {
21 _print!(@$color, printer, $($args)+);
22 }
23 }};
24 ($($args:tt)+) => {{
25 if $crate::log::log_level() <= &$crate::log::LogLevel::Info {
26 _print!(printer, $($args)+);
27 }
28 }};
29}
30
31macro_rules! cli_println {
33 (@$color:ident, $($args:tt)+) => {{
34 cli_print!(@$color, $($args)+);
35 cli_print!("\n");
36 }};
37 ($($args:tt)+) => {{
39 cli_print!($($args)+);
40 cli_print!("\n");
41 }}
42}
43
44macro_rules! cli_eprint {
46 (@$color:ident, $($args:tt)+) => {{
47 if $crate::log::log_level() <= &$crate::log::LogLevel::Error {
48 _print!(@$color, eprinter, $($args)+);
49 }
50 }};
51 ($($args:tt)+) => {{
52 if $crate::log::log_level() <= &$crate::log::LogLevel::Error {
53 _print!(eprinter, $($args)+);
54 }
55 }}
56}
57
58macro_rules! cli_eprintln {
60 (@$color:ident, $($args:tt)+) => {{
61 cli_eprint!(@$color, $($args)+);
62 cli_eprint!("\n");
63 }};
64 ($($args:tt)*) => {{
65 cli_eprint!($($args)+);
66 cli_eprint!("\n");
67 }}
68}
69
70#[allow(unused_macros)]
72macro_rules! cli_bail {
73 (@impl $prefix:expr, $status:expr, $($args:tt)*) => {{
74 cli_eprint!(@Red, $prefix);
75 cli_eprintln!($($args)+);
76 ::std::process::exit($status);
77 }};
78 (@prefix $prefix:expr, @status $status:expr, $($args:tt)+) => {{
79 cli_bail!(@impl $prefix, $status, $($args)+);
80 }};
81 (@status $status:expr, $($args:tt)+) => {{
82 cli_bail!(@impl "error: ", $status, $($args)+);
83 }};
84 (@prefix $prefix:expr, $($args:tt)+) => {{
85 cli_bail!(@impl $prefix, 1, $($args)+);
86 }};
87 ($($args:tt)*) => {{
88 cli_bail!(@impl "error: ", 1, $($args)+);
89 }};
90}
91
92macro_rules! cli_warn {
99 (@prefix, @$color:ident, $($args:tt)+) => {{
100 if $crate::log::log_level() <= &$crate::log::LogLevel::Warn {
101 _print!(@Yellow, printer, "warn: ");
102 _print!(@$color, printer, $($args)+);
103 }
104 }};
105 (@prefix, $($args:tt)+) => {{
106 if $crate::log::log_level() <= &$crate::log::LogLevel::Warn {
107 _print!(printer, "warn: ");
108 _print!(printer, $($args)+);
109 }
110 }};
111 (@$color:ident, $($args:tt)+) => {{
112 if $crate::log::log_level() <= &$crate::log::LogLevel::Warn {
113 _print!(@$color, printer, $($args)+);
114 }
115 }};
116 ($($args:tt)+) => {{
117 if $crate::log::log_level() <= &$crate::log::LogLevel::Warn {
118 _print!(printer, $($args)+);
119 }
120 }};
121}
122
123macro_rules! cli_warnln {
130 (@noprefix, @$color:ident, $($args:tt)+) => {{
131 cli_warn!(@$color, $($args)+);
132 cli_warn!("\n");
133 }};
134 (@noprefix, $($args:tt)+) => {{
136 cli_warn!($($args)+);
137 cli_warn!("\n");
138 }};
139 (@$color:ident, $($args:tt)+) => {{
140 cli_warn!(@prefix, @$color, $($args)+);
141 cli_warn!("\n");
142 }};
143 ($($args:tt)+) => {{
145 cli_warn!(@prefix, $($args)+);
146 cli_warn!("\n");
147 }}
148}
149
150macro_rules! cli_debug {
157 (@prefix, @$color:ident, $($args:tt)+) => {{
158 if $crate::log::log_level() <= &$crate::log::LogLevel::Debug {
159 _print!(@$color, eprinter, "DEBUG: ");
160 _print!(@$color, eprinter, $($args)+);
161 }
162 }};
163 (@prefix, $($args:tt)+) => {{
164 if $crate::log::log_level() <= &$crate::log::LogLevel::Debug {
165 _print!(eprinter, "DEBUG: ");
166 _print!(eprinter, $($args)+);
167 }
168 }};
169 (@$color:ident, $($args:tt)+) => {{
170 if $crate::log::log_level() <= &$crate::log::LogLevel::Debug {
171 _print!(@$color, eprinter, $($args)+);
172 }
173 }};
174 ($($args:tt)+) => {{
175 if $crate::log::log_level() <= &$crate::log::LogLevel::Debug {
176 _print!(eprinter, $($args)+);
177 }
178 }};
179}
180
181macro_rules! cli_debugln {
188 (@prefix, @$color:ident, $($args:tt)+) => {{
189 cli_debug!(@prefix, @$color, $($args)+);
190 cli_debug!("\n");
191 }};
192 (@prefix, $($args:tt)+) => {{
194 cli_debug!(@prefix, $($args)+);
195 cli_debug!("\n");
196 }};
197 (@$color:ident, $($args:tt)+) => {{
198 cli_debug!(@$color, $($args)+);
199 cli_debug!("\n");
200 }};
201 ($($args:tt)+) => {{
203 cli_debug!($($args)+);
204 cli_debug!("\n");
205 }}
206}
207
208macro_rules! cli_trace {
215 (@prefix, @$color:ident, $($args:tt)+) => {{
216 if $crate::log::log_level() <= &$crate::log::LogLevel::Trace {
217 _print!(@$color, eprinter, "TRACE: ");
218 _print!(@$color, eprinter, $($args)+);
219 }
220 }};
221 (@prefix, $($args:tt)+) => {{
222 if $crate::log::log_level() <= &$crate::log::LogLevel::Trace {
223 _print!(eprinter, "TRACE: ");
224 _print!(eprinter, $($args)+);
225 }
226 }};
227 (@$color:ident, $($args:tt)+) => {{
228 if $crate::log::log_level() <= &$crate::log::LogLevel::Trace {
229 _print!(@$color, eprinter, $($args)+);
230 }
231 }};
232 ($($args:tt)+) => {{
233 if $crate::log::log_level() <= &$crate::log::LogLevel::Trace {
234 _print!(eprinter, $($args)+);
235 }
236 }};
237}
238
239macro_rules! cli_traceln {
246 (@prefix, @$color:ident, $($args:tt)+) => {{
247 cli_trace!(@prefix, @$color, $($args)+);
248 cli_trace!("\n");
249 }};
250 (@prefix, $($args:tt)+) => {{
252 cli_trace!(@prefix, $($args)+);
253 cli_trace!("\n");
254 }};
255 (@$color:ident, $($args:tt)+) => {{
256 cli_trace!(@$color, $($args)+);
257 cli_trace!("\n");
258 }};
259 ($($args:tt)+) => {{
261 cli_trace!($($args)+);
262 cli_trace!("\n");
263 }}
264}
265
266#[macro_export]
310macro_rules! arg {
311 (@arg ($arg:expr) ) => { $arg };
312 (@arg ($arg:expr) --$long:ident $($tail:tt)*) => {
313 arg!(@arg ($arg.long(stringify!($long)).action(::clap::ArgAction::SetTrue)) $($tail)* )
314 };
315 (@arg ($arg:expr) -($short:expr) $($tail:tt)*) => {
316 arg!(@arg ($arg.short($short).action(::clap::ArgAction::SetTrue)) $($tail)* )
317 };
318 (@arg ($arg:expr) | ($alias:expr) $($tail:tt)*) => {
319 arg!(@arg ($arg.visible_alias($alias)) $($tail)* )
320 };
321 (@arg ($arg:expr) | $alias:ident $($tail:tt)*) => {
322 arg!(@arg ($arg.visible_alias(stringify!($alias))) $($tail)* )
323 };
324 (@arg ($arg:expr) ... $($tail:tt)*) => {
325 arg!(@arg ({
326 let arg = $arg.value_delimiter(',').action(::clap::ArgAction::Append);
327 if arg.get_long().is_some() || arg.get_short().is_some() {
328 arg.number_of_values(1)
329 } else {
330 arg
331 }
332 }) $($tail)* )
333 };
334 (@arg ($arg:expr) =[$var:expr$(=>$default:expr)?] $($tail:tt)*) => {
335 arg!(@arg ({
336 #[allow(unused_mut)]
337 let mut a = $arg.value_name($var).action(::clap::ArgAction::Set);
338 $(
339 a = a.default_value($default);
340 )?
341 a
342 }) $($tail)*)
343 };
344 (@arg ($arg:expr) !$ident:ident $($tail:tt)*) => {
346 arg!(@arg ($arg.$ident(false)) $($tail)*)
347 };
348 (@arg ($arg:expr) $ident:ident $($tail:tt)*) => {
350 arg!(@arg ($arg.$ident(true)) $($tail)*)
351 };
352 ($name:ident $($tail:tt)*) => {
353 arg!(@arg (::clap::Arg::new(stringify!($name))) $($tail)* )
354 };
355 (--($name:expr) $($tail:tt)*) => {
356 arg!(@arg (::clap::Arg::new($name)
357 .long($name)
358 .action(::clap::ArgAction::SetTrue))
359 $($tail)*
360 )
361 };
362 (--$name:ident $($tail:tt)*) => {
363 arg!(@arg (::clap::Arg::new(stringify!($name))
364 .long(stringify!($name))
365 .action(::clap::ArgAction::SetTrue))
366 $($tail)*
367 )
368 };
369}
370
371macro_rules! maybe_base64_arg {
374 ($m:expr, $arg:expr, $is_base64:expr) => {
375 if let Some(raw_key) = $m.get_one::<String>($arg) {
376 let engine = ::base64::engine::fast_portable::FastPortable::from(
377 &::base64::alphabet::URL_SAFE,
378 ::base64::engine::fast_portable::NO_PAD,
379 );
380 if $is_base64 {
381 let _ = ::base64::decode_engine(raw_key, &engine)?;
382 Some(raw_key.to_string())
383 } else {
384 Some(::base64::encode_engine(raw_key, &engine))
385 }
386 } else {
387 None
388 }
389 };
390}
391
392macro_rules! vec_remove_if {
394 ($v:expr, $f:expr) => {{
395 let idx: Vec<_> = $v
396 .iter()
397 .enumerate()
398 .rev()
399 .filter_map(|(i, item)| if $f(item) { Some(i) } else { None })
400 .collect();
401 let mut ret = Vec::new();
402 for i in idx {
403 ret.push($v.swap_remove(i));
404 }
405 ret
406 }};
407}
408
409macro_rules! maybe_retry {
413 ($this:ident . $fn:ident ( $($arg:expr),* ) ) => {{
414 if $this.inner.is_none() {
415 $this.refresh_inner()?;
416 }
417 let req = &mut $this.inner.as_mut().unwrap();
418
419 let res = match req.$fn($( $arg ),*) {
420 Ok(ret) => Ok(ret),
421 Err(SeaplaneError::ApiResponse(ae))
422 if ae.kind == ApiErrorKind::Unauthorized =>
423 {
424 $this.token = Some(request_token(
425 &$this.api_key,
426 $this.identity_url.as_ref(),
427 $this.insecure_urls,
428 $this.invalid_certs)?);
429 Ok(req.$fn($( $arg ,)*)?)
430 }
431 Err(e) => Err(e),
432 };
433 res.map_err(CliError::from)
434 }};
435}
436
437macro_rules! maybe_retry_cloned {
439 ($this:ident . $fn:ident ( $($arg:expr),* ) ) => {{
440 maybe_retry!($this.$fn($( $arg.clone() ),*))
441 }};
442}