Skip to content

refactor: adopt cobra's error handling and properly validate inputs - #280

Open
mynk8 wants to merge 3 commits into
microcks:masterfrom
mynk8:fix-cobra
Open

refactor: adopt cobra's error handling and properly validate inputs#280
mynk8 wants to merge 3 commits into
microcks:masterfrom
mynk8:fix-cobra

Conversation

@mynk8

@mynk8 mynk8 commented May 4, 2026

Copy link
Copy Markdown

Description

The codebase utilizes cobra but does not use it's error handling features like cobra's PreRunE, Args, and RunE hooks were never adopted, and defer statements were skipped on error paths and never surfaced due to os.Exit(0) (cannot repro this just a speculation).

This refactor improves CI reliability by routing command validation through Cobra's standard Args / PreRunE / RunE flow. Invalid CLI usage now consistently returns a non-zero exit code while preserving actionable usage output, which is important for GitHub Actions and other automated environments that rely on process status.

The changes fix the problems with input validation with proper integration of cobra in the codebase.

I have added small tests for sanity checks as well, would like some feedback on this PR :)

Changes:

  • All commands: Run converted to RunE correctly returning error
  • test: Args ExactArgs(3), PreRunE for runner and --waitFor validation
  • import: Args MinimumNArgs(1)
  • import-url: Args MinimumNArgs(1)
  • import-dir: Args ExactArgs(1)
  • login: Args ExactArgs(1), PreRunE for --sso-port range validation
  • logout: Args ExactArgs(1)
  • context: conditional arg validation in RunE
  • start: PreRunE for --port range validation
  • Root command: add SilenceErrors alongside existing SilenceUsage
  • 24 os.Exit(1) calls removed, replaced by error returns
  • log.Fatalf calls in start/stop replaced by fmt.Errorf returns
  • Port validation tests for --port and --sso-port

Bugs

Addresses #281

@mynk8
mynk8 requested review from Harsh4902, lbroudoux and yada as code owners May 4, 2026 16:21
@mynk8
mynk8 force-pushed the fix-cobra branch 4 times, most recently from e1f7d7e to 50b11c8 Compare May 5, 2026 02:39
…aths

Signed-off-by: mynk8 <mr.mayankgame@gmail.com>
@Caesarsage

Copy link
Copy Markdown
Contributor

Thank you @mynk8

Master now uses RunE throughout with a single exit funnel (#490/#491) and declarative validators (e.g. cobra.ExactArgs(3) on test). The os.Exit/log.Fatalf pattern this describes is gone.

I believe this can be closed.

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.

2 participants