Skip to content

Refactor resource and tool label management commands#1189

Draft
matheusandre1 wants to merge 1 commit into
wanaku-ai:mainfrom
matheusandre1:issue1178
Draft

Refactor resource and tool label management commands#1189
matheusandre1 wants to merge 1 commit into
wanaku-ai:mainfrom
matheusandre1:issue1178

Conversation

@matheusandre1
Copy link
Copy Markdown
Contributor

@matheusandre1 matheusandre1 commented May 23, 2026

Closes: #1178

Summary by Sourcery

Refactor CLI label and removal commands to share common helpers for validation, label parsing, batch operations, and not-found handling across resources, tools, namespaces, data stores, and related commands.

Enhancements:

  • Introduce a reusable LabelHelper to centralize label parsing, validation, and add/remove operations for label-aware entities.
  • Introduce a handleNotFound helper to standardize 404 error handling and user messaging across multiple CLI commands.
  • Refactor resource, tool, namespace, datastore, forward, prompt, and toolset repo commands to use the new helpers, reducing duplicated logic and aligning exit codes.
  • Enhance batch resource removal commands to show a preview table and support non-interactive confirmation via an --assume-yes flag.
  • Simplify ToolsRemove and ResourcesRemove implementations and ensure consistent EXIT_OK/EXIT_ERROR semantics and logging behavior.

Tests:

  • Adjust label removal tests for tools and resources to reflect the new behavior that always updates entities when label operations are invoked.

Signed-off-by: Matheus André <matheusandr2@gmail.com>
@sourcery-ai
Copy link
Copy Markdown

sourcery-ai Bot commented May 23, 2026

Reviewer's Guide

Refactors CLI label add/remove and remove commands for resources, tools, datastores, and namespaces to share common label parsing, validation, mutation, and error handling utilities, reducing duplication and standardizing behavior across commands.

File-Level Changes

Change Details Files
Centralize label parsing, expression validation, and label mutation logic into reusable helpers used by all label add/remove commands.
  • Introduce LabelHelper with utilities for parsing label strings, validating mutual exclusivity of identifiers and label expressions, adding/removing labels on single entities, and applying label changes across entities returned from list APIs.
  • Refactor DataStoresLabelAdd/NamespacesLabelAdd/ResourcesLabelAdd/ToolsLabelAdd to use LabelHelper.validateLabelExpression, LabelHelper.parseLabels, LabelHelper.addLabelsToEntity, and LabelHelper.addLabelsByExpression.
  • Refactor DataStoresLabelRemove/NamespacesLabelRemove/ResourcesLabelRemove/ToolsLabelRemove to use LabelHelper.validateLabelExpression, LabelHelper.removeLabelsFromEntity, and LabelHelper.removeLabelsByExpression instead of custom per-command implementations.
  • Standardize printer messages for label operations (added/updated/removed/not-found) via LabelHelper, including slight changes to when updates are issued (always calling update even if no labels removed).
apps/wanaku-cli/src/main/java/ai/wanaku/cli/main/support/LabelHelper.java
apps/wanaku-cli/src/main/java/ai/wanaku/cli/main/commands/datastores/DataStoresLabelAdd.java
apps/wanaku-cli/src/main/java/ai/wanaku/cli/main/commands/namespaces/NamespacesLabelAdd.java
apps/wanaku-cli/src/main/java/ai/wanaku/cli/main/commands/resources/ResourcesLabelAdd.java
apps/wanaku-cli/src/main/java/ai/wanaku/cli/main/commands/tools/ToolsLabelAdd.java
apps/wanaku-cli/src/main/java/ai/wanaku/cli/main/commands/resources/ResourcesLabelRemove.java
apps/wanaku-cli/src/main/java/ai/wanaku/cli/main/commands/tools/ToolsLabelRemove.java
apps/wanaku-cli/src/main/java/ai/wanaku/cli/main/commands/datastores/DataStoresLabelRemove.java
apps/wanaku-cli/src/main/java/ai/wanaku/cli/main/commands/namespaces/NamespacesLabelRemove.java
Standardize handling of 404 and other WebApplicationException responses across commands using ResponseHelper.handleNotFound.
  • Add ResponseHelper.handleNotFound that prints a warning when a 404 is returned, otherwise delegates to commonResponseErrorHandler, and always returns EXIT_ERROR.
  • Replace ad-hoc try/catch and response status handling in label add/remove, delete, update, refresh, and remove commands (namespaces, resources, tools, toolset repos, prompts, forwards, datastores) with calls to handleNotFound.
  • Change some commands’ doCall signatures (e.g., ToolSetRepoRemove) to declare throws Exception to align with shared helper usage.
apps/wanaku-cli/src/main/java/ai/wanaku/cli/main/support/ResponseHelper.java
apps/wanaku-cli/src/main/java/ai/wanaku/cli/main/commands/datastores/DataStoresLabelAdd.java
apps/wanaku-cli/src/main/java/ai/wanaku/cli/main/commands/namespaces/NamespacesLabelAdd.java
apps/wanaku-cli/src/main/java/ai/wanaku/cli/main/commands/resources/ResourcesLabelAdd.java
apps/wanaku-cli/src/main/java/ai/wanaku/cli/main/commands/tools/ToolsLabelAdd.java
apps/wanaku-cli/src/main/java/ai/wanaku/cli/main/commands/resources/ResourcesLabelRemove.java
apps/wanaku-cli/src/main/java/ai/wanaku/cli/main/commands/tools/ToolsLabelRemove.java
apps/wanaku-cli/src/main/java/ai/wanaku/cli/main/commands/datastores/DataStoresLabelRemove.java
apps/wanaku-cli/src/main/java/ai/wanaku/cli/main/commands/namespaces/NamespacesLabelRemove.java
apps/wanaku-cli/src/main/java/ai/wanaku/cli/main/commands/resources/ResourcesRemove.java
apps/wanaku-cli/src/main/java/ai/wanaku/cli/main/commands/tools/ToolsRemove.java
apps/wanaku-cli/src/main/java/ai/wanaku/cli/main/commands/namespaces/NamespacesDelete.java
apps/wanaku-cli/src/main/java/ai/wanaku/cli/main/commands/toolset/ToolSetRepoRemove.java
apps/wanaku-cli/src/main/java/ai/wanaku/cli/main/commands/prompts/PromptsRemove.java
apps/wanaku-cli/src/main/java/ai/wanaku/cli/main/commands/forwards/ForwardsRefresh.java
apps/wanaku-cli/src/main/java/ai/wanaku/cli/main/commands/forwards/ForwardsRemove.java
apps/wanaku-cli/src/main/java/ai/wanaku/cli/main/commands/namespaces/NamespacesUpdate.java
apps/wanaku-cli/src/main/java/ai/wanaku/cli/main/commands/datastores/DataStoresRemove.java
Enhance resource and tool removal commands with consistent label-expression support and UX improvements.
  • Refactor ToolsRemove and ResourcesRemove to use LabelHelper.validateLabelExpression instead of inlined validation logic for name vs label-expression.
  • Update ToolsRemove.removeByLabelExpression and ResourcesRemove.removeByLabelExpression to return EXIT_OK explicitly and, for resources, show a preview table of matching resources, add an --assume-yes option, and prompt for confirmation using CommandHelper.confirm before batch deletion.
  • Minor Javadoc and formatting tweaks in ToolsRemove and ResourcesExpose (e.g., updated throws clause, doc comment normalization).
  • Simplify DataStoresRemove by using a shared identifier() helper and handleNotFound for 404 handling instead of duplicating identifier construction and logging logic.
apps/wanaku-cli/src/main/java/ai/wanaku/cli/main/commands/tools/ToolsRemove.java
apps/wanaku-cli/src/main/java/ai/wanaku/cli/main/commands/resources/ResourcesRemove.java
apps/wanaku-cli/src/main/java/ai/wanaku/cli/main/commands/resources/ResourcesExpose.java
apps/wanaku-cli/src/main/java/ai/wanaku/cli/main/commands/datastores/DataStoresRemove.java
Align tests with new label removal behavior that always updates the service even when labels are absent or null.
  • Update ToolsLabelRemoveTest and ResourcesLabelRemoveTest expectations to verify that update(...) is invoked instead of never() when labels are non-existent or null, aligning with LabelHelper.removeLabelsFromEntity semantics.
apps/wanaku-cli/src/test/java/ai/wanaku/cli/main/commands/tools/ToolsLabelRemoveTest.java
apps/wanaku-cli/src/test/java/ai/wanaku/cli/main/commands/resources/ResourcesLabelRemoveTest.java

Assessment against linked issues

Issue Objective Addressed Explanation
#1178 Centralize NOT_FOUND (404) error handling for CLI commands into a shared helper instead of duplicating WebApplicationException handling blocks in each command.
#1178 Extract and reuse label-expression-related logic (mutual exclusivity validation between identifier options and --label-expression, and parsing of label key=value pairs) into a shared helper for all relevant label/add/remove commands.
#1178 Refactor the batch label add/remove loops (success/failure counting and messaging for operations driven by label expressions) into reusable helper methods to remove duplication across tools, resources, datastores, and namespaces CLI commands.

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

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.

Reduce code duplication across CLI command classes

1 participant