use crate::cli::{print_value, DaemonClient};
use anyhow::Result;
pub async fn add(
client: &DaemonClient,
local_addr: &str,
peer: &str,
target_host: &str,
target_port: u16,
) -> Result<()> {
client.ensure_running().await?;
let body = serde_json::json!({
"local_addr": local_addr,
"peer_agent": peer,
"target_host": target_host,
"target_port": target_port,
});
let resp = client.post("/forwards", &body).await?;
print_value(client.format(), &resp);
Ok(())
}
pub async fn list(client: &DaemonClient) -> Result<()> {
client.ensure_running().await?;
let resp = client.get("/forwards").await?;
print_value(client.format(), &resp);
Ok(())
}
pub async fn remove(client: &DaemonClient, local_addr: &str) -> Result<()> {
client.ensure_running().await?;
let resp = client.delete(&format!("/forwards/{local_addr}")).await?;
print_value(client.format(), &resp);
Ok(())
}
pub async fn streams(client: &DaemonClient) -> Result<()> {
client.ensure_running().await?;
let resp = client.get("/streams").await?;
print_value(client.format(), &resp);
Ok(())
}
#[cfg(test)]
mod tests {
use serde_json::json;
#[test]
fn add_body_shape_is_stable() {
let body = json!({
"local_addr": "127.0.0.1:8022",
"peer_agent": "deadbeef",
"target_host": "127.0.0.1",
"target_port": 22,
});
assert_eq!(body["target_port"], 22);
assert_eq!(body["local_addr"], "127.0.0.1:8022");
}
}