bonsaidb_server/
cli.rs

1/// Command-line interface for managing the root certificate.
2pub mod certificate;
3/// Command-line interface for hosting a server.
4pub mod serve;
5
6use bonsaidb_local::cli::StorageCommand;
7use clap::Parser;
8
9use crate::{Backend, BackendError, CustomServer, NoBackend, ServerConfiguration};
10
11/// Available commands for `bonsaidb server`.
12#[derive(Parser, Debug)]
13pub enum Command<B: Backend = NoBackend> {
14    /// Manage the server's root certificate.
15    #[clap(subcommand)]
16    Certificate(certificate::Command),
17
18    /// Execute the server.
19    Serve(serve::Serve<B>),
20
21    /// Manage the server's storage.
22    #[clap(flatten)]
23    Storage(StorageCommand),
24}
25
26impl<B: Backend> Command<B> {
27    /// Executes the command.
28    pub async fn execute(
29        self,
30        configuration: ServerConfiguration<B>,
31    ) -> Result<(), BackendError<B::Error>> {
32        let server = CustomServer::<B>::open(configuration).await?;
33        self.execute_on(server).await
34    }
35
36    /// Executes the command on `server`.
37    pub async fn execute_on(self, server: CustomServer<B>) -> Result<(), BackendError<B::Error>> {
38        match self {
39            Self::Certificate(command) => command.execute(&server).await,
40            Self::Serve(command) => command.execute(&server).await,
41            Self::Storage(command) => command
42                .execute_on_async(&server)
43                .await
44                .map_err(BackendError::from),
45        }
46    }
47}