OXYGENHUB

Coverage / src/cli/open.rs 34.4%

22 of 64 lines run, 4 of 18 functions · run #24 on main · run missed

1
//! `spectra open`: sign in to Radar (spec §8.3). The owner's CLI signs a
2
//! request for a one-time link with the owner's key; the link, valid for a
3
//! minute, opens a level-2 browser session.
4
5
use super::identity::{owner_home, recorded_owner};
6
use super::{ui, Ctx};
7
use anyhow::{bail, Context, Result};
8
use spectra_core::ledger::Ledger;
9
use spectra_mcp::client::Client;
10
use std::path::PathBuf;
11
121×
pub async fn open(ctx: &Ctx, owner_home_flag: Option<PathBuf>, launch: bool) -> Result<i32> {
131×
    let socket = ctx.socket();
141×
    if !Client::alive(&socket).await {
150×
        bail!("Spectra is not serving {} (spectra serve)", ctx.data.display());
161×
    }
171×
    let home = owner_home(ctx, owner_home_flag);
181×
    let ledger = Ledger::open_read_only(&ctx.db()?).await?;
191×
    let owner = recorded_owner(&ledger, &home).await?;
201×
    let r = Client::new(&socket, Some(owner)).send("POST", "/radar/link", &[], Vec::new()).await?;
211×
    if r.status != 200 {
220×
        bail!("the daemon refused the sign-in: {}", String::from_utf8_lossy(&r.body));
231×
    }
241×
    let url = r.json()?.get("url").and_then(|u| u.as_str()).map(String::from).context("no link in the answer")?;
251×
    println!("{} {}", ui::done("Signed"), ui::subject("a one-time link to Radar, good for a minute"));
261×
    show(&url, launch);
271×
    Ok(0)
281×
}
29
301×
fn show(url: &str, launch: bool) {
311×
    println!("  {}", ui::id(url));
321×
    if launch
330×
        && std::process::Command::new("xdg-open")
340×
            .arg(url)
350×
            .stdout(std::process::Stdio::null())
360×
            .stderr(std::process::Stdio::null())
370×
            .spawn()
380×
            .is_ok()
390×
    {
400×
        println!("  {} {}", ui::note(), ui::dim("opened in your browser"));
411×
    }
421×
}
43
44
/// `spectra open` with Spectra on another machine (spec §8.6): serve Radar
45
/// through a loopback proxy to the pinned server, and sign in through it.
460×
pub async fn open_remote(ctx: &Ctx, owner_home_flag: Option<PathBuf>, launch: bool, port: u16) -> Result<i32> {
470×
    let cfg = ctx.remote()?.context("no server configured")?;
480×
    let home = owner_home(ctx, owner_home_flag);
490×
    let listener = tokio::net::TcpListener::bind(("127.0.0.1", port))
500×
        .await
510×
        .with_context(|| format!("binding 127.0.0.1:{port} for Radar (another `spectra open` running? --port picks another)"))?;
520×
    let port = listener.local_addr()?.port();
530×
    let client = ctx.owner_client(&home).await?.context("no server configured")?;
540×
    let r = client.send("POST", "/radar/link", &[], Vec::new()).await?;
550×
    if r.status != 200 {
560×
        bail!("the server refused the sign-in: {}", String::from_utf8_lossy(&r.body));
570×
    }
580×
    let url = r.json()?.get("url").and_then(|u| u.as_str()).map(String::from).context("no link in the answer")?;
590×
    let local = spectra_mcp::proxy::local_link(&url, port);
600×
    println!("{} {}{}{}", ui::done("Serving"), ui::subject("Radar"), ui::sep(), ui::dim(format!("from {} over pinned TLS", cfg.server)));
610×
    println!("{}", ui::kv("radar", format!("http://127.0.0.1:{port}")));
620×
    println!("{} {}", ui::done("Signed"), ui::subject("a one-time link, good for a minute"));
630×
    show(&local, launch);
640×
    println!("  {} {}", ui::note(), ui::dim("Ctrl-C stops the proxy"));
65
    use std::io::Write;
660×
    std::io::stdout().flush().ok();
670×
    let app = spectra_mcp::proxy::router(cfg.endpoint()?, port);
680×
    axum::serve(listener, app)
690×
        .with_graceful_shutdown(async {
700×
            let _ = tokio::signal::ctrl_c().await;
710×
        })
720×
        .await?;
730×
    Ok(0)
740×
}