OXYGENHUB
# d17cbc7069ce spectra / src / cli / open.rs
1.6 KiB · Rust · last changed Radar: Live, channels, threads and the log for the owner, signed in by a CLI-signed link; ledger writes survive a cancelled caller (#10) 2 days ago source blame history raw
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
12
pub async fn open(ctx: &Ctx, owner_home_flag: Option<PathBuf>, launch: bool) -> Result<i32> {
13
    let socket = ctx.socket();
14
    if !Client::alive(&socket).await {
15
        bail!("Spectra is not serving {} (spectra serve)", ctx.data.display());
16
    }
17
    let home = owner_home(ctx, owner_home_flag);
18
    let ledger = Ledger::open_read_only(&ctx.db()?).await?;
19
    let owner = recorded_owner(&ledger, &home).await?;
20
    let r = Client::new(&socket, Some(owner)).send("POST", "/radar/link", &[], Vec::new()).await?;
21
    if r.status != 200 {
22
        bail!("the daemon refused the sign-in: {}", String::from_utf8_lossy(&r.body));
23
    }
24
    let url = r.json()?.get("url").and_then(|u| u.as_str()).map(String::from).context("no link in the answer")?;
25
    println!("{} {}", ui::done("Signed"), ui::subject("a one-time link to Radar, good for a minute"));
26
    println!("  {}", ui::id(&url));
27
    if launch
28
        && std::process::Command::new("xdg-open")
29
            .arg(&url)
30
            .stdout(std::process::Stdio::null())
31
            .stderr(std::process::Stdio::null())
32
            .spawn()
33
            .is_ok()
34
    {
35
        println!("  {} {}", ui::note(), ui::dim("opened in your browser"));
36
    }
37
    Ok(0)
38
}