1
/// Command-line interface for managing the root certificate.
2
pub mod certificate;
3
/// Command-line interface for hosting a server.
4
pub mod serve;
5

            
6
use bonsaidb_local::cli::StorageCommand;
7
use clap::Parser;
8

            
9
use crate::{Backend, BackendError, CustomServer, NoBackend, ServerConfiguration};
10

            
11
/// Available commands for `bonsaidb server`.
12
28
#[derive(Parser, Debug)]
13
pub 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

            
26
impl<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
4
    pub async fn execute_on(self, server: CustomServer<B>) -> Result<(), BackendError<B::Error>> {
38
4
        match self {
39
10
            Self::Certificate(command) => command.execute(&server).await,
40
2
            Self::Serve(command) => command.execute(&server).await,
41
2
            Self::Storage(command) => command
42
2
                .execute_on_async(&server)
43
2
                .await
44
2
                .map_err(BackendError::from),
45
        }
46
3
    }
47
}