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");