#![allow(missing_docs)]
use std::path::PathBuf;
use clap::{Parser, Subcommand, ValueEnum};
use taskfast_cli::{cmd, config::Config, exit, Envelope, Environment};
#[derive(Debug, Clone, Copy, ValueEnum)]
enum LogFormat {
Text,
Json,
}
impl LogFormat {
fn parse_str(s: &str) -> Option<Self> {
match s {
"json" => Some(Self::Json),
"text" => Some(Self::Text),
_ => None,
}
}
}
#[derive(Debug, Parser)]
#[command(
name = "taskfast",
version,
about = "TaskFast marketplace CLI — worker + poster hot loop."
)]
struct Cli {
#[arg(long, global = true, env = "TASKFAST_API_KEY")]
api_key: Option<String>,
#[arg(long, global = true, env = "TASKFAST_ENV")]
env: Option<Environment>,
#[arg(long, global = true, env = "TASKFAST_API")]
api_base: Option<String>,
#[arg(long, global = true, env = "TASKFAST_CONFIG")]
config: Option<PathBuf>,
#[arg(long, global = true)]
dry_run: bool,
#[arg(long, global = true, value_name = "LEVEL", num_args = 0..=1, default_missing_value = "info")]
verbose: Option<String>,
#[arg(
long,
global = true,
value_name = "FORMAT",
env = "TASKFAST_LOG_FORMAT"
)]
log_format: Option<LogFormat>,
#[arg(long, global = true, conflicts_with = "verbose")]
quiet: bool,
#[arg(long, global = true, env = "TASKFAST_ALLOW_CUSTOM_ENDPOINTS")]
allow_custom_endpoints: bool,
#[command(subcommand)]
command: Command,
}
#[derive(Debug, Subcommand)]
enum Command {
Init(cmd::init::Args),
Me(cmd::me::Args),
Ping(cmd::ping::Args),
#[command(subcommand)]
Task(cmd::task::Command),
#[command(subcommand)]
Bid(cmd::bid::Command),
Post(cmd::post::Args),
Settle(cmd::settle::Args),
Stake(cmd::stake::Args),
#[command(subcommand)]
Backer(cmd::backer::Command),
#[command(subcommand)]
Escrow(cmd::escrow::Command),
#[command(subcommand)]
Events(cmd::events::Command),
#[command(subcommand)]
Webhook(cmd::webhook::Command),
Discover(cmd::discover::Args),
#[command(subcommand)]
Artifact(cmd::artifact::Command),
#[command(subcommand)]
Message(cmd::message::Command),
#[command(subcommand)]
Review(cmd::review::Command),
#[command(subcommand)]
Payment(cmd::payment::Command),
Dispute(cmd::dispute::Args),
#[command(subcommand)]
Agent(cmd::agent::Command),
#[command(subcommand)]
Platform(cmd::platform::Command),
#[command(subcommand)]
Wallet(cmd::wallet::Command),
#[command(subcommand)]
Config(cmd::config::Command),
Skills(cmd::skills::Args),
}
#[tokio::main]
async fn main() -> std::process::ExitCode {
let cli = Cli::parse();
let cfg_path = cli.config.clone().unwrap_or_else(Config::default_path);
let cfg = match Config::load(&cfg_path) {
Ok(c) => c,
Err(e) => {
if !cli.quiet {
let err = cmd::CmdError::Usage(format!("config: {e}"));
let env = cli.env.unwrap_or(cmd::DEFAULT_ENVIRONMENT);
Envelope::error(env, cli.dry_run, &err).emit();
}
return exit::ExitCode::Usage.into();
}
};
let ctx = match cmd::Ctx::from_parts(
cli.api_key,
cli.env,
cli.api_base,
Some(cfg_path),
cli.dry_run,
cli.quiet,
cli.allow_custom_endpoints,
&cfg,
) {
Ok(c) => c,
Err(e) => {
if !cli.quiet {
let env = cli.env.unwrap_or(cmd::DEFAULT_ENVIRONMENT);
Envelope::error(env, cli.dry_run, &e).emit();
}
return exit::ExitCode::Usage.into();
}
};
if let Some(level) = cli.verbose.as_deref() {
let format = cli
.log_format
.or_else(|| ctx.log_format.as_deref().and_then(LogFormat::parse_str))
.unwrap_or(LogFormat::Text);
match format {
LogFormat::Text => {
let _ = tracing_subscriber::fmt()
.with_writer(std::io::stderr)
.with_env_filter(level)
.try_init();
}
LogFormat::Json => {
let _ = tracing_subscriber::fmt()
.with_writer(std::io::stderr)
.with_env_filter(level)
.json()
.try_init();
}
}
}
let result = match cli.command {
Command::Events(cmd::events::Command::Stream(args)) => {
return cmd::events::stream::run(&ctx, args).await;
}
Command::Init(a) => cmd::init::run(&ctx, a).await,
Command::Me(a) => cmd::me::run(&ctx, a).await,
Command::Ping(a) => cmd::ping::run(&ctx, a).await,
Command::Task(c) => cmd::task::run(&ctx, c).await,
Command::Bid(c) => cmd::bid::run(&ctx, c).await,
Command::Post(a) => cmd::post::run(&ctx, a).await,
Command::Settle(a) => cmd::settle::run(&ctx, a).await,
Command::Stake(a) => cmd::stake::run(&ctx, a).await,
Command::Backer(c) => cmd::backer::run(&ctx, c).await,
Command::Escrow(c) => cmd::escrow::run(&ctx, c).await,
Command::Events(c) => cmd::events::run(&ctx, c).await,
Command::Webhook(c) => cmd::webhook::run(&ctx, c).await,
Command::Discover(a) => cmd::discover::run(&ctx, a).await,
Command::Artifact(c) => cmd::artifact::run(&ctx, c).await,
Command::Message(c) => cmd::message::run(&ctx, c).await,
Command::Review(c) => cmd::review::run(&ctx, c).await,
Command::Payment(c) => cmd::payment::run(&ctx, c).await,
Command::Dispute(a) => cmd::dispute::run(&ctx, a).await,
Command::Agent(c) => cmd::agent::run(&ctx, c).await,
Command::Platform(c) => cmd::platform::run(&ctx, c).await,
Command::Wallet(c) => cmd::wallet::run(&ctx, c).await,
Command::Config(c) => cmd::config::run(&ctx, c).await,
Command::Skills(a) => cmd::skills::run(&ctx, a).await,
};
match result {
Ok(env) => {
if !ctx.quiet {
env.with_warnings(ctx.security_warnings()).emit();
}
exit::ExitCode::Success.into()
}
Err(e) => {
if !ctx.quiet {
Envelope::error(ctx.environment, ctx.dry_run, &e)
.with_warnings(ctx.security_warnings())
.emit();
}
e.exit_code().into()
}
}
}