use anyhow::Result;
use clap::{Parser, Subcommand};
mod commands;
mod config;
#[derive(Parser)]
#[command(name = "idprova", version, about, long_about = None)]
struct Cli {
#[command(subcommand)]
command: Commands,
}
#[derive(Subcommand)]
enum Commands {
Keygen {
#[arg(short, long, default_value = "~/.idprova/keys/agent.key")]
output: String,
},
Aid {
#[command(subcommand)]
action: AidCommands,
},
Dat {
#[command(subcommand)]
action: DatCommands,
},
Receipt {
#[command(subcommand)]
action: ReceiptCommands,
},
}
#[derive(Subcommand)]
enum AidCommands {
Create {
#[arg(long)]
id: String,
#[arg(long)]
name: String,
#[arg(long)]
controller: String,
#[arg(long)]
model: Option<String>,
#[arg(long)]
runtime: Option<String>,
#[arg(long)]
key: String,
},
Resolve {
id: String,
#[arg(long)]
registry: Option<String>,
},
Verify {
file: String,
},
}
#[derive(Subcommand)]
enum DatCommands {
Issue {
#[arg(long)]
issuer: String,
#[arg(long)]
subject: String,
#[arg(long)]
scope: String,
#[arg(long, default_value = "24h")]
expires_in: String,
#[arg(long)]
key: String,
},
Verify {
token: String,
#[arg(long)]
registry: Option<String>,
#[arg(long)]
key: Option<String>,
#[arg(long, default_value = "")]
scope: String,
},
Inspect {
token: String,
},
}
#[derive(Subcommand)]
enum ReceiptCommands {
Verify {
file: String,
},
Stats {
file: String,
},
}
fn main() -> Result<()> {
tracing_subscriber::fmt()
.with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
.init();
let cfg = config::Config::load().unwrap_or_default();
let cli = Cli::parse();
match cli.command {
Commands::Keygen { output } => {
commands::keygen::run(&output)?;
}
Commands::Aid { action } => match action {
AidCommands::Create {
id,
name,
controller,
model,
runtime,
key,
} => {
commands::aid::create(
&id,
&name,
&controller,
model.as_deref(),
runtime.as_deref(),
&key,
)?;
}
AidCommands::Resolve { id, registry } => {
let reg = registry.unwrap_or_else(|| cfg.registry_url.clone());
commands::aid::resolve(&id, ®)?;
}
AidCommands::Verify { file } => {
commands::aid::verify(&file)?;
}
},
Commands::Dat { action } => match action {
DatCommands::Issue {
issuer,
subject,
scope,
expires_in,
key,
} => {
commands::dat::issue(&issuer, &subject, &scope, &expires_in, &key)?;
}
DatCommands::Verify {
token,
registry,
key,
scope,
} => {
let reg = registry.unwrap_or_else(|| cfg.registry_url.clone());
commands::dat::verify(&token, ®, key.as_deref(), &scope)?;
}
DatCommands::Inspect { token } => {
commands::dat::inspect(&token)?;
}
},
Commands::Receipt { action } => match action {
ReceiptCommands::Verify { file } => {
commands::receipt::verify(&file)?;
}
ReceiptCommands::Stats { file } => {
commands::receipt::stats(&file)?;
}
},
}
Ok(())
}