podman_rest_client/
cli.rs

1use std::process::Stdio;
2
3use serde::Deserialize;
4use tokio::process::Command;
5
6#[derive(thiserror::Error, Debug)]
7pub enum CliError {
8    #[error("JSON error: {0}")]
9    Json(#[from] serde_json::Error),
10    #[error("IO error: {0}")]
11    Io(#[from] std::io::Error),
12}
13
14#[derive(Deserialize, Debug)]
15pub struct PodmanConnection {
16    #[serde(rename = "Name")]
17    pub name: String,
18    #[serde(rename = "URI")]
19    pub uri: String,
20    #[serde(rename = "Identity")]
21    pub identity: Option<String>,
22    #[serde(rename = "Default")]
23    pub default: bool,
24    #[serde(rename = "IsMachine")]
25    pub is_machine: bool,
26    #[serde(rename = "ReadWrite")]
27    pub read_write: bool,
28}
29
30pub async fn get_system_connections() -> Result<Vec<PodmanConnection>, CliError> {
31    let cmd = Command::new("podman")
32        .args(["system", "connection", "list", "--format", "json"])
33        .stdout(Stdio::piped())
34        .spawn()?;
35    let output = cmd.wait_with_output().await?;
36    let string = &String::from_utf8_lossy(&output.stdout);
37
38    Ok(serde_json::from_str(string)?)
39}
40
41pub async fn get_default_system_connection() -> Result<Option<PodmanConnection>, CliError> {
42    let connections = get_system_connections().await?;
43    Ok(connections
44        .into_iter()
45        .find(|connection| connection.default))
46}