brev-cli
brev-cli copied to clipboard
Refactor `ls` command, enhance argument handling, and add unit tests
(Full disclosure, this is an attempt to get hired 😄 )
Summary
This PR introduces a comprehensive refactor of the brev ls command within our CLI, focusing on improving argument handling, user guidance, and overall maintainability. Additionally, the PR adds robust unit tests to ensure the reliability of these changes and provides better tools for testing the terminal output.
Key Changes
-
Refactored Argument Handling:
- Moved argument routing directly into the
RunEfunction of thecobra.Commandto simplify the command's logic. - Replaced the previous
if-elsestructure inhandleLsArgwith a more concise and readableswitchstatement. - Extended
ValidArgsto include all possible singular and plural forms of valid arguments ("orgs", "workspaces", "users", "hosts").
- Moved argument routing directly into the
-
Enhanced Help Output:
- The
lscommand now properly lists all valid arguments in the help message, providing users with clear guidance on the available options. - Updated usage examples and error messages to better reflect the command's functionality and improve user experience.
- The
-
Improved Error Handling:
- Enhanced error handling for scenarios involving invalid arguments and admin-restricted operations (e.g., user and host management).
- Added detailed validation errors when users attempt to perform operations they are not authorized for.
-
Unit Testing:
- Added extensive unit tests for the
lscommand to cover a variety of scenarios, including:- No arguments provided (default behavior).
- Specific valid arguments (e.g., "org", "workspace").
- Invalid arguments and their corresponding error handling.
- Flag-specific behavior (e.g.,
--all,--org).
- Created a
NewTestTerminalfunction to support testing, allowing the capture and inspection of terminal output (stdout,stderr, and verbose output).
- Added extensive unit tests for the
-
Terminal Utility Enhancements:
- Modified the
Terminalstruct to include anOut()method to access the output writer.
- Modified the
Impact
- The
lscommand is now more intuitive and user-friendly, with clearer guidance and improved error messaging. - The refactor has made the codebase cleaner, more maintainable, and easier to extend in the future.
- The introduction of unit tests ensures that these changes are robust and that the
lscommand behaves as expected across a variety of use cases.