@@ -2,7 +2,13 @@ |
| 2 | 2 | //! |
| 3 | 3 | //! Separate CLI for querying daemon status and managing configuration. |
| 4 | 4 | |
| 5 | +use std::path::PathBuf; |
| 6 | + |
| 5 | 7 | use clap::{Parser, Subcommand}; |
| 8 | +use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader}; |
| 9 | +use tokio::net::UnixStream; |
| 10 | + |
| 11 | +use hyprkvm_common::protocol::{IpcRequest, IpcResponse}; |
| 6 | 12 | |
| 7 | 13 | #[derive(Parser)] |
| 8 | 14 | #[command(name = "hyprkvm-ctl")] |
@@ -23,35 +29,284 @@ enum Commands { |
| 23 | 29 | }, |
| 24 | 30 | |
| 25 | 31 | /// List connected peers |
| 26 | | - Peers, |
| 32 | + Peers { |
| 33 | + /// Output as JSON |
| 34 | + #[arg(long)] |
| 35 | + json: bool, |
| 36 | + }, |
| 27 | 37 | |
| 28 | 38 | /// Ping a peer |
| 29 | 39 | Ping { |
| 30 | | - /// Peer name or direction |
| 40 | + /// Peer name to ping |
| 31 | 41 | peer: String, |
| 32 | 42 | }, |
| 33 | 43 | } |
| 34 | 44 | |
| 35 | | -#[tokio::main] |
| 36 | | -async fn main() -> anyhow::Result<()> { |
| 37 | | - let cli = Cli::parse(); |
| 45 | +// ============================================================================ |
| 46 | +// IPC Client |
| 47 | +// ============================================================================ |
| 38 | 48 | |
| 39 | | - match cli.command { |
| 40 | | - Commands::Status { json } => { |
| 41 | | - // TODO: Connect to daemon and get status |
| 42 | | - if json { |
| 43 | | - println!("{{\"status\": \"not_implemented\"}}"); |
| 49 | +/// Get the IPC socket path |
| 50 | +fn socket_path() -> PathBuf { |
| 51 | + let runtime_dir = |
| 52 | + std::env::var("XDG_RUNTIME_DIR").unwrap_or_else(|_| "/tmp".to_string()); |
| 53 | + PathBuf::from(runtime_dir).join("hyprkvm.sock") |
| 54 | +} |
| 55 | + |
| 56 | +/// IPC client for sending commands to daemon |
| 57 | +struct IpcClient { |
| 58 | + stream: UnixStream, |
| 59 | +} |
| 60 | + |
| 61 | +impl IpcClient { |
| 62 | + /// Connect to the daemon |
| 63 | + async fn connect() -> std::io::Result<Self> { |
| 64 | + let path = socket_path(); |
| 65 | + let stream = UnixStream::connect(&path).await?; |
| 66 | + Ok(Self { stream }) |
| 67 | + } |
| 68 | + |
| 69 | + /// Send a request and get response |
| 70 | + async fn request(&mut self, req: &IpcRequest) -> std::io::Result<IpcResponse> { |
| 71 | + // Send request |
| 72 | + let json = serde_json::to_string(req)?; |
| 73 | + self.stream.write_all(json.as_bytes()).await?; |
| 74 | + self.stream.write_all(b"\n").await?; |
| 75 | + self.stream.flush().await?; |
| 76 | + |
| 77 | + // Read response |
| 78 | + let mut reader = BufReader::new(&mut self.stream); |
| 79 | + let mut line = String::new(); |
| 80 | + reader.read_line(&mut line).await?; |
| 81 | + |
| 82 | + serde_json::from_str(&line) |
| 83 | + .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e)) |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +// ============================================================================ |
| 88 | +// Helpers |
| 89 | +// ============================================================================ |
| 90 | + |
| 91 | +/// Format uptime in human-readable form |
| 92 | +fn format_uptime(secs: u64) -> String { |
| 93 | + let days = secs / 86400; |
| 94 | + let hours = (secs % 86400) / 3600; |
| 95 | + let mins = (secs % 3600) / 60; |
| 96 | + let secs = secs % 60; |
| 97 | + |
| 98 | + if days > 0 { |
| 99 | + format!("{}d {}h {}m {}s", days, hours, mins, secs) |
| 100 | + } else if hours > 0 { |
| 101 | + format!("{}h {}m {}s", hours, mins, secs) |
| 102 | + } else if mins > 0 { |
| 103 | + format!("{}m {}s", mins, secs) |
| 104 | + } else { |
| 105 | + format!("{}s", secs) |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +/// Get colored status indicator |
| 110 | +fn status_indicator(status: &str) -> &'static str { |
| 111 | + match status { |
| 112 | + "connected" => "\x1b[32m●\x1b[0m", // Green dot |
| 113 | + "connecting" => "\x1b[33m●\x1b[0m", // Yellow dot |
| 114 | + "disconnected" => "\x1b[31m●\x1b[0m", // Red dot |
| 115 | + _ => "○", // Empty dot |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +// ============================================================================ |
| 120 | +// Command Handlers |
| 121 | +// ============================================================================ |
| 122 | + |
| 123 | +async fn handle_status(json_output: bool) -> anyhow::Result<()> { |
| 124 | + let mut client = match IpcClient::connect().await { |
| 125 | + Ok(c) => c, |
| 126 | + Err(e) => { |
| 127 | + if json_output { |
| 128 | + println!( |
| 129 | + "{}", |
| 130 | + serde_json::json!({ |
| 131 | + "error": "daemon not running", |
| 132 | + "details": e.to_string() |
| 133 | + }) |
| 134 | + ); |
| 44 | 135 | } else { |
| 45 | | - println!("HyprKVM Status: not implemented yet"); |
| 136 | + eprintln!("Error: daemon not running ({})", e); |
| 46 | 137 | } |
| 138 | + std::process::exit(1); |
| 47 | 139 | } |
| 48 | | - Commands::Peers => { |
| 49 | | - println!("Peer listing not implemented yet"); |
| 140 | + }; |
| 141 | + |
| 142 | + let response = client.request(&IpcRequest::Status).await?; |
| 143 | + |
| 144 | + match response { |
| 145 | + IpcResponse::Status { |
| 146 | + state, |
| 147 | + connected_peers, |
| 148 | + uptime_secs, |
| 149 | + machine_name, |
| 150 | + } => { |
| 151 | + if json_output { |
| 152 | + println!( |
| 153 | + "{}", |
| 154 | + serde_json::json!({ |
| 155 | + "machine_name": machine_name, |
| 156 | + "state": state, |
| 157 | + "connected_peers": connected_peers, |
| 158 | + "uptime_secs": uptime_secs, |
| 159 | + }) |
| 160 | + ); |
| 161 | + } else { |
| 162 | + println!("HyprKVM Status"); |
| 163 | + println!("──────────────────────────────"); |
| 164 | + println!("Machine: {}", machine_name); |
| 165 | + println!("State: {}", state); |
| 166 | + println!("Uptime: {}", format_uptime(uptime_secs)); |
| 167 | + println!("Peers: {} connected", connected_peers.len()); |
| 168 | + if !connected_peers.is_empty() { |
| 169 | + println!(" {}", connected_peers.join(", ")); |
| 170 | + } |
| 171 | + } |
| 172 | + } |
| 173 | + IpcResponse::Error { message } => { |
| 174 | + if json_output { |
| 175 | + println!("{}", serde_json::json!({ "error": message })); |
| 176 | + } else { |
| 177 | + eprintln!("Error: {}", message); |
| 178 | + } |
| 179 | + std::process::exit(1); |
| 50 | 180 | } |
| 51 | | - Commands::Ping { peer } => { |
| 52 | | - println!("Ping {} not implemented yet", peer); |
| 181 | + _ => { |
| 182 | + eprintln!("Unexpected response from daemon"); |
| 183 | + std::process::exit(1); |
| 53 | 184 | } |
| 54 | 185 | } |
| 55 | 186 | |
| 56 | 187 | Ok(()) |
| 57 | 188 | } |
| 189 | + |
| 190 | +async fn handle_peers(json_output: bool) -> anyhow::Result<()> { |
| 191 | + let mut client = match IpcClient::connect().await { |
| 192 | + Ok(c) => c, |
| 193 | + Err(e) => { |
| 194 | + if json_output { |
| 195 | + println!( |
| 196 | + "{}", |
| 197 | + serde_json::json!({ |
| 198 | + "error": "daemon not running", |
| 199 | + "details": e.to_string() |
| 200 | + }) |
| 201 | + ); |
| 202 | + } else { |
| 203 | + eprintln!("Error: daemon not running ({})", e); |
| 204 | + } |
| 205 | + std::process::exit(1); |
| 206 | + } |
| 207 | + }; |
| 208 | + |
| 209 | + let response = client.request(&IpcRequest::ListPeers).await?; |
| 210 | + |
| 211 | + match response { |
| 212 | + IpcResponse::Peers { peers } => { |
| 213 | + if json_output { |
| 214 | + println!("{}", serde_json::to_string_pretty(&peers)?); |
| 215 | + } else { |
| 216 | + if peers.is_empty() { |
| 217 | + println!("No peers configured"); |
| 218 | + } else { |
| 219 | + println!("Configured Peers"); |
| 220 | + println!("──────────────────────────────────────────────────"); |
| 221 | + for peer in &peers { |
| 222 | + let indicator = status_indicator(&peer.status); |
| 223 | + println!( |
| 224 | + "{} {} ({:?}) - {}", |
| 225 | + indicator, peer.name, peer.direction, peer.address |
| 226 | + ); |
| 227 | + } |
| 228 | + println!("──────────────────────────────────────────────────"); |
| 229 | + let connected = peers.iter().filter(|p| p.connected).count(); |
| 230 | + println!("{}/{} peers connected", connected, peers.len()); |
| 231 | + } |
| 232 | + } |
| 233 | + } |
| 234 | + IpcResponse::Error { message } => { |
| 235 | + if json_output { |
| 236 | + println!("{}", serde_json::json!({ "error": message })); |
| 237 | + } else { |
| 238 | + eprintln!("Error: {}", message); |
| 239 | + } |
| 240 | + std::process::exit(1); |
| 241 | + } |
| 242 | + _ => { |
| 243 | + eprintln!("Unexpected response from daemon"); |
| 244 | + std::process::exit(1); |
| 245 | + } |
| 246 | + } |
| 247 | + |
| 248 | + Ok(()) |
| 249 | +} |
| 250 | + |
| 251 | +async fn handle_ping(peer_name: String) -> anyhow::Result<()> { |
| 252 | + let mut client = match IpcClient::connect().await { |
| 253 | + Ok(c) => c, |
| 254 | + Err(e) => { |
| 255 | + eprintln!("Error: daemon not running ({})", e); |
| 256 | + std::process::exit(1); |
| 257 | + } |
| 258 | + }; |
| 259 | + |
| 260 | + println!("Pinging {}...", peer_name); |
| 261 | + |
| 262 | + let response = client |
| 263 | + .request(&IpcRequest::PingPeer { |
| 264 | + peer_name: peer_name.clone(), |
| 265 | + }) |
| 266 | + .await?; |
| 267 | + |
| 268 | + match response { |
| 269 | + IpcResponse::PingResult { |
| 270 | + peer_name, |
| 271 | + latency_ms, |
| 272 | + error, |
| 273 | + } => { |
| 274 | + if let Some(err) = error { |
| 275 | + eprintln!("Ping failed: {}", err); |
| 276 | + std::process::exit(1); |
| 277 | + } else if let Some(ms) = latency_ms { |
| 278 | + println!("Reply from {}: time={}ms", peer_name, ms); |
| 279 | + } else { |
| 280 | + eprintln!("Ping failed: no response"); |
| 281 | + std::process::exit(1); |
| 282 | + } |
| 283 | + } |
| 284 | + IpcResponse::Error { message } => { |
| 285 | + eprintln!("Error: {}", message); |
| 286 | + std::process::exit(1); |
| 287 | + } |
| 288 | + _ => { |
| 289 | + eprintln!("Unexpected response from daemon"); |
| 290 | + std::process::exit(1); |
| 291 | + } |
| 292 | + } |
| 293 | + |
| 294 | + Ok(()) |
| 295 | +} |
| 296 | + |
| 297 | +// ============================================================================ |
| 298 | +// Main |
| 299 | +// ============================================================================ |
| 300 | + |
| 301 | +#[tokio::main] |
| 302 | +async fn main() -> anyhow::Result<()> { |
| 303 | + let cli = Cli::parse(); |
| 304 | + |
| 305 | + match cli.command { |
| 306 | + Commands::Status { json } => handle_status(json).await?, |
| 307 | + Commands::Peers { json } => handle_peers(json).await?, |
| 308 | + Commands::Ping { peer } => handle_ping(peer).await?, |
| 309 | + } |
| 310 | + |
| 311 | + Ok(()) |
| 312 | +} |