Skip to main content

atd_cli/
connect.rs

1//! Endpoint resolution + AtdClient connect helper.
2
3use atd_protocol::AtdError;
4use atd_sdk::{AtdClient, Endpoint};
5use std::path::PathBuf;
6
7/// Resolve the endpoint from an optional explicit `--sock PATH` override.
8/// Falls back to `Endpoint::default_anos()` which reads `$HOME/.anos/anos.sock`.
9pub fn resolve_endpoint(sock: Option<PathBuf>) -> Endpoint {
10    match sock {
11        Some(p) => Endpoint::unix(p),
12        None => Endpoint::default_anos(),
13    }
14}
15
16/// Connect to the configured ATD server. On failure returns an `AtdError`
17/// suitable for the top-level error formatter; the caller owns printing.
18pub async fn connect(sock: Option<PathBuf>) -> Result<AtdClient, AtdError> {
19    let endpoint = resolve_endpoint(sock);
20    AtdClient::connect(endpoint).await
21}
22
23#[cfg(test)]
24mod tests {
25    use super::*;
26
27    #[test]
28    fn resolve_endpoint_uses_override_when_provided() {
29        let e = resolve_endpoint(Some(PathBuf::from("/tmp/custom.sock")));
30        match e {
31            Endpoint::UnixSocket(p) => assert_eq!(p, PathBuf::from("/tmp/custom.sock")),
32        }
33    }
34
35    #[test]
36    fn resolve_endpoint_falls_back_to_default_anos() {
37        let e = resolve_endpoint(None);
38        match e {
39            Endpoint::UnixSocket(p) => assert!(
40                p.to_string_lossy().ends_with(".anos/anos.sock"),
41                "default should point at ~/.anos/anos.sock, got {p:?}"
42            ),
43        }
44    }
45}