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
/// Command-line interface for managing the root certificate.
pub mod certificate;
/// Command-line interface for hosting a server.
pub mod serve;

use std::path::{Path, PathBuf};

use structopt::StructOpt;

use crate::{Configuration, Server};

/// Command-line interface for `pliantdb server`.
#[derive(StructOpt, Debug)]
pub struct Cli {
    /// The path to the directory where the server should store its data.
    pub server_data_directory: PathBuf,

    /// The command to execute.
    #[structopt(subcommand)]
    pub subcommand: Command,
}

/// Available commands for `pliantdb server`.
#[derive(StructOpt, Debug)]
pub enum Command {
    /// Manage the server's root certificate.
    Certificate(certificate::Command),

    /// Execute the server.
    #[structopt(flatten)]
    Serve(serve::Serve),
}

impl Command {
    /// Executes the command.
    pub async fn execute<F: Fn(&Server) + Send>(
        &self,
        database_path: &Path,
        schema_registrar: F,
    ) -> anyhow::Result<()> {
        let server = Server::open(database_path, Configuration::default()).await?;
        schema_registrar(&server);
        match self {
            Self::Certificate(command) => command.execute(server).await,
            Self::Serve(command) => command.execute(server).await,
        }
    }
}