brev-cli icon indicating copy to clipboard operation
brev-cli copied to clipboard

Refactor `ls` command, enhance argument handling, and add unit tests

Open asideofcode-dev opened this issue 1 year ago • 0 comments

(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

  1. Refactored Argument Handling:

    • Moved argument routing directly into the RunE function of the cobra.Command to simplify the command's logic.
    • Replaced the previous if-else structure in handleLsArg with a more concise and readable switch statement.
    • Extended ValidArgs to include all possible singular and plural forms of valid arguments ("orgs", "workspaces", "users", "hosts").
  2. Enhanced Help Output:

    • The ls command 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.
  3. 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.
  4. Unit Testing:

    • Added extensive unit tests for the ls command 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 NewTestTerminal function to support testing, allowing the capture and inspection of terminal output (stdout, stderr, and verbose output).
  5. Terminal Utility Enhancements:

    • Modified the Terminal struct to include an Out() method to access the output writer.

Impact

  • The ls command 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 ls command behaves as expected across a variety of use cases.

asideofcode-dev avatar Aug 14 '24 19:08 asideofcode-dev