-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add resources directory validation for init and install commands #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
this is an mcp test |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR adds automatic detection and validation of the resources directory for the init and install commands, ensuring that modules.json is always placed under the correct directory.
- Introduces
find_resources_directory()to locate the nearestresourcesfolder - Updates CLI flow in
main.rsto call validation before runninginitorinstall - Adds user feedback prints in
installer::init_fxpkg
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| src/main.rs | New helper to find resources dir and updated command logic |
| src/installer/mod.rs | Added success message print after creating modules.json |
Comments suppressed due to low confidence (1)
src/main.rs:71
- No tests cover the failure path of find_resources_directory; consider adding unit tests to verify both success and error scenarios.
Err("Could not find 'resources' directory. Please run 'fxpkg init' from within or above a 'resources' directory.".to_string())
| fn find_resources_directory(current_path: &Path) -> Result<String, String> { | ||
| let mut path = current_path.to_path_buf(); | ||
|
|
||
| // Check if current directory is already "resources" | ||
| if path.file_name().and_then(|n| n.to_str()) == Some("resources") { | ||
| return Ok(path.to_string_lossy().to_string()); | ||
| } | ||
|
|
||
| // Traverse upwards to find "resources" directory | ||
| loop { | ||
| let resources_path = path.join("resources"); | ||
| if resources_path.exists() && resources_path.is_dir() { | ||
| return Ok(resources_path.to_string_lossy().to_string()); |
Copilot
AI
May 24, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider returning a PathBuf instead of String to preserve path semantics and avoid unnecessary string conversions.
| fn find_resources_directory(current_path: &Path) -> Result<String, String> { | |
| let mut path = current_path.to_path_buf(); | |
| // Check if current directory is already "resources" | |
| if path.file_name().and_then(|n| n.to_str()) == Some("resources") { | |
| return Ok(path.to_string_lossy().to_string()); | |
| } | |
| // Traverse upwards to find "resources" directory | |
| loop { | |
| let resources_path = path.join("resources"); | |
| if resources_path.exists() && resources_path.is_dir() { | |
| return Ok(resources_path.to_string_lossy().to_string()); | |
| fn find_resources_directory(current_path: &Path) -> Result<PathBuf, String> { | |
| let mut path = current_path.to_path_buf(); | |
| // Check if current directory is already "resources" | |
| if path.file_name().and_then(|n| n.to_str()) == Some("resources") { | |
| return Ok(path); | |
| } | |
| // Traverse upwards to find "resources" directory | |
| loop { | |
| let resources_path = path.join("resources"); | |
| if resources_path.exists() && resources_path.is_dir() { | |
| return Ok(resources_path); |
| /// The package to install | ||
| name: String, | ||
| } | ||
|
|
Copilot
AI
May 24, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a doc comment explaining the lookup strategy and error cases for find_resources_directory to improve readability and maintainability.
| /// Locates the "resources" directory starting from the given path. | |
| /// | |
| /// This function first checks if the current directory is named "resources". | |
| /// If not, it traverses upwards through parent directories to find a directory | |
| /// named "resources". If the directory is found, its path is returned as a `String`. | |
| /// | |
| /// # Errors | |
| /// Returns an `Err` with a descriptive message if the "resources" directory | |
| /// cannot be found. This typically happens if the function is called from a | |
| /// location that is not within or above a "resources" directory. |
| match find_resources_directory(&pwd) { | ||
| Ok(resources_path) => { | ||
| let mut pkg_installer = installer::PackageInstall::new(); | ||
| pkg_installer | ||
| .install(&resources_path, &package.name) | ||
| .await; | ||
| } | ||
| Err(error_msg) => { | ||
| eprintln!("Error: {}", error_msg); | ||
| std::process::exit(1); | ||
| } | ||
| } | ||
| } | ||
| Commands::Init => { | ||
| let pwd = env::current_dir().unwrap(); | ||
| let path = pwd.to_str().unwrap(); | ||
|
|
||
| installer::init_fxpkg(path); | ||
| match find_resources_directory(&pwd) { | ||
| Ok(resources_path) => { | ||
| installer::init_fxpkg(&resources_path); | ||
| println!("Initialized fxpkg in: {}", resources_path); | ||
| } | ||
| Err(error_msg) => { | ||
| eprintln!("Error: {}", error_msg); | ||
| std::process::exit(1); | ||
| } | ||
| } |
Copilot
AI
May 24, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error handling logic for both commands is duplicated; extract the match block into a helper to reduce repetition.
|
mcp test |
Summary
This PR implements automatic resources directory detection and validation for both
initandinstallcommands, ensuring thatmodules.jsonis always created in the correct location.Key Changes
find_resources_directory()function that traverses upward to find resources directoryHow It Works
The commands now work from:
This ensures modules.json is always created in the correct resources directory regardless of where the user runs the command.