use std::path::PathBuf;
use anyhow::Result;
use clap::{Parser, Subcommand};
use tracing::info;
use ilink_hub::bridge::{builtin, resolve_hub_connection, run_bridge, BridgeApp};
use ilink_hub::paths::default_bridge_config_path;
#[derive(Parser)]
#[command(name = "ilink-hub-bridge")]
#[command(
version,
about = "Bridge WeChat (via iLink Hub) to a local coding CLI (Claude Code, Codex, …)"
)]
struct Cli {
#[arg(long, env = "WEIXIN_BASE_URL", default_value = "http://127.0.0.1:8765", global = true)]
hub_url: String,
#[arg(long, env = "WEIXIN_TOKEN", global = true)]
token: Option<String>,
#[arg(long, env = "ILINKHUB_BRIDGE_CREDS", global = true)]
cred_file: Option<String>,
#[arg(long, default_value_t = false, global = true)]
pair: bool,
#[arg(long, env = "ILINKHUB_BRIDGE_REGISTER_NAME", global = true)]
register_name: Option<String>,
#[arg(long, default_value_t = false, global = true)]
force_register: bool,
#[arg(long)]
config: Option<PathBuf>,
#[command(subcommand)]
command: Option<Commands>,
}
#[derive(Subcommand)]
enum Commands {
Profile {
#[arg(value_name = "TYPE")]
profile_type: String,
},
}
fn explicit_token(cli: &Cli) -> Option<&str> {
cli.token
.as_deref()
.map(str::trim)
.filter(|s| !s.is_empty())
}
#[tokio::main]
async fn main() -> Result<()> {
tracing_subscriber::fmt()
.with_env_filter(
tracing_subscriber::EnvFilter::from_default_env()
.add_directive("ilink_hub=info".parse()?),
)
.init();
let cli = Cli::parse();
match &cli.command {
Some(Commands::Profile { profile_type }) => {
builtin::run_builtin_profile(profile_type).await
}
None => {
let (hub_url, token) = resolve_hub_connection(
&cli.hub_url,
explicit_token(&cli),
cli.cred_file.as_deref(),
cli.pair,
cli.register_name.as_deref(),
cli.force_register,
)
.await?;
info!(%hub_url, "using Hub base URL for downstream");
let config_path = cli
.config
.clone()
.unwrap_or_else(default_bridge_config_path);
let app = BridgeApp::load(&config_path)?;
info!(config_path = %config_path.display(), "loaded bridge config");
let handle = tokio::spawn(run_bridge(hub_url, token, app));
let _ = tokio::signal::ctrl_c().await;
handle.abort();
let _ = handle.await;
info!("exit");
Ok(())
}
}
}