//! CLI view over the unified Maestro config at `~/.config/maestro/config.toml`. //! //! Reads `[shared].registry_url` (registry HTTP base) and `[cli].cookie` //! (persisted `maestro_session` JWT). All other sections in the file are //! preserved on write. use std::net::SocketAddr; use std::path::{Path, PathBuf}; use maestro_config::MaestroConfigFile; pub use maestro_config::CONFIG_PATH_ENV; /// Last-resort home-daemon HTTP base when neither config nor a local /// `[daemon].bind_addr` tells us otherwise. Matches the daemon's default /// `bind_addr` (`crates/maestro-daemon` defaults). pub const DEFAULT_HOME_DAEMON_URL: &str = "http://127.0.0.1:7070"; /// `~/.config/maestro/config.toml` (XDG-aware). pub fn default_config_path() -> Result { Ok(maestro_config::default_config_path()?) } /// `$MAESTRO_CONFIG` if set, else [`default_config_path`]. pub fn resolve_config_path() -> Result { Ok(maestro_config::resolve_config_path()?) } #[derive(Debug, thiserror::Error)] pub enum ConfigError { #[error(transparent)] Inner(#[from] maestro_config::ConfigError), #[error( "no registry URL configured — pass --registry-url, set \ MAESTRO_REGISTRY_URL, or run `maestro set-registry `" )] NoRegistryUrl, } /// Resolved CLI config — the subset of the unified file that this binary /// actually consumes. #[derive(Debug, Clone, Default, PartialEq, Eq)] pub struct CliConfig { pub registry_url: Option, pub cookie: Option, /// `[cli].home_daemon_url` — explicit home-daemon HTTP base (NAP-244). pub home_daemon_url: Option, /// `[daemon].bind_addr` if a daemon is configured on this host. Used to /// derive the default home-daemon URL when `home_daemon_url` is unset. pub daemon_bind_addr: Option, } impl CliConfig { /// Load the subset of the unified file the CLI consumes: /// `[shared].registry_url`, `[cli].cookie`, `[cli].home_daemon_url`, and /// `[daemon].bind_addr` (for default home-daemon derivation). Returns an /// empty config if the file is missing — the CLI tolerates a missing file /// so `maestro set-registry ` can bootstrap it. pub fn load(path: &Path) -> Result { let file = MaestroConfigFile::load_or_default(path)?; Ok(Self { registry_url: file.shared.registry_url, cookie: file.cli.cookie, home_daemon_url: file.cli.home_daemon_url, daemon_bind_addr: file.daemon.bind_addr, }) } /// Resolve the home-daemon HTTP base the broker path should target. /// /// Precedence: explicit `override_url` (CLI flag / env) → `[cli] /// .home_daemon_url` → `http://<[daemon].bind_addr>` → the built-in /// [`DEFAULT_HOME_DAEMON_URL`]. Always returns a value; reachability is /// decided later by whether the connection actually succeeds. pub fn resolve_home_daemon_url(&self, override_url: Option) -> String { override_url .or_else(|| self.home_daemon_url.clone()) .or_else(|| self.daemon_bind_addr.map(|a| format!("http://{a}"))) .unwrap_or_else(|| DEFAULT_HOME_DAEMON_URL.to_string()) } /// Persist `[shared].registry_url`. Read-modify-write — preserves /// any other sections already in the file (`[daemon]`, `[registry]`, /// existing `[cli].cookie`). pub fn save_registry_url(path: &Path, url: &str) -> Result<(), ConfigError> { let mut file = MaestroConfigFile::load_or_default(path)?; file.shared.registry_url = Some(url.to_string()); file.save(path)?; Ok(()) } /// Persist `[cli].cookie`. Same read-modify-write semantics as /// [`save_registry_url`]; in particular this does not touch /// `[shared].registry_url`. pub fn save_cookie(path: &Path, cookie: &str) -> Result<(), ConfigError> { let mut file = MaestroConfigFile::load_or_default(path)?; file.cli.cookie = Some(cookie.to_string()); file.save(path)?; Ok(()) } } #[cfg(test)] mod tests { use super::*; use maestro_config::SharedSection; fn write_file(path: &Path, body: &str) { std::fs::write(path, body).unwrap(); } #[test] fn missing_file_returns_empty() { let dir = tempfile::tempdir().unwrap(); let cfg = CliConfig::load(&dir.path().join("config.toml")).unwrap(); assert_eq!(cfg, CliConfig::default()); } #[test] fn load_reads_shared_registry_url_and_cli_cookie() { let dir = tempfile::tempdir().unwrap(); let path = dir.path().join("config.toml"); write_file( &path, r#" [shared] registry_url = "https://r.example" [cli] cookie = "eyJ..." "#, ); let cfg = CliConfig::load(&path).unwrap(); assert_eq!(cfg.registry_url.as_deref(), Some("https://r.example")); assert_eq!(cfg.cookie.as_deref(), Some("eyJ...")); } #[test] fn save_registry_url_preserves_other_sections() { let dir = tempfile::tempdir().unwrap(); let path = dir.path().join("config.toml"); write_file( &path, r#" [shared] token = "abc" [daemon] daemon_url = "ws://laptop:7070" "#, ); CliConfig::save_registry_url(&path, "https://new.example/").unwrap(); let after = MaestroConfigFile::load(&path).unwrap(); assert_eq!( after.shared.registry_url.as_deref(), Some("https://new.example/") ); assert_eq!(after.shared.token.as_deref(), Some("abc")); assert_eq!(after.daemon.daemon_url.as_deref(), Some("ws://laptop:7070")); } #[test] fn save_cookie_does_not_touch_registry_url() { let dir = tempfile::tempdir().unwrap(); let path = dir.path().join("config.toml"); write_file( &path, r#" [shared] registry_url = "https://r.example" "#, ); CliConfig::save_cookie(&path, "eyJ.new").unwrap(); let after = MaestroConfigFile::load(&path).unwrap(); assert_eq!( after.shared.registry_url.as_deref(), Some("https://r.example") ); assert_eq!(after.cli.cookie.as_deref(), Some("eyJ.new")); } #[test] fn resolve_home_daemon_url_precedence() { // override wins over everything let cfg = CliConfig { home_daemon_url: Some("http://from-cli-section:7070".into()), daemon_bind_addr: Some("127.0.0.1:9999".parse().unwrap()), ..Default::default() }; assert_eq!( cfg.resolve_home_daemon_url(Some("http://override:1".into())), "http://override:1" ); // [cli].home_daemon_url next assert_eq!( cfg.resolve_home_daemon_url(None), "http://from-cli-section:7070" ); // derive from [daemon].bind_addr when no explicit url let cfg = CliConfig { daemon_bind_addr: Some("10.0.0.7:7070".parse().unwrap()), ..Default::default() }; assert_eq!(cfg.resolve_home_daemon_url(None), "http://10.0.0.7:7070"); // built-in default when nothing is configured let cfg = CliConfig::default(); assert_eq!(cfg.resolve_home_daemon_url(None), DEFAULT_HOME_DAEMON_URL); } #[test] fn load_reads_home_daemon_url_and_daemon_bind_addr() { let dir = tempfile::tempdir().unwrap(); let path = dir.path().join("config.toml"); write_file( &path, r#" [cli] home_daemon_url = "http://laptop:7070" [daemon] bind_addr = "127.0.0.1:7071" "#, ); let cfg = CliConfig::load(&path).unwrap(); assert_eq!(cfg.home_daemon_url.as_deref(), Some("http://laptop:7070")); assert_eq!( cfg.daemon_bind_addr, Some("127.0.0.1:7071".parse().unwrap()) ); } #[test] fn save_creates_file_if_missing() { let dir = tempfile::tempdir().unwrap(); let path = dir.path().join("nested/config.toml"); CliConfig::save_registry_url(&path, "https://x").unwrap(); assert!(path.exists()); let after = MaestroConfigFile::load(&path).unwrap(); assert_eq!( after, MaestroConfigFile { shared: SharedSection { registry_url: Some("https://x".into()), token: None, }, ..Default::default() } ); } }