2024-11-23 22:58:43 +01:00
|
|
|
use std::net::SocketAddr;
|
|
|
|
|
|
|
|
use clap::{Parser, Subcommand};
|
|
|
|
|
2024-11-24 00:53:43 +01:00
|
|
|
use crate::{agent, api, state::SharedState};
|
2024-11-23 22:58:43 +01:00
|
|
|
|
|
|
|
pub async fn execute() -> anyhow::Result<()> {
|
|
|
|
let state = SharedState::new().await?;
|
|
|
|
|
|
|
|
let cli = Command::parse();
|
2024-11-24 00:53:43 +01:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
},
|
2024-11-23 22:58:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
},
|
2024-11-24 00:53:43 +01:00
|
|
|
Agent {
|
|
|
|
#[command(subcommand)]
|
|
|
|
commands: AgentCommands,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Subcommand)]
|
|
|
|
enum AgentCommands {
|
|
|
|
Start {
|
|
|
|
#[arg(env = "SERVICE_HOST", long = "service-host")]
|
|
|
|
host: String,
|
|
|
|
},
|
2024-11-23 22:58:43 +01:00
|
|
|
}
|