-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[ENH] Use writer injection in CLI #4500
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
base: main
Are you sure you want to change the base?
Conversation
Reviewer ChecklistPlease leverage this checklist to ensure your code review is thorough before approving Testing, Bugs, Errors, Logs, Documentation
System Compatibility
Quality
|
Writer Injection Pattern for CLI Commands This PR refactors the CLI command system to use dependency injection for writing output, making the code more testable. It introduces a CommandHandler trait with an async run method and adapts the RunCommand to use this pattern with a configurable writer. This change improves testability by allowing output to be captured during tests while maintaining the same user-facing functionality. Key Changes: Affected Areas: Potential Impact: Functionality: No change to user-facing functionality, though internal command execution flow is significantly altered Performance: Minimal impact, possibly slight overhead from trait-based dispatch Security: No significant security implications Scalability: Improved code extensibility for adding new commands that follow the same pattern Review Focus: Testing Needed• Verify all Code Quality Assessmentrust/cli/src/commands/run.rs: Major restructuring with good separation of concerns and testability improvements rust/cli/src/lib.rs: Added CommandHandler trait and appropriate dispatch logic rust/cli/src/ui_utils.rs: Added cli_writeln! macro for consistent error handling Best PracticesTesting: Error Handling: Potential Issues• Error propagation from commands to CLI exit codes isn't fully implemented This summary was automatically generated by @propel-code-bot |
rust/cli/src/lib.rs
Outdated
if result.is_err() { | ||
let error_message = result.err().unwrap().to_string(); | ||
eprintln!("{}", error_message.red()); | ||
} | ||
|
||
println!(); | ||
|
||
Ok(()) |
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.
[CriticalError]
The chroma_cli
function currently prints errors from command execution but then proceeds to return Ok(())
and prints a final newline regardless of the outcome. This prevents the main application from knowing if an error occurred (e.g., to exit with an appropriate status code) because the error is not propagated. The function should propagate the Err
variant, and the final newline should ideally be conditional or consistently placed after any primary output.
if result.is_err() { | |
let error_message = result.err().unwrap().to_string(); | |
eprintln!("{}", error_message.red()); | |
} | |
println!(); | |
Ok(()) | |
// Assuming 'result' holds the outcome of command execution as determined | |
// by the preceding logic (synchronous calls or async handler outcome). | |
match result { | |
Ok(_) => { | |
println!(); // Final newline on success. | |
Ok(()) | |
} | |
Err(e) => { | |
eprintln!("{}", e.to_string().red()); | |
println!(); // Final newline after error message for console readability. | |
Err(e) // Propagate the error. | |
} | |
} | |
} // This closes the function body, ensure it replaces from the original `if result.is_err()...` to `Ok(())` |
⚡ Committable suggestion
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.
let args: Vec<String> = env::args().collect(); | ||
chroma_cli(args) | ||
let _ = chroma_cli(args); | ||
} |
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.
[BestPractice]
The main
function should handle the Result
returned by chroma_cli
to ensure the program exits with a non-zero status code if an error occurs. Currently, let _ = chroma_cli(args);
discards this result. Assuming chroma_cli
is modified to correctly propagate errors, main
should act on them.
let args: Vec<String> = env::args().collect(); | |
chroma_cli(args) | |
let _ = chroma_cli(args); | |
} | |
fn main() { | |
let args: Vec<String> = env::args().collect(); | |
if let Err(_e) = chroma_cli::chroma_cli(args) { | |
// Assuming chroma_cli now prints its own errors before returning Err. | |
// Otherwise, you might want to print the error here: eprintln!("Error: {}", _e); | |
std::process::exit(1); | |
} | |
} |
⚡ Committable suggestion
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.
sleep(Duration::from_millis(500)).await; | ||
|
||
let url = format!("http://localhost:{}", port); | ||
let chroma_client = ChromaClient::local_default(); |
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.
[CriticalError]
The test test_run
starts a server on a dynamic port (e.g., 8001 defined by the port
variable) but then creates a ChromaClient
using ChromaClient::local_default()
. This default client likely connects to a standard port (e.g., 8000) and not the port the test server is running on. The client should be initialized with the URL of the test server.
let chroma_client = ChromaClient::local_default(); | |
let url = format!("http://localhost:{}", port); | |
let chroma_client = ChromaClient::new(url); |
⚡ Committable suggestion
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.
Description of changes
We currently can't test the CLI properly because there is no easy way to capture stdout in Rust. This is a prototype for a refactor for the CLI, such that each command takes in a "Writer". For standard runs by users this is just Stdout, and for testing this could be any kind of buffer, which we can use to verify that the CLI output was correct.
The change only implemented for the "run" command, as this is a prototype for using this approach.
Test plan
pytest
for python,yarn test
for js,cargo test
for rustDocumentation Changes
N/A