tawasuyu

tawasuyu/brahman

Rust 459TOML 177Markdown 49JSON 176207 Definiciones

⎇ 53dbdf0f1deea10d6fc567ea2b43e1e75d6d60cd crates/core/ente-bus/examples/busctl.rs Rust · 52 líneas · 1.9 KB Historia Quién tocó qué crudo

chore: monorepo inicial con arje + minga + yahweh absorbidos 53dbdf0f1 · Sergio ·

1//! busctl: cliente CLI para el bus interno del fractal.
2//!
3//! Uso:
4//!   cargo run --example busctl -- list-entes
5//!   cargo run --example busctl -- announce
6//!   cargo run --example busctl -- power-off
7//!
8//! Si `ENTE_BUS_SOCK` no está en el entorno, cae al path dev por defecto.
9
10use ente_bus::{BusClient, BusRequest};
11use std::env;
12
13#[tokio::main(flavor = "current_thread")]
14async fn main() -> anyhow::Result<()> {
15    let args: Vec<String> = env::args().collect();
16    let cmd = args.get(1).map(|s| s.as_str()).unwrap_or("list-entes");
17
18    let mut client = match BusClient::from_env().await {
19        Ok(c) => c,
20        Err(_) => {
21            let user = env::var("USER").unwrap_or_else(|_| "ente".into());
22            let runtime = env::var("XDG_RUNTIME_DIR")
23                .unwrap_or_else(|_| env::var("TMPDIR").unwrap_or_else(|_| "/tmp".into()));
24            let path = format!("{runtime}/ente-bus-{user}.sock");
25            eprintln!("ENTE_BUS_SOCK no definido, intentando {path}");
26            BusClient::connect(&path).await?
27        }
28    };
29
30    let req = match cmd {
31        "list-entes" => BusRequest::ListEntes,
32        "announce" => BusRequest::Announce { capabilities: vec![] },
33        "power-off" => BusRequest::PowerOff { interactive: false },
34        "reboot" => BusRequest::Reboot { interactive: false },
35        "suspend" => BusRequest::Suspend { interactive: false },
36        "invoke-echo" => {
37            let msg = args.get(2).map(|s| s.as_str()).unwrap_or("hola");
38            BusRequest::Invoke {
39                cap: ente_echo::echo_capability(),
40                blob: msg.as_bytes().to_vec(),
41            }
42        }
43        other => {
44            eprintln!("subcomando desconocido: {other}");
45            eprintln!("válidos: list-entes, announce, power-off, reboot, suspend, invoke-echo <text>");
46            std::process::exit(2);
47        }
48    };
49    let resp = client.call(req).await?;
50    println!("{resp:#?}");
51    Ok(())
52}