story-209: accept optional positional path argument on startup
Add CLI path argument support: `story-kit-server /path/to/project` opens the given project directly (scaffolding .story_kit/ if needed) instead of relying on auto-detection. Resolves conflict with story-208's port parameter. Squash merge of feature/story-209 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -29,6 +29,13 @@ use std::path::PathBuf;
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::broadcast;
|
||||
|
||||
/// Resolve the optional positional path argument (everything after the binary
|
||||
/// name) into an absolute `PathBuf`. Returns `None` when no argument was
|
||||
/// supplied so that the caller can fall back to the auto-detect behaviour.
|
||||
fn parse_project_path_arg(args: &[String], cwd: &std::path::Path) -> Option<PathBuf> {
|
||||
args.first().map(|s| io::fs::resolve_cli_path(cwd, s))
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), std::io::Error> {
|
||||
let app_state = Arc::new(SessionState::default());
|
||||
@@ -39,26 +46,56 @@ async fn main() -> Result<(), std::io::Error> {
|
||||
|
||||
let port = resolve_port();
|
||||
|
||||
// Auto-detect a .story_kit/ project in cwd or parent directories.
|
||||
if let Some(project_root) = find_story_kit_root(&cwd) {
|
||||
io::fs::open_project(
|
||||
project_root.to_string_lossy().to_string(),
|
||||
// Collect CLI args, skipping the binary name (argv[0]).
|
||||
let cli_args: Vec<String> = std::env::args().skip(1).collect();
|
||||
let explicit_path = parse_project_path_arg(&cli_args, &cwd);
|
||||
|
||||
if let Some(explicit_root) = explicit_path {
|
||||
// An explicit path was given on the command line.
|
||||
// Open it directly — scaffold .story_kit/ if it is missing — and
|
||||
// exit with a clear error message if the path is invalid.
|
||||
match io::fs::open_project(
|
||||
explicit_root.to_string_lossy().to_string(),
|
||||
&app_state,
|
||||
store.as_ref(),
|
||||
port,
|
||||
)
|
||||
.await
|
||||
.unwrap_or_else(|e| {
|
||||
slog!("Warning: failed to auto-open project at {project_root:?}: {e}");
|
||||
project_root.to_string_lossy().to_string()
|
||||
});
|
||||
|
||||
// Validate agent config for the detected project root.
|
||||
config::ProjectConfig::load(&project_root)
|
||||
.unwrap_or_else(|e| panic!("Invalid project.toml: {e}"));
|
||||
{
|
||||
Ok(_) => {
|
||||
if let Some(root) = app_state.project_root.lock().unwrap().as_ref() {
|
||||
config::ProjectConfig::load(root)
|
||||
.unwrap_or_else(|e| panic!("Invalid project.toml: {e}"));
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("error: {e}");
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// No .story_kit/ found — fall back to cwd so existing behaviour is preserved.
|
||||
*app_state.project_root.lock().unwrap() = Some(cwd.clone());
|
||||
// No path argument — auto-detect a .story_kit/ project in cwd or
|
||||
// parent directories (preserves existing behaviour).
|
||||
if let Some(project_root) = find_story_kit_root(&cwd) {
|
||||
io::fs::open_project(
|
||||
project_root.to_string_lossy().to_string(),
|
||||
&app_state,
|
||||
store.as_ref(),
|
||||
port,
|
||||
)
|
||||
.await
|
||||
.unwrap_or_else(|e| {
|
||||
slog!("Warning: failed to auto-open project at {project_root:?}: {e}");
|
||||
project_root.to_string_lossy().to_string()
|
||||
});
|
||||
|
||||
// Validate agent config for the detected project root.
|
||||
config::ProjectConfig::load(&project_root)
|
||||
.unwrap_or_else(|e| panic!("Invalid project.toml: {e}"));
|
||||
} else {
|
||||
// No .story_kit/ found — fall back to cwd so existing behaviour is preserved.
|
||||
*app_state.project_root.lock().unwrap() = Some(cwd.clone());
|
||||
}
|
||||
}
|
||||
|
||||
let workflow = Arc::new(std::sync::Mutex::new(WorkflowState::default()));
|
||||
@@ -215,4 +252,43 @@ name = "coder"
|
||||
config::ProjectConfig::load(tmp.path())
|
||||
.unwrap_or_else(|e| panic!("Invalid project.toml: {e}"));
|
||||
}
|
||||
|
||||
// ── parse_project_path_arg ────────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn parse_project_path_arg_none_when_no_args() {
|
||||
let cwd = PathBuf::from("/home/user/project");
|
||||
let result = parse_project_path_arg(&[], &cwd);
|
||||
assert!(result.is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_project_path_arg_returns_path_for_absolute_arg() {
|
||||
let cwd = PathBuf::from("/home/user/project");
|
||||
let args = vec!["/some/absolute/path".to_string()];
|
||||
let result = parse_project_path_arg(&args, &cwd).unwrap();
|
||||
// Absolute path returned as-is (canonicalize may fail, fallback used)
|
||||
assert!(result.ends_with("absolute/path") || result == PathBuf::from("/some/absolute/path"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_project_path_arg_resolves_dot_to_cwd() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let cwd = tmp.path().to_path_buf();
|
||||
let args = vec![".".to_string()];
|
||||
let result = parse_project_path_arg(&args, &cwd).unwrap();
|
||||
// "." relative to an existing cwd should canonicalize to the cwd itself
|
||||
assert_eq!(result, cwd.canonicalize().unwrap_or(cwd));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_project_path_arg_resolves_relative_path() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let cwd = tmp.path().to_path_buf();
|
||||
let subdir = cwd.join("myproject");
|
||||
std::fs::create_dir_all(&subdir).unwrap();
|
||||
let args = vec!["myproject".to_string()];
|
||||
let result = parse_project_path_arg(&args, &cwd).unwrap();
|
||||
assert_eq!(result, subdir.canonicalize().unwrap_or(subdir));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user