churn-v2/crates/churn/src/cli.rs

58 lines
1.3 KiB
Rust
Raw Normal View History

use std::net::SocketAddr;
use clap::{Parser, Subcommand};
use crate::{agent, api, state::SharedState};
pub async fn execute() -> anyhow::Result<()> {
let state = SharedState::new().await?;
let cli = Command::parse();
match cli.command.expect("to have a subcommand") {
Commands::Serve { host } => {
tracing::info!("Starting service");
notmad::Mad::builder()
.add(api::Api::new(&state, host))
.run()
.await?;
}
Commands::Agent { commands } => match commands {
AgentCommands::Start { host } => {
tracing::info!("starting agent");
agent::execute(host).await?;
tracing::info!("shut down agent");
}
},
}
Ok(())
}
#[derive(Parser)]
#[command(author, version, about, long_about = None, subcommand_required = true)]
struct Command {
#[command(subcommand)]
command: Option<Commands>,
}
#[derive(Subcommand)]
enum Commands {
Serve {
#[arg(env = "SERVICE_HOST", long, default_value = "127.0.0.1:3000")]
host: SocketAddr,
},
Agent {
#[command(subcommand)]
commands: AgentCommands,
},
}
#[derive(Subcommand)]
enum AgentCommands {
Start {
#[arg(env = "SERVICE_HOST", long = "service-host")]
host: String,
},
}