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