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