use clap::{Parser, Subcommand};
use anyhow::Result;
use colored::Colorize;
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
mod commands;
mod config;
mod utils;
use commands::*;
#[derive(Parser)]
#[command(author, version, about, long_about = None)]
#[command(propagate_version = true)]
struct Cli {
#[arg(short, long, global = true, env = "REVOKE_CONFIG")]
config: Option<String>,
#[arg(short, long, global = true, action = clap::ArgAction::Count)]
verbose: u8,
#[command(subcommand)]
command: Commands,
}
#[derive(Subcommand)]
enum Commands {
Service {
#[command(subcommand)]
command: ServiceCommands,
},
Config {
#[command(subcommand)]
command: ConfigCommands,
},
Gateway {
#[command(subcommand)]
command: GatewayCommands,
},
Trace {
#[command(subcommand)]
command: TraceCommands,
},
Health {
#[command(subcommand)]
command: HealthCommands,
},
Init {
#[arg(default_value = "my-revoke-app")]
name: String,
#[arg(short, long, default_value = "basic")]
template: String,
},
Dev {
#[arg(short, long)]
services: Vec<String>,
#[arg(short, long)]
all: bool,
},
}
#[tokio::main]
async fn main() -> Result<()> {
let cli = Cli::parse();
init_logging(cli.verbose);
let config = config::load_config(cli.config.as_deref())?;
match cli.command {
Commands::Service { command } => {
service::handle_command(command, &config).await?
}
Commands::Config { command } => {
commands::config::handle_command(command, &config).await?
}
Commands::Gateway { command } => {
gateway::handle_command(command, &config).await?
}
Commands::Trace { command } => {
trace::handle_command(command, &config).await?
}
Commands::Health { command } => {
health::handle_command(command, &config).await?
}
Commands::Init { name, template } => {
init::handle_init(&name, &template).await?
}
Commands::Dev { services, all } => {
dev::handle_dev(services, all, &config).await?
}
}
Ok(())
}
fn init_logging(verbosity: u8) {
let filter = match verbosity {
0 => "warn",
1 => "info",
2 => "debug",
_ => "trace",
};
tracing_subscriber::registry()
.with(tracing_subscriber::EnvFilter::try_from_default_env()
.unwrap_or_else(|_| tracing_subscriber::EnvFilter::new(filter)))
.with(tracing_subscriber::fmt::layer()
.with_target(false)
.with_thread_ids(false))
.init();
}