use clap::{Args, Parser, Subcommand};
use tracing::Instrument;
#[path = "../bundle/backend.rs"]
pub mod backend;
mod context;
mod daemon;
mod log;
mod reviews;
mod server;
mod store;
mod ui;
#[derive(Parser)]
#[command(name = "lgtm", version, about)]
struct Cli {
#[command(subcommand)]
command: Command,
}
#[derive(Subcommand)]
enum Command {
Up(Up),
Down,
Status {
#[arg(long)]
json: bool,
},
Review(reviews::ReviewArgs),
Session(reviews::SessionArgs),
Sessions(reviews::SessionsArgs),
Comments(reviews::CommentsArgs),
#[command(hide = true)]
Serve {
#[arg(long, default_value_t = 8080)]
port: u16,
#[arg(long)]
daemonize: bool,
},
}
#[derive(Args)]
struct Up {
#[arg(long, default_value_t = 8080)]
port: u16,
#[arg(long)]
foreground: bool,
#[arg(long)]
no_open: bool,
}
fn main() -> anyhow::Result<()> {
let cli = Cli::parse();
let store = store::Store::new()?;
if let Command::Serve {
daemonize: true, ..
} = cli.command
{
daemon::detach(&store)?;
}
if matches!(cli.command, Command::Serve { .. })
|| matches!(&cli.command, Command::Up(options) if options.foreground)
{
log::init(&store)?;
}
let command = match &cli.command {
Command::Up(_) => "up",
Command::Down => "down",
Command::Status { .. } => "status",
Command::Review(_) => "review",
Command::Session(_) => "session",
Command::Sessions(_) => "sessions",
Command::Comments(_) => "comments",
Command::Serve { .. } => "serve",
};
tokio::runtime::Runtime::new()?.block_on(
async {
match cli.command {
Command::Up(options) if options.foreground => {
server::serve(options.port, &store, false).await
}
Command::Up(options) => daemon::up(options.port, !options.no_open, &store).await,
Command::Down => daemon::down(&store).await,
Command::Status { json } => daemon::status(&store, json).await,
Command::Review(args) => reviews::review(args, &store).await,
Command::Session(args) => reviews::session(args, &store).await,
Command::Sessions(args) => reviews::sessions(args, &store).await,
Command::Comments(args) => reviews::comments(args, &store).await,
Command::Serve { port, daemonize } => server::serve(port, &store, daemonize).await,
}
}
.instrument(tracing::info_span!("cli_command", command)),
)
}