diff --git a/Cargo.lock b/Cargo.lock index 7c94808..0ad41f8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -947,6 +947,7 @@ dependencies = [ "futures-util", "jsonwebtoken", "libc", + "maestro-config", "maestro-protocol", "maestro-registry", "serde", diff --git a/crates/maestro-daemon/Cargo.toml b/crates/maestro-daemon/Cargo.toml index e7a3623..27833f9 100644 --- a/crates/maestro-daemon/Cargo.toml +++ b/crates/maestro-daemon/Cargo.toml @@ -22,6 +22,7 @@ dirs.workspace = true futures-util.workspace = true jsonwebtoken.workspace = true libc.workspace = true +maestro-config.workspace = true maestro-protocol.workspace = true serde.workspace = true serde_json.workspace = true diff --git a/crates/maestro-daemon/src/config.rs b/crates/maestro-daemon/src/config.rs index cb8df45..193e655 100644 --- a/crates/maestro-daemon/src/config.rs +++ b/crates/maestro-daemon/src/config.rs @@ -1,41 +1,60 @@ -//! Daemon configuration: on-disk TOML, env-var overrides, runtime resolution. +//! Daemon view over the unified Maestro config. //! -//! Precedence per field: **env > file > default**. Required fields -//! (`registry_url`, `daemon_url`, `token`) error if absent in both sources. +//! Reads `[shared]` (registry_url, token) and `[daemon]` (daemon_id, +//! hostname, daemon_url, bind_addr, state_dir, worktree_root) from +//! `~/.config/maestro/config.toml`. //! -//! Default config path: `~/.config/maestrod/config.toml` (override via -//! `$MAESTROD_CONFIG`). +//! Precedence per field: **env > file > default**. The daemon refuses +//! to start if `registry_url`, `daemon_url`, or `token` are absent +//! from both env and file. +//! +//! Default config path: `~/.config/maestro/config.toml` (override via +//! `$MAESTRO_CONFIG`). If the file is missing entirely the daemon +//! errors with a pointer at `maestrod configure` — legacy +//! `~/.config/maestrod/config.toml` is no longer read. use std::net::SocketAddr; use std::path::{Path, PathBuf}; -use serde::{Deserialize, Serialize}; +use maestro_config::{derive_daemon_ws_url, MaestroConfigFile}; + +pub use maestro_config::{ + default_config_path, resolve_config_path, DaemonSection, SharedSection, CONFIG_PATH_ENV, +}; /// Default bind address when neither env nor file specify one. pub const DEFAULT_BIND_ADDR: &str = "127.0.0.1:7070"; +/// Env var that overrides `[shared].registry_url`. +pub const ENV_SHARED_REGISTRY_URL: &str = "MAESTRO_SHARED_REGISTRY_URL"; +/// Env var that overrides `[shared].token`. +pub const ENV_SHARED_TOKEN: &str = "MAESTRO_SHARED_TOKEN"; +/// Env var that overrides `[daemon].daemon_id`. +pub const ENV_DAEMON_ID: &str = "MAESTRO_DAEMON_ID"; +/// Env var that overrides `[daemon].daemon_url`. +pub const ENV_DAEMON_URL: &str = "MAESTRO_DAEMON_URL"; +/// Env var that overrides `[daemon].bind_addr`. +pub const ENV_DAEMON_BIND_ADDR: &str = "MAESTRO_DAEMON_BIND_ADDR"; + fn default_bind_addr() -> SocketAddr { DEFAULT_BIND_ADDR.parse().expect("valid default bind_addr") } #[derive(Debug, thiserror::Error)] pub enum ConfigError { + #[error(transparent)] + Inner(#[from] maestro_config::ConfigError), #[error("io: {0}")] Io(#[from] std::io::Error), - #[error("toml decode: {0}")] - TomlDecode(#[from] toml::de::Error), - #[error("toml encode: {0}")] - TomlEncode(#[from] toml::ser::Error), - #[error("could not determine config directory (XDG_CONFIG_HOME / $HOME unset?)")] - NoConfigDir, #[error("missing required config field `{field}` — set it in {path:?} or via env var `{env}`")] Missing { field: &'static str, env: &'static str, path: PathBuf, }, - #[error("invalid MAESTROD_BIND_ADDR ({input:?}): {source}")] + #[error("invalid {env} ({input:?}): {source}")] InvalidBindAddr { + env: &'static str, input: String, source: std::net::AddrParseError, }, @@ -46,19 +65,13 @@ pub enum ConfigError { pub struct DaemonConfig { pub daemon_id: String, pub hostname: String, + /// Registry WS URL the daemon dials. Derived from `[shared].registry_url` + /// via [`maestro_config::derive_daemon_ws_url`]. pub registry_url: String, pub bind_addr: SocketAddr, - /// Reachable URL for this daemon's per-session WS endpoint, advertised in - /// `Hello` so the registry can hand it to clients via `connect-info`. - /// Distinct from `bind_addr` because the bind may be `0.0.0.0` while the - /// publicly reachable address is a Wireguard / VPN interface. pub daemon_url: String, pub token: String, - /// Root for everything daemon-local: sidecars, FIFOs, etc. - /// Default: `~/.local/state/maestrod`. pub state_dir: PathBuf, - /// Root for ephemeral worktrees the daemon creates on `CreateSession` - /// with a `worktree` spec. Default: `state_dir/worktrees`. pub worktree_root: PathBuf, } @@ -72,124 +85,101 @@ impl DaemonConfig { } } -/// On-disk representation. Every field optional so a minimal config file -/// (just `registry_url` / `daemon_url` / `token`) is enough. -#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)] -pub struct DaemonConfigFile { - #[serde(default, skip_serializing_if = "Option::is_none")] - pub daemon_id: Option, - #[serde(default, skip_serializing_if = "Option::is_none")] - pub hostname: Option, - #[serde(default, skip_serializing_if = "Option::is_none")] - pub registry_url: Option, - #[serde(default, skip_serializing_if = "Option::is_none")] - pub bind_addr: Option, - #[serde(default, skip_serializing_if = "Option::is_none")] - pub daemon_url: Option, - #[serde(default, skip_serializing_if = "Option::is_none")] - pub token: Option, - #[serde(default, skip_serializing_if = "Option::is_none")] - pub state_dir: Option, - #[serde(default, skip_serializing_if = "Option::is_none")] - pub worktree_root: Option, -} - -impl DaemonConfigFile { - pub fn load(path: &Path) -> Result { - let bytes = std::fs::read(path)?; - let s = std::str::from_utf8(&bytes).unwrap_or(""); - Ok(toml::from_str(s)?) - } - - /// Atomic write (tmp + rename). Creates parent dirs on demand. - pub fn save(&self, path: &Path) -> Result<(), ConfigError> { - if let Some(parent) = path.parent() { - std::fs::create_dir_all(parent)?; - } - let body = toml::to_string_pretty(self)?; - let tmp = path.with_extension("toml.tmp"); - std::fs::write(&tmp, body)?; - std::fs::rename(&tmp, path)?; - Ok(()) - } -} - /// Env-var overrides applied by [`load_config`]. Tests pass explicit /// `Option`s; production reads from real env vars in `main`. #[derive(Debug, Clone, Default)] pub struct EnvOverrides { pub daemon_id: Option, - pub registry_url: Option, + pub shared_registry_url: Option, pub bind_addr: Option, pub daemon_url: Option, - pub token: Option, + pub shared_token: Option, } impl EnvOverrides { /// Read every supported env var from the process environment. pub fn from_env() -> Self { Self { - daemon_id: std::env::var("MAESTROD_DAEMON_ID").ok(), - registry_url: std::env::var("MAESTROD_REGISTRY_URL").ok(), - bind_addr: std::env::var("MAESTROD_BIND_ADDR").ok(), - daemon_url: std::env::var("MAESTROD_DAEMON_URL").ok(), - token: std::env::var("MAESTRO_TOKEN").ok(), + daemon_id: std::env::var(ENV_DAEMON_ID).ok(), + shared_registry_url: std::env::var(ENV_SHARED_REGISTRY_URL).ok(), + bind_addr: std::env::var(ENV_DAEMON_BIND_ADDR).ok(), + daemon_url: std::env::var(ENV_DAEMON_URL).ok(), + shared_token: std::env::var(ENV_SHARED_TOKEN).ok(), } } } -/// Resolve the runtime configuration. Treats a missing file as empty, applies -/// env overrides, fills in defaults, and validates the three required fields. +/// Resolve the runtime configuration. Applies env overrides on top of the +/// unified file, fills defaults, validates the three required fields, and +/// derives the daemon's WS endpoint from the shared registry URL. +/// +/// Returns [`maestro_config::ConfigError::MissingFile`] (wrapped) when the +/// file is absent and not all required fields come from env vars — this is +/// the hard-cutover error that points the operator at `maestrod configure`. pub fn load_config(path: &Path, env: EnvOverrides) -> Result { let file = if path.exists() { - DaemonConfigFile::load(path)? + MaestroConfigFile::load(path)? + } else if env_has_all_required(&env) { + // Container / systemd path: every required field comes from env, + // so the on-disk file is genuinely optional. + MaestroConfigFile::default() } else { - DaemonConfigFile::default() + return Err(maestro_config::ConfigError::MissingFile { + path: path.to_path_buf(), + } + .into()); }; - let hostname = file.hostname.unwrap_or_else(system_hostname); + let shared = file.shared; + let daemon = file.daemon; + + let hostname = daemon.hostname.unwrap_or_else(system_hostname); let daemon_id = env .daemon_id - .or(file.daemon_id) + .or(daemon.daemon_id) .unwrap_or_else(|| hostname.clone()); - let registry_url = - env.registry_url - .or(file.registry_url) - .ok_or_else(|| ConfigError::Missing { - field: "registry_url", - env: "MAESTROD_REGISTRY_URL", + let shared_registry_url = + env.shared_registry_url + .or(shared.registry_url) + .ok_or(ConfigError::Missing { + field: "shared.registry_url", + env: ENV_SHARED_REGISTRY_URL, path: path.to_path_buf(), })?; + let registry_url = derive_daemon_ws_url(&shared_registry_url)?; + let daemon_url = env .daemon_url - .or(file.daemon_url) - .ok_or_else(|| ConfigError::Missing { - field: "daemon_url", - env: "MAESTROD_DAEMON_URL", + .or(daemon.daemon_url) + .ok_or(ConfigError::Missing { + field: "daemon.daemon_url", + env: ENV_DAEMON_URL, path: path.to_path_buf(), })?; let token = env - .token - .or(file.token) - .ok_or_else(|| ConfigError::Missing { - field: "token", - env: "MAESTRO_TOKEN", + .shared_token + .or(shared.token) + .ok_or(ConfigError::Missing { + field: "shared.token", + env: ENV_SHARED_TOKEN, path: path.to_path_buf(), })?; let bind_addr = match env.bind_addr { - Some(raw) => raw - .parse() - .map_err(|source| ConfigError::InvalidBindAddr { input: raw, source })?, - None => file.bind_addr.unwrap_or_else(default_bind_addr), + Some(raw) => raw.parse().map_err(|source| ConfigError::InvalidBindAddr { + env: ENV_DAEMON_BIND_ADDR, + input: raw, + source, + })?, + None => daemon.bind_addr.unwrap_or_else(default_bind_addr), }; - let state_dir = match file.state_dir { + let state_dir = match daemon.state_dir { Some(p) => p, None => default_state_dir()?, }; - let worktree_root = file + let worktree_root = daemon .worktree_root .unwrap_or_else(|| state_dir.join("worktrees")); @@ -205,15 +195,14 @@ pub fn load_config(path: &Path, env: EnvOverrides) -> Result Result { - let base = dirs::config_dir().ok_or(ConfigError::NoConfigDir)?; - Ok(base.join("maestrod").join("config.toml")) +fn env_has_all_required(env: &EnvOverrides) -> bool { + env.shared_registry_url.is_some() && env.daemon_url.is_some() && env.shared_token.is_some() } pub fn default_state_dir() -> Result { let base = dirs::state_dir() .or_else(dirs::data_local_dir) - .ok_or(ConfigError::NoConfigDir)?; + .ok_or(maestro_config::ConfigError::NoConfigDir)?; Ok(base.join("maestrod")) } @@ -235,9 +224,12 @@ mod tests { fn minimal_file() -> &'static str { r#" -registry_url = "ws://r/daemon" -daemon_url = "ws://localhost:7070" +[shared] +registry_url = "https://r.example" token = "abc" + +[daemon] +daemon_url = "ws://localhost:7070" "# } @@ -247,7 +239,7 @@ token = "abc" let path = dir.path().join("config.toml"); std::fs::write(&path, minimal_file()).unwrap(); let cfg = load_config(&path, EnvOverrides::default()).unwrap(); - assert_eq!(cfg.registry_url, "ws://r/daemon"); + assert_eq!(cfg.registry_url, "wss://r.example/daemon"); assert_eq!(cfg.daemon_url, "ws://localhost:7070"); assert_eq!(cfg.token, "abc"); assert_eq!(cfg.bind_addr, default_bind_addr()); @@ -258,20 +250,32 @@ token = "abc" } #[test] - fn missing_file_with_env_only_works() { + fn missing_file_with_full_env_works() { + // systemd / container scenario: no config file, everything in env. let dir = tempfile::tempdir().unwrap(); let path = dir.path().join("does-not-exist.toml"); let env = EnvOverrides { - registry_url: Some("ws://r/daemon".into()), + shared_registry_url: Some("http://r.example".into()), daemon_url: Some("ws://x:7070".into()), - token: Some("t".into()), + shared_token: Some("t".into()), ..EnvOverrides::default() }; let cfg = load_config(&path, env).unwrap(); + assert_eq!(cfg.registry_url, "ws://r.example/daemon"); assert_eq!(cfg.token, "t"); assert!(!path.exists(), "load_config must not create the file"); } + #[test] + fn missing_file_without_full_env_errors_with_hard_cutover_message() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("does-not-exist.toml"); + let err = load_config(&path, EnvOverrides::default()).unwrap_err(); + let msg = err.to_string(); + assert!(msg.contains("maestrod configure"), "msg: {msg}"); + assert!(msg.contains("Legacy"), "msg: {msg}"); + } + #[test] fn env_overrides_file() { let dir = tempfile::tempdir().unwrap(); @@ -279,24 +283,27 @@ token = "abc" std::fs::write( &path, r#" -registry_url = "ws://from-file" -daemon_url = "ws://from-file:1" +[shared] +registry_url = "http://from-file" token = "from-file" -bind_addr = "127.0.0.1:1" + +[daemon] daemon_id = "from-file" +daemon_url = "ws://from-file:1" +bind_addr = "127.0.0.1:1" "#, ) .unwrap(); let env = EnvOverrides { daemon_id: Some("from-env".into()), - registry_url: Some("ws://from-env".into()), + shared_registry_url: Some("https://from-env".into()), bind_addr: Some("0.0.0.0:9999".into()), daemon_url: Some("ws://from-env:1".into()), - token: Some("from-env".into()), + shared_token: Some("from-env".into()), }; let cfg = load_config(&path, env).unwrap(); assert_eq!(cfg.daemon_id, "from-env"); - assert_eq!(cfg.registry_url, "ws://from-env"); + assert_eq!(cfg.registry_url, "wss://from-env/daemon"); assert_eq!(cfg.daemon_url, "ws://from-env:1"); assert_eq!(cfg.token, "from-env"); assert_eq!(cfg.bind_addr, "0.0.0.0:9999".parse().unwrap()); @@ -309,7 +316,10 @@ daemon_id = "from-file" std::fs::write( &path, r#" -registry_url = "ws://r" +[shared] +registry_url = "http://r" + +[daemon] daemon_url = "ws://x" "#, ) @@ -321,11 +331,11 @@ daemon_url = "ws://x" env, path: p, } => { - assert_eq!(field, "token"); - assert_eq!(env, "MAESTRO_TOKEN"); + assert_eq!(field, "shared.token"); + assert_eq!(env, ENV_SHARED_TOKEN); assert_eq!(p, path); } - other => panic!("expected Missing(token), got {other:?}"), + other => panic!("expected Missing(shared.token), got {other:?}"), } } @@ -340,7 +350,10 @@ daemon_url = "ws://x" }; let err = load_config(&path, env).unwrap_err(); match err { - ConfigError::InvalidBindAddr { input, .. } => assert_eq!(input, "not-an-addr"), + ConfigError::InvalidBindAddr { input, env, .. } => { + assert_eq!(input, "not-an-addr"); + assert_eq!(env, ENV_DAEMON_BIND_ADDR); + } other => panic!("expected InvalidBindAddr, got {other:?}"), } } @@ -352,10 +365,13 @@ daemon_url = "ws://x" std::fs::write( &path, r#" -hostname = "vm-42" -registry_url = "ws://r" -daemon_url = "ws://x" +[shared] +registry_url = "http://r" token = "t" + +[daemon] +hostname = "vm-42" +daemon_url = "ws://x" "#, ) .unwrap(); @@ -365,18 +381,24 @@ token = "t" } #[test] - fn config_file_round_trips() { + fn registry_url_must_be_http_or_https() { let dir = tempfile::tempdir().unwrap(); let path = dir.path().join("config.toml"); - let file = DaemonConfigFile { - registry_url: Some("ws://r/daemon".into()), - daemon_url: Some("ws://x:7070".into()), - token: Some("secret".into()), - bind_addr: Some("0.0.0.0:7070".parse().unwrap()), - ..DaemonConfigFile::default() - }; - file.save(&path).unwrap(); - assert_eq!(DaemonConfigFile::load(&path).unwrap(), file); + std::fs::write( + &path, + r#" +[shared] +registry_url = "ws://r/daemon" +token = "t" + +[daemon] +daemon_url = "ws://x" +"#, + ) + .unwrap(); + let err = load_config(&path, EnvOverrides::default()).unwrap_err(); + let msg = err.to_string(); + assert!(msg.contains("registry_url"), "msg: {msg}"); } #[test] diff --git a/crates/maestro-daemon/src/configure.rs b/crates/maestro-daemon/src/configure.rs index 0621c84..343cb77 100644 --- a/crates/maestro-daemon/src/configure.rs +++ b/crates/maestro-daemon/src/configure.rs @@ -1,14 +1,20 @@ //! `maestrod configure` — interactive + flag-driven config bootstrap. //! -//! Writes `~/.config/maestrod/config.toml` (or `$MAESTROD_CONFIG`) from a -//! mix of CLI flags and (when stdin is a TTY) prompts. Refuses to overwrite -//! an existing file unless `--force` is passed. +//! Writes the unified Maestro config file at `~/.config/maestro/config.toml` +//! (or `$MAESTRO_CONFIG`) from a mix of CLI flags and (when stdin is a TTY) +//! prompts. Refuses to overwrite an existing file unless `--force` is passed. +//! +//! With `--force`, the daemon-relevant sections (`[shared]` and `[daemon]`) +//! are rewritten, but any `[cli]` / `[registry]` sections already in the file +//! are preserved. use std::io::{self, BufRead, IsTerminal, Write}; use std::net::SocketAddr; use std::path::{Path, PathBuf}; -use crate::config::{system_hostname, ConfigError, DaemonConfigFile, DEFAULT_BIND_ADDR}; +use maestro_config::{DaemonSection, MaestroConfigFile, SharedSection}; + +use crate::config::{system_hostname, ConfigError, DEFAULT_BIND_ADDR}; #[derive(Debug, thiserror::Error)] pub enum ConfigureError { @@ -70,7 +76,7 @@ pub fn run( let registry_url = resolve_required( "registry_url", - "Registry URL (e.g. wss://maestro.example.com/daemon)", + "Registry base URL (e.g. https://maestro.example.com)", inputs.registry_url, &mut handle, &stdin, @@ -86,7 +92,7 @@ pub fn run( )?; let token = resolve_required( "token", - "Shared MAESTRO_TOKEN (paste from the registry)", + "Shared token (MAESTRO_SHARED_TOKEN; paste from the registry)", inputs.token, &mut handle, &stdin, @@ -100,18 +106,23 @@ pub fn run( None => None, }; - let file = DaemonConfigFile { + // Read-modify-write: preserve `[cli]` / `[registry]` sections that may + // already exist (e.g. on a host that runs both the registry and the + // daemon, or one that's been used as a CLI client). + let mut file = MaestroConfigFile::load_or_default(path).map_err(ConfigError::from)?; + file.shared = SharedSection { + registry_url: Some(registry_url), + token: Some(token), + }; + file.daemon = DaemonSection { daemon_id: Some(daemon_id), hostname: Some(host), - registry_url: Some(registry_url), daemon_url: Some(daemon_url), - token: Some(token), bind_addr, state_dir: inputs.state_dir, worktree_root: inputs.worktree_root, }; - - file.save(path)?; + file.save(path).map_err(ConfigError::from)?; let _ = handle; // stdout handle only used during prompts; binary prints "wrote ..." Ok(path.to_path_buf()) } @@ -165,7 +176,7 @@ mod tests { fn full_inputs() -> ConfigureInputs { ConfigureInputs { - registry_url: Some("wss://reg/daemon".into()), + registry_url: Some("https://reg.example".into()), daemon_url: Some("ws://localhost:7070".into()), token: Some("secret".into()), daemon_id: Some("laptop".into()), @@ -177,14 +188,21 @@ mod tests { } #[test] - fn non_interactive_with_full_flags_writes_file() { + fn non_interactive_with_full_flags_writes_unified_file() { let dir = tempfile::tempdir().unwrap(); let path = dir.path().join("config.toml"); run(&path, full_inputs(), false, false).unwrap(); - let written = DaemonConfigFile::load(&path).unwrap(); - assert_eq!(written.daemon_id.as_deref(), Some("laptop")); - assert_eq!(written.token.as_deref(), Some("secret")); - assert_eq!(written.bind_addr.unwrap().to_string(), "0.0.0.0:7070"); + let written = MaestroConfigFile::load(&path).unwrap(); + assert_eq!( + written.shared.registry_url.as_deref(), + Some("https://reg.example") + ); + assert_eq!(written.shared.token.as_deref(), Some("secret")); + assert_eq!(written.daemon.daemon_id.as_deref(), Some("laptop")); + assert_eq!( + written.daemon.bind_addr.unwrap().to_string(), + "0.0.0.0:7070" + ); } #[test] @@ -213,13 +231,31 @@ mod tests { } #[test] - fn force_overwrites_existing_file() { + fn force_preserves_unrelated_sections() { + // Existing file has [cli] (set by the user) and [registry] (set by + // a co-located registry on the same host). `configure --force` + // should overwrite [shared] + [daemon] but leave those alone. let dir = tempfile::tempdir().unwrap(); let path = dir.path().join("config.toml"); - std::fs::write(&path, "garbage").unwrap(); + std::fs::write( + &path, + r#" +[cli] +cookie = "keep-me" + +[registry] +bind_addr = "0.0.0.0:8080" +"#, + ) + .unwrap(); run(&path, full_inputs(), false, true).unwrap(); - let written = DaemonConfigFile::load(&path).unwrap(); - assert_eq!(written.token.as_deref(), Some("secret")); + let written = MaestroConfigFile::load(&path).unwrap(); + assert_eq!(written.shared.token.as_deref(), Some("secret")); + assert_eq!(written.cli.cookie.as_deref(), Some("keep-me")); + assert_eq!( + written.registry.bind_addr, + Some("0.0.0.0:8080".parse().unwrap()) + ); } #[test] @@ -237,15 +273,15 @@ mod tests { let dir = tempfile::tempdir().unwrap(); let path = dir.path().join("config.toml"); let inputs = ConfigureInputs { - registry_url: Some("ws://r".into()), + registry_url: Some("http://r".into()), daemon_url: Some("ws://x".into()), token: Some("t".into()), hostname: Some("override-host".into()), ..ConfigureInputs::default() }; run(&path, inputs, false, false).unwrap(); - let written = DaemonConfigFile::load(&path).unwrap(); - assert_eq!(written.daemon_id.as_deref(), Some("override-host")); - assert_eq!(written.hostname.as_deref(), Some("override-host")); + let written = MaestroConfigFile::load(&path).unwrap(); + assert_eq!(written.daemon.daemon_id.as_deref(), Some("override-host")); + assert_eq!(written.daemon.hostname.as_deref(), Some("override-host")); } } diff --git a/crates/maestro-daemon/src/lib.rs b/crates/maestro-daemon/src/lib.rs index 0876675..e5463c7 100644 --- a/crates/maestro-daemon/src/lib.rs +++ b/crates/maestro-daemon/src/lib.rs @@ -17,10 +17,7 @@ pub mod sidecar; pub mod tmux; pub mod worktree; -pub use config::{ - default_config_path, default_state_dir, load_config, DaemonConfig, DaemonConfigFile, - EnvOverrides, -}; +pub use config::{default_config_path, default_state_dir, load_config, DaemonConfig, EnvOverrides}; pub use configure::{run as run_configure, ConfigureError, ConfigureInputs}; pub use jwt::{issue as issue_jwt, JwtError, JwtVerifier}; pub use registry_client::{ClientError, RegistryClient, HEARTBEAT_INTERVAL}; diff --git a/crates/maestro-daemon/src/main.rs b/crates/maestro-daemon/src/main.rs index acf5fd1..33e48c6 100644 --- a/crates/maestro-daemon/src/main.rs +++ b/crates/maestro-daemon/src/main.rs @@ -27,8 +27,8 @@ use tracing_subscriber::EnvFilter; about = "Per-host Maestro daemon: tmux wrapper + registry client" )] struct Cli { - /// Override the config-file path (default `~/.config/maestrod/config.toml`). - #[arg(long, global = true, env = "MAESTROD_CONFIG")] + /// Override the unified Maestro config-file path (default `~/.config/maestro/config.toml`). + #[arg(long, global = true, env = "MAESTRO_CONFIG")] config: Option, #[command(subcommand)] @@ -39,19 +39,20 @@ struct Cli { enum Command { /// Start the daemon. Default when no subcommand is given. Run, - /// Write `~/.config/maestrod/config.toml` interactively or from flags. + /// Write `~/.config/maestro/config.toml` interactively or from flags. Configure(ConfigureCmd), } #[derive(Debug, clap::Args)] struct ConfigureCmd { - /// Registry WS URL (e.g. `wss://maestro.example.com/daemon`). + /// Registry base URL (e.g. `https://maestro.example.com`). The daemon + /// derives its WS endpoint by scheme-swapping and appending `/daemon`. #[arg(long)] registry_url: Option, /// Reachable URL clients dial for this daemon's attach WS. #[arg(long)] daemon_url: Option, - /// Shared MAESTRO_TOKEN. Tip: pipe from `pass` / `op` / `secret-tool`. + /// Shared token (MAESTRO_SHARED_TOKEN). Tip: pipe from `pass` / `op` / `secret-tool`. #[arg(long)] token: Option, /// Stable daemon ID. Defaults to the system hostname. diff --git a/crates/maestro-e2e/src/daemon.rs b/crates/maestro-e2e/src/daemon.rs index 0e692fd..1972513 100644 --- a/crates/maestro-e2e/src/daemon.rs +++ b/crates/maestro-e2e/src/daemon.rs @@ -28,12 +28,13 @@ pub struct Daemon { } impl Daemon { - /// Spawn a fresh daemon pointing at `registry_url` (the daemon-facing WS - /// URL — usually `ws:///daemon`). - pub async fn spawn(daemon_id: &str, registry_url: &str, token: &str) -> Self { + /// Spawn a fresh daemon pointing at `registry_base_url` (the registry's + /// HTTP base, e.g. `http://127.0.0.1:1234`). The daemon derives its WS + /// endpoint internally via `derive_daemon_ws_url`. + pub async fn spawn(daemon_id: &str, registry_base_url: &str, token: &str) -> Self { let port = free_port(); let bind = SocketAddr::from((Ipv4Addr::LOCALHOST, port)); - Self::spawn_with_port(daemon_id, registry_url, token, bind).await + Self::spawn_with_port(daemon_id, registry_base_url, token, bind).await } /// Stop the underlying process, but keep its state dirs around so we can @@ -151,11 +152,11 @@ fn launch( .env("XDG_CONFIG_HOME", home.join(".config")) .env("XDG_STATE_HOME", home.join(".local/state")) .env("TMUX_TMPDIR", tmux_tmpdir) - .env("MAESTRO_TOKEN", token) - .env("MAESTROD_DAEMON_ID", daemon_id) - .env("MAESTROD_REGISTRY_URL", registry_url) - .env("MAESTROD_BIND_ADDR", bind.to_string()) - .env("MAESTROD_DAEMON_URL", daemon_url) + .env("MAESTRO_SHARED_TOKEN", token) + .env("MAESTRO_DAEMON_ID", daemon_id) + .env("MAESTRO_SHARED_REGISTRY_URL", registry_url) + .env("MAESTRO_DAEMON_BIND_ADDR", bind.to_string()) + .env("MAESTRO_DAEMON_URL", daemon_url) .env("RUST_LOG", "warn") .stdout(Stdio::null()) .stderr(Stdio::inherit()) diff --git a/crates/maestro-e2e/tests/e2e.rs b/crates/maestro-e2e/tests/e2e.rs index d16eb65..87facd8 100644 --- a/crates/maestro-e2e/tests/e2e.rs +++ b/crates/maestro-e2e/tests/e2e.rs @@ -26,7 +26,7 @@ async fn bring_up(daemon_ids: &[&str]) -> (Registry, Vec, TestClient) { let registry = Registry::spawn(TOKEN).await; let mut daemons = Vec::with_capacity(daemon_ids.len()); for id in daemon_ids { - let d = Daemon::spawn(id, ®istry.daemon_ws_url(), TOKEN).await; + let d = Daemon::spawn(id, ®istry.http_base(), TOKEN).await; daemons.push(d); } let client = TestClient::new(®istry.http_base(), ®istry.ws_base());