sesame/
launch.rs

1use comfy_table::{Table, presets::UTF8_FULL};
2use core_types::{EventKind, SecurityLevel, TrustProfileName};
3use owo_colors::OwoColorize;
4
5use crate::ipc::{connect, rpc};
6
7pub(crate) async fn cmd_launch_search(
8    query: &str,
9    max_results: u32,
10    profile: Option<&str>,
11) -> anyhow::Result<()> {
12    let client = connect().await?;
13    let profile = profile
14        .map(|s| TrustProfileName::try_from(s).map_err(|e| anyhow::anyhow!("{e}")))
15        .transpose()?;
16
17    let event = EventKind::LaunchQuery {
18        query: query.to_owned(),
19        max_results,
20        profile,
21    };
22
23    match rpc(&client, event, SecurityLevel::Internal).await? {
24        EventKind::LaunchQueryResponse { results } => {
25            if results.is_empty() {
26                println!("{}", "No results.".dimmed());
27            } else {
28                let mut table = Table::new();
29                table.load_preset(UTF8_FULL);
30                table.set_header(vec!["Name", "ID", "Score"]);
31
32                for r in &results {
33                    table.add_row(vec![&r.name, &r.entry_id, &format!("{:.2}", r.score)]);
34                }
35
36                println!("{table}");
37            }
38        }
39        other => anyhow::bail!("unexpected response: {other:?}"),
40    }
41
42    Ok(())
43}
44
45pub(crate) async fn cmd_launch_run(entry_id: &str, profile: Option<&str>) -> anyhow::Result<()> {
46    let client = connect().await?;
47    let profile = profile
48        .map(|s| TrustProfileName::try_from(s).map_err(|e| anyhow::anyhow!("{e}")))
49        .transpose()?;
50
51    let event = EventKind::LaunchExecute {
52        entry_id: entry_id.to_owned(),
53        profile,
54        tags: Vec::new(),
55        launch_args: Vec::new(),
56    };
57
58    match rpc(&client, event, SecurityLevel::Internal).await? {
59        EventKind::LaunchExecuteResponse { pid, error, .. } => {
60            if pid == 0 {
61                let detail = error.as_deref().unwrap_or("unknown error");
62                anyhow::bail!("launch failed: {detail}");
63            }
64            println!("Launched {} (PID {})", entry_id.green(), pid);
65        }
66        other => anyhow::bail!("unexpected response: {other:?}"),
67    }
68
69    Ok(())
70}