From eb6b07531af87dd080eb6a5b28a4ac0191402d0b Mon Sep 17 00:00:00 2001 From: dave Date: Sun, 17 May 2026 14:43:53 +0000 Subject: [PATCH] huskies: merge 1114 story new project: --path flag to override default host directory --- .../matrix/bot/messages/on_room_message.rs | 1 + .../src/chat/transport/matrix/new_project.rs | 66 +++++++++++++++++-- server/src/gateway/tests.rs | 3 + server/src/service/gateway/config.rs | 13 ++++ server/src/service/gateway/mod.rs | 2 + 5 files changed, 78 insertions(+), 7 deletions(-) diff --git a/server/src/chat/transport/matrix/bot/messages/on_room_message.rs b/server/src/chat/transport/matrix/bot/messages/on_room_message.rs index 69918fcc..9be7e95a 100644 --- a/server/src/chat/transport/matrix/bot/messages/on_room_message.rs +++ b/server/src/chat/transport/matrix/bot/messages/on_room_message.rs @@ -281,6 +281,7 @@ pub(in crate::chat::transport::matrix::bot) async fn on_room_message( cmd.stack.as_deref(), cmd.git_url.as_deref(), cmd.git_token.as_deref(), + cmd.host_path.as_deref(), store, &ctx.services.project_root, ) diff --git a/server/src/chat/transport/matrix/new_project.rs b/server/src/chat/transport/matrix/new_project.rs index fe66728d..947c6bcc 100644 --- a/server/src/chat/transport/matrix/new_project.rs +++ b/server/src/chat/transport/matrix/new_project.rs @@ -1,8 +1,8 @@ -//! `new project ` chat command — Phase 4: `--git` clone and push credentials. +//! `new project ` chat command — Phase 5: `--path` override. //! //! Provisions a project container and registers it with the gateway. //! The command is gateway-only: -//! `new project [--stack ] [--git ] [--git-token ]` +//! `new project [--stack ] [--git ] [--git-token ] [--path ]` //! //! Without `--stack`, the orchestrator inspects the (just-cloned or //! just-init'd) source tree for stack markers found in @@ -47,7 +47,7 @@ use tokio::sync::RwLock; use crate::service::gateway::config::ProjectEntry; -/// Parsed result of a `new project [--stack ] [--git ] [--git-token ]` command. +/// Parsed result of a `new project [--stack ] [--git ] [--git-token ] [--path ]` command. pub struct NewProjectCommand { /// Project name (alphanumeric, hyphens, underscores). pub name: String, @@ -63,9 +63,14 @@ pub struct NewProjectCommand { /// Stored in the container's git credential helper by the entrypoint. /// **Never echoed in any chat reply or log line.** pub git_token: Option, + /// Override the default host directory (`~/huskies//`). + /// + /// When `Some`, the project is created at this path instead of the default. + /// The same existence check applies: the path must not already exist. + pub host_path: Option, } -/// Parse a `new project [--stack ] [--git ] [--git-token ]` command. +/// Parse a `new project [--stack ] [--git ] [--git-token ] [--path ]` command. /// /// Returns `Some(NewProjectCommand)` when the stripped message starts with /// "new project" (case-insensitive). An empty name (bare "new project" with @@ -97,12 +102,14 @@ pub fn extract_new_project_command( let stack = parse_flag(&remaining, "--stack"); let git_url = parse_flag(&remaining, "--git"); let git_token = parse_flag(&remaining, "--git-token"); + let host_path = parse_flag(&remaining, "--path"); Some(NewProjectCommand { name, stack, git_url, git_token, + host_path, }) } @@ -355,7 +362,8 @@ async fn generate_ssh_keypair(key_path: &std::path::Path) -> Result/`, scaffolds `.huskies/`, runs `git clone` (when +/// Creates the project directory (default `~/huskies//`, or `host_path` +/// when `--path` is supplied), scaffolds `.huskies/`, runs `git clone` (when /// `git_url` is provided) or `git init`, auto-detects or honours the requested /// stack, generates an SSH keypair, launches the appropriate Docker container, /// and registers the project in the gateway's in-memory store and the CRDT. @@ -369,6 +377,7 @@ pub async fn handle_new_project( stack: Option<&str>, git_url: Option<&str>, git_token: Option<&str>, + host_path_override: Option<&str>, projects_store: &Arc>>, config_dir: &Path, ) -> String { @@ -398,9 +407,12 @@ pub async fn handle_new_project( } } - // Default host path: ~/huskies// let home = std::env::var("HOME").unwrap_or_else(|_| "/home/huskies".to_string()); - let host_path = std::path::PathBuf::from(&home).join("huskies").join(name); + // `--path` overrides the default `~/huskies//`. + let host_path = match host_path_override { + Some(p) => std::path::PathBuf::from(p), + None => std::path::PathBuf::from(&home).join("huskies").join(name), + }; if host_path.exists() { return format!( @@ -630,6 +642,7 @@ pub async fn handle_new_project( url: Some(container_url.clone()), auth_token: None, ssh_port: Some(ssh_port), + host_path: Some(host_path.to_string_lossy().into_owned()), }, ); crate::service::gateway::io::save_config(&projects, config_dir).await; @@ -1135,6 +1148,45 @@ mod tests { assert_eq!(cmd.git_token, None); } + // ── Phase 5: --path flag parsing ───────────────────────────────────────── + + #[test] + fn extract_parses_path_flag() { + let cmd = extract_new_project_command( + "@timmy new project myapp --path /projects/myapp", + "Timmy", + "@timmy:srv.local", + ) + .unwrap(); + assert_eq!(cmd.name, "myapp"); + assert_eq!(cmd.host_path, Some("/projects/myapp".to_string())); + } + + #[test] + fn extract_path_flag_with_stack_and_git() { + let cmd = extract_new_project_command( + "@timmy new project myapp --stack rust --git https://github.com/u/r --path /srv/myapp", + "Timmy", + "@timmy:srv.local", + ) + .unwrap(); + assert_eq!(cmd.name, "myapp"); + assert_eq!(cmd.stack, Some("rust".to_string())); + assert_eq!(cmd.git_url, Some("https://github.com/u/r".to_string())); + assert_eq!(cmd.host_path, Some("/srv/myapp".to_string())); + } + + #[test] + fn extract_no_path_flag_returns_none() { + let cmd = extract_new_project_command( + "@timmy new project myapp --stack node", + "Timmy", + "@timmy:srv.local", + ) + .unwrap(); + assert_eq!(cmd.host_path, None); + } + #[test] fn inject_token_into_https_url() { let result = inject_token_into_url("https://github.com/user/repo", "mytoken"); diff --git a/server/src/gateway/tests.rs b/server/src/gateway/tests.rs index 1ea89c44..e2a0d19e 100644 --- a/server/src/gateway/tests.rs +++ b/server/src/gateway/tests.rs @@ -1176,6 +1176,7 @@ async fn ws_only_sled_handles_tools_list_and_tools_call() { url: None, auth_token: Some("secret".into()), ssh_port: None, + host_path: None, }, ); let config = GatewayConfig { @@ -1246,6 +1247,7 @@ async fn two_concurrent_sleds_are_routed_by_active_project() { url: None, auth_token: Some("alpha-tok".into()), ssh_port: None, + host_path: None, }, ); projects.insert( @@ -1254,6 +1256,7 @@ async fn two_concurrent_sleds_are_routed_by_active_project() { url: None, auth_token: Some("beta-tok".into()), ssh_port: None, + host_path: None, }, ); let config = GatewayConfig { diff --git a/server/src/service/gateway/config.rs b/server/src/service/gateway/config.rs index f9665bb5..2275630f 100644 --- a/server/src/service/gateway/config.rs +++ b/server/src/service/gateway/config.rs @@ -33,6 +33,13 @@ pub struct ProjectEntry { /// `ssh huskies@127.0.0.1 -p -i ~/.huskies//id_ed25519`. #[serde(default, skip_serializing_if = "Option::is_none")] pub ssh_port: Option, + /// Absolute host path of the project directory (workspace bind-mount source). + /// + /// Set by `new project` (story 1114). When `--path` is supplied the value + /// differs from the default `~/huskies//`. Stored here so that later + /// commands can route to the correct directory without re-deriving it. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub host_path: Option, } impl ProjectEntry { @@ -44,6 +51,7 @@ impl ProjectEntry { url: Some(url.into()), auth_token: None, ssh_port: None, + host_path: None, } } @@ -214,6 +222,7 @@ auth_token = "secret" url: None, auth_token: Some("secret".into()), ssh_port: None, + host_path: None, }, ); let config = GatewayConfig { @@ -248,6 +257,7 @@ auth_token = "secret" url: None, auth_token: Some("tok".into()), ssh_port: None, + host_path: None, }, ); assert_eq!(validate_project_exists(&projects, "ws").unwrap(), ""); @@ -267,6 +277,7 @@ auth_token = "secret" url: None, auth_token: Some("tok".into()), ssh_port: None, + host_path: None, }; assert!(!e.has_url()); } @@ -309,6 +320,7 @@ auth_token = "secret" url: Some("http://a:3001".into()), auth_token: Some("mysecret".into()), ssh_port: None, + host_path: None, }; let mut projects = BTreeMap::new(); projects.insert("myproj".into(), entry); @@ -334,6 +346,7 @@ auth_token = "secret" url: Some("http://127.0.0.1:3101".into()), auth_token: None, ssh_port: Some(2201), + host_path: None, }; let mut projects = BTreeMap::new(); projects.insert("myproj".into(), entry); diff --git a/server/src/service/gateway/mod.rs b/server/src/service/gateway/mod.rs index de614732..4bdb5dda 100644 --- a/server/src/service/gateway/mod.rs +++ b/server/src/service/gateway/mod.rs @@ -748,6 +748,7 @@ mod tests { url: None, auth_token: Some("tok".into()), ssh_port: None, + host_path: None, }, ); let config = GatewayConfig { @@ -887,6 +888,7 @@ mod tests { url: Some("http://huskies:3001".into()), auth_token: Some("secret-token".into()), ssh_port: None, + host_path: None, }, ); let config = GatewayConfig {