mod cli;
pub use cli::{
    Cli,
    Action,
    exec,
    get,
    invoke_shortcut,
    docker_credential_helper,
};

pub(crate) use platform::connect;
pub use platform::server_addr;

pub mod proto;


#[cfg(unix)]
mod platform {
    use std::path::PathBuf;
    use tokio::net::UnixStream;

    pub async fn connect(addr: Option<PathBuf>) -> Result<UnixStream, std::io::Error> {
        let path = addr.unwrap_or_else(|| server_addr("creddy-server"));
        UnixStream::connect(&path).await
    }

    pub fn server_addr(sock_name: &str) -> PathBuf {
        let mut path = dirs::runtime_dir()
            .unwrap_or_else(|| PathBuf::from("/tmp"));
        path.push(format!("{sock_name}.sock"));
        path
    }
}


#[cfg(windows)]
mod platform {
    pub fn server_addr(sock_name: &str) -> String {
        format!(r"\\.\pipe\{sock_name}")
    }
}
