huskies: merge 1114 story new project: --path flag to override default host directory

This commit is contained in:
dave
2026-05-17 14:43:53 +00:00
parent 2d6846fe03
commit eb6b07531a
5 changed files with 78 additions and 7 deletions
@@ -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,
)
@@ -1,8 +1,8 @@
//! `new project <name>` chat command — Phase 4: `--git` clone and push credentials.
//! `new project <name>` chat command — Phase 5: `--path` override.
//!
//! Provisions a project container and registers it with the gateway.
//! The command is gateway-only:
//! `new project <name> [--stack <stack>] [--git <url>] [--git-token <token>]`
//! `new project <name> [--stack <stack>] [--git <url>] [--git-token <token>] [--path <dir>]`
//!
//! 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 <name> [--stack <s>] [--git <url>] [--git-token <tok>]` command.
/// Parsed result of a `new project <name> [--stack <s>] [--git <url>] [--git-token <tok>] [--path <dir>]` 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<String>,
/// Override the default host directory (`~/huskies/<name>/`).
///
/// 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<String>,
}
/// Parse a `new project <name> [--stack <s>] [--git <url>] [--git-token <tok>]` command.
/// Parse a `new project <name> [--stack <s>] [--git <url>] [--git-token <tok>] [--path <dir>]` 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<String, Stri
/// Bootstrap a new project from the `new project` chat command.
///
/// Creates `~/huskies/<name>/`, scaffolds `.huskies/`, runs `git clone` (when
/// Creates the project directory (default `~/huskies/<name>/`, 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<RwLock<BTreeMap<String, ProjectEntry>>>,
config_dir: &Path,
) -> String {
@@ -398,9 +407,12 @@ pub async fn handle_new_project(
}
}
// Default host path: ~/huskies/<name>/
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/<name>/`.
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");
+3
View File
@@ -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 {
+13
View File
@@ -33,6 +33,13 @@ pub struct ProjectEntry {
/// `ssh huskies@127.0.0.1 -p <ssh_port> -i ~/.huskies/<name>/id_ed25519`.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub ssh_port: Option<u16>,
/// 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/<name>/`. 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<String>,
}
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);
+2
View File
@@ -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 {