Skip to content

refactor(cli): collapse provider-null check into Providers.require helper#169

Merged
rlaope merged 1 commit into
masterfrom
refactor/provider-require-helper
May 9, 2026
Merged

refactor(cli): collapse provider-null check into Providers.require helper#169
rlaope merged 1 commit into
masterfrom
refactor/provider-require-helper

Conversation

@rlaope
Copy link
Copy Markdown
Owner

@rlaope rlaope commented May 9, 2026

Summary

Every CLI command that resolves a typed provider repeated the same 5-line block:

GcProvider provider = registry.findGcProvider(pid, sourceOverride);
if (provider == null) {
    System.err.println(messages.get("error.provider.none", pid));
    return;
}

The block returned with exit 0, so missing-provider runs silently looked like success to scripts piping argus output.

This PR introduces a single Providers.require(provider, pid, messages) static helper in the command package. It prints the localized error and throws CommandExitException(1), which ArgusCli already converts to a non-zero exit. 32 call sites across 31 command files shrink from 5 lines to 1, and missing-provider runs now correctly exit 1.

  • New file: argus-cli/src/main/java/io/argus/cli/command/Providers.java (~30 LOC, single static helper)
  • 31 commands migrated to Providers.require(...)
  • Net change: βˆ’95 lines from command files (after counting the +30 new helper and +41 unit test, total diff is +103 / βˆ’127)
  • Unit test: ProvidersTest covers both branches (non-null pass-through, null β†’ throws CommandExitException(1) + writes to stderr)

Out of scope

Sites that don't fit the helper shape are left alone:

  • ClassLeak/CompilerQueue/Events/GcRunCommand β€” check JcmdExecutor.isJcmdAvailable() directly, not a typed provider.
  • ProfileCommand's parallel fan-out callable wraps the message into a ProfileResult.error rather than printing/exiting.
  • PsCommand passes "ps" instead of a pid.

Behaviour change

CLI commands that previously printed error.provider.none to stderr and exited 0 now exit 1. This is the correct behaviour for shell pipelines and CI gates β€” silent exit-0 on a missing provider was a latent bug.

Test plan

  • :argus-cli:test passes (incl. new ProvidersTest)
  • Full ./gradlew build (excluding integrationTest) passes
  • Smoke: argus gc 999999 exits 1 with stderr message
  • Verify on user side that no downstream script depended on the old exit-0 behaviour

…lper

Every CLI command that needed a typed provider repeated the same 5-line
block: lookup β†’ null check β†’ print error.provider.none β†’ return. Each
copy returned with exit 0, so a missing provider silently looked like
success to scripts piping argus output.

Introduce a single Providers.require(provider, pid, messages) static
helper in the command package. It prints the localized error and throws
CommandExitException(1), which ArgusCli already converts to a non-zero
exit. 32 call sites across 31 command files shrink from 5 lines to 1,
and missing-provider runs now correctly exit 1.

Sites that don't fit the shape are left alone:
- ClassLeak/CompilerQueue/Events/GcRunCommand check JcmdExecutor
  availability directly, not a typed provider.
- ProfileCommand's parallel fan-out callable wraps the message into a
  ProfileResult.error rather than printing.
- PsCommand uses "ps" instead of a pid.

Behaviour change: CLI commands that previously printed the missing-
provider message and exited 0 now exit 1.

Signed-off-by: rlaope <piyrw9754@gmail.com>
@rlaope rlaope merged commit e3544aa into master May 9, 2026
11 checks passed
@rlaope rlaope deleted the refactor/provider-require-helper branch May 9, 2026 03:39
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