bestool/
actions.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
use clap::Subcommand;
use miette::Result;
use tracing::{debug, trace};

pub use context::Context;
pub mod context;

#[macro_export]
macro_rules! subcommands {
	(
		[$argtype:ty => $ctxcode:block]($ctxmethod:ident)
		$(
			$(#[$meta:meta])*
			$modname:ident => $enumname:ident($argname:ident)
		),+
	) => {
		$(
			$(#[$meta])*
			pub mod $modname;
		)*

		#[derive(Debug, Clone, Subcommand)]
		pub enum Action {
			$(
				$(#[$meta])*
				$enumname($modname::$argname),
			)*
		}

		pub async fn run(ctx: $argtype) -> Result<()> {
			let ctxfn = $ctxcode;
			match ctxfn(ctx)? {
				$(
					$(#[$meta])*
					(Action::$enumname(args), ctx) => $modname::run(ctx.$ctxmethod(args)).await,
				)*
			}
		}
	};
}
#[allow(unused_imports)]
pub use subcommands;

use crate::args::Args;

subcommands! {
	[Args => {|args: Args| -> Result<(Action, Context<()>)> {
		debug!(version=%env!("CARGO_PKG_VERSION"), "starting up");
		trace!(action=?args.action, "action");
		Ok((args.action, Context::new()))
	}}](with_top)

	#[cfg(feature = "caddy")]
	caddy => Caddy(CaddyArgs),
	#[cfg(feature = "completions")]
	completions => Completions(CompletionsArgs),
	#[cfg(feature = "crypto")]
	crypto => Crypto(CryptoArgs),
	#[cfg(feature = "dyndns")]
	dyndns => Dyndns(DyndnsArgs),
	#[cfg(feature = "__iti")]
	iti => Iti(ItiArgs),
	#[cfg(feature = "self-update")]
	self_update => SelfUpdate(SelfUpdateArgs),
	#[cfg(feature = "ssh")]
	ssh => Ssh(SshArgs),
	#[cfg(feature = "__tamanu")]
	tamanu => Tamanu(TamanuArgs),
	#[cfg(feature = "walg")]
	walg => WalG(WalgArgs)
}