Skip to main content

tuwunel_admin/
admin.rs

1use async_trait::async_trait;
2use clap::{CommandFactory, FromArgMatches, Parser};
3use tuwunel_core::Result;
4
5use crate::{
6	Context,
7	appservice::{self, AppserviceCommand},
8	debug::{self, DebugCommand},
9	federation::{self, FederationCommand},
10	media::{self, MediaCommand},
11	query::{self, QueryCommand},
12	room::{self, RoomCommand},
13	server::{self, ServerCommand},
14	token::{self, TokenCommand},
15	user::{self, UserCommand},
16};
17
18/// Concrete root installed into [`tuwunel_service::admin::Service::command`].
19pub(crate) struct Root;
20
21#[async_trait]
22impl tuwunel_service::admin::Command for Root {
23	fn clap(&self) -> clap::Command { <AdminCommand as CommandFactory>::command() }
24
25	async fn dispatch(
26		&self,
27		matches: clap::ArgMatches,
28		context: &tuwunel_service::admin::Context<'_>,
29	) -> Result {
30		let command = <AdminCommand as FromArgMatches>::from_arg_matches(&matches)?;
31		let context = Context::new(context);
32
33		process(command, &context).await
34	}
35}
36
37#[derive(Debug, Parser)]
38#[command(name = "tuwunel", version = tuwunel_core::version())]
39pub(super) enum AdminCommand {
40	#[command(subcommand)]
41	/// - Commands for managing appservices
42	Appservices(AppserviceCommand),
43
44	#[command(subcommand)]
45	/// - Commands for managing local users
46	Users(UserCommand),
47
48	#[command(subcommand)]
49	/// - Commands for managing rooms
50	Rooms(RoomCommand),
51
52	#[command(subcommand)]
53	/// - Commands for managing federation
54	Federation(FederationCommand),
55
56	#[command(subcommand)]
57	/// - Commands for managing the server
58	Server(ServerCommand),
59
60	#[command(subcommand)]
61	/// - Commands for managing media
62	Media(MediaCommand),
63
64	#[command(subcommand)]
65	/// - Commands for debugging things
66	Debug(DebugCommand),
67
68	#[command(subcommand)]
69	/// - Low-level queries for database getters and iterators
70	Query(QueryCommand),
71
72	#[command(subcommand)]
73	/// - Commands for managing registration tokens
74	Token(TokenCommand),
75}
76
77#[tracing::instrument(skip_all, name = "command")]
78pub(super) async fn process(command: AdminCommand, context: &Context<'_>) -> Result {
79	use AdminCommand::*;
80
81	match command {
82		| Appservices(command) => appservice::process(command, context).await,
83		| Media(command) => media::process(command, context).await,
84		| Users(command) => user::process(command, context).await,
85		| Rooms(command) => room::process(command, context).await,
86		| Federation(command) => federation::process(command, context).await,
87		| Server(command) => server::process(command, context).await,
88		| Debug(command) => debug::process(command, context).await,
89		| Query(command) => query::process(command, context).await,
90		| Token(command) => token::process(command, context).await,
91	}
92}