use anyhow::Result; use serde::{Deserialize, Serialize}; use std::{fs, path::Path}; use url::Url; #[derive(Debug, Clone, Serialize, Deserialize)] pub struct AgentConfig { pub server_url: String, pub agent_id: Option, pub token: Option, pub secret: Option, pub name: Option, pub heartbeat_interval: Option, #[serde(skip)] pub config_path: Option, } impl Default for AgentConfig { fn default() -> Self { Self { server_url: "http://127.0.0.1:8080".to_string(), agent_id: None, token: None, secret: None, name: None, heartbeat_interval: Some(30), config_path: None, } } } impl AgentConfig { pub fn load_optional(path: &str) -> Result { if Path::new(path).exists() { Ok(toml::from_str(&fs::read_to_string(path)?)?) } else { Ok(Self::default()) } } pub fn save(&self, path: &str) -> Result<()> { if let Some(parent) = Path::new(path).parent() { fs::create_dir_all(parent)?; } fs::write(path, toml::to_string_pretty(self)?)?; Ok(()) } pub fn ws_url(&self) -> Result { let mut url = Url::parse(&self.server_url)?; url.set_scheme(match url.scheme() { "https" => "wss", _ => "ws", }) .ok(); url.set_path("/api/agent/ws"); Ok(url.to_string()) } }