Skip to content
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

Refactor ls command, enhance argument handling, and add unit tests #198

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

asideofcode-dev
Copy link

@asideofcode-dev asideofcode-dev commented Aug 14, 2024

(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.

- Refactored the `ls` command to include argument routing directly within the `RunE` function for improved clarity and maintainability.
- Replaced `if-else` statements in `handleLsArg` with a cleaner `switch` statement.
- Updated the help output to properly display valid arguments for the `ls` command, enhancing user guidance.
- Improved error handling, particularly for invalid arguments and admin-only operations.
- Refactored `NewCmdLs` to include dynamic routing and improved user experience.
- Introduced new unit tests for various `ls` command scenarios, ensuring coverage for no args, valid args, invalid args, and specific flag handling.
- Added a `NewTestTerminal` function to facilitate testing by providing an easily configurable terminal instance with buffers for `stdout`, `stderr`, and verbose output.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant