mod config;
mod jwt;
pub mod run;
use clap::{
builder::{styling, Styles}, ColorChoice, Parser, Subcommand
};
#[derive(Parser)]
#[command(color = ColorChoice::Always)]
#[command(
styles = Styles::styled()
.header(styling::AnsiColor::Green.on_default().bold().underline())
.error(styling::AnsiColor::BrightRed.on_default().bold().underline())
.usage(styling::AnsiColor::Cyan.on_default().bold().underline())
.literal(styling::AnsiColor::BrightWhite.on_default().bold())
.placeholder(styling::AnsiColor::White.on_default().dimmed())
// .valid(style)
// .invalid(style)
)]
#[command(version, author, about, long_about = None)]
#[command(disable_help_subcommand = true, subcommand_required = true)]
pub struct Cli {
#[command(subcommand)]
pub subcommand: CliCommand,
#[arg(long = "config-path", short = 'C')]
#[arg(default_value = "crab-vault.toml")]
pub config_path: String,
}
impl Cli {
#[inline(always)]
pub fn action(&self) -> Action {
self.subcommand.action()
}
}
#[derive(Subcommand)]
pub enum CliCommand {
#[command(about = "Run the server.")]
#[command(
long_about = r#"Run the server, but all the attributes passed by cli will override those from config file."#
)]
Run(run::RunArgs),
#[command(subcommand, about = "Set / Unset / Show the configuration item(s).", long_about = None)]
Config(config::Command),
#[command(subcommand, about = "JWT management commands")]
Jwt(jwt::Command),
}
pub enum Action {
Run,
Config,
Jwt,
}
impl CliCommand {
pub const fn action(&self) -> Action {
match self {
CliCommand::Run(_) => Action::Run,
CliCommand::Config(_) => Action::Config,
CliCommand::Jwt(_) => Action::Jwt,
}
}
}
pub async fn run() {
let cli = Cli::parse();
match cli.action() {
Action::Config | Action::Jwt | Action::Run => {
let Cli {
subcommand,
config_path,
} = cli;
exec(subcommand, config_path).await
}
}
}
async fn exec(subcommand: CliCommand, config_path: String) {
match subcommand {
CliCommand::Config(command) => config::exec(config_path, command).await,
CliCommand::Jwt(command) => jwt::exec(command),
CliCommand::Run(_) => crate::http::server::run().await,
}
}