microcks / microcks/microcks-cli

Refactor CLI error handling to idiomatic Cobra RunE pattern for improved testability and maintainability

Offen
#322 1 Kommentar 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Vorherrschende Sprache
Go
Sterne
52
Forks
68
Ø Merge
6 Std. 54 Min.
Gemergte PRs (30 T.)
10

Beschreibung

### Reason/Context

The current CLI implementation relies heavily on `errors.CheckError()` and `Run` handlers, which causes abrupt termination of commands and reduces testability.

This pattern makes it difficult to:
- properly propagate errors through Cobra command chain
- write unit tests for command execution
- ensure graceful cleanup of resources
- maintain consistent error formatting across commands

Modern Go CLI tools (such as kubectl, docker CLI) prefer using `RunE` and returning errors up the call stack for centralized handling.

This improvement is motivated by the need to align the CLI with idiomatic Go practices and improve long-term maintainability and developer experience.

### Description

This enhancement proposes a gradual refactor of the CLI command execution model from `Run` + `errors.CheckError()` to Cobra's `RunE` pattern.

Key changes include:
- Replace `Run` functions with `RunE` in command definitions
- Remove direct calls to `errors.CheckError()`
- Return errors using `fmt.Errorf("%w")` for proper wrapping
- Centralize error handling at the root command level
- Ensure `SilenceErrors` is enabled to avoid duplicate error printing
- Preserve existing CLI behavior and user-facing output

This is NOT a breaking change if done incrementally, as each command will be migrated independently.

Expected benefits:
- Improved testability of CLI commands
- Cleaner error propagation model
- Better alignment with idiomatic Go CLI design
- Safer resource cleanup and execution flow
- Easier future extension of CLI features

No user-facing breaking changes are expected if migration is done incrementally.

### Implementation ideas

- Start migration with low-risk commands (e.g., stop, version, context)
- Convert command definitions from `Run` → `RunE`
- Replace `errors.CheckError()` with error returns
- Use `fmt.Errorf("%w")` for error wrapping
- Add centralized error handler in root command
- Add `SilenceErrors: true` in root Cobra command
- Introduce minimal unit tests for migrated commands using RunE
- Validate CLI behavior remains identical after each migration step
- Avoid large-scale refactor; follow incremental PR strategy per command

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne damit, den Root-Cobra-Befehl sowie die Definitionen der stop-, version- und context-Befehle zu lokalisieren, die derzeit Run und errors.CheckError() verwenden. Lies, wie Fehler derzeit ausgegeben werden und wie Befehle getestet werden, und migriere dann einen risikoarmen Befehl unter Beibehaltung seiner für Benutzer sichtbaren Ausgabe. Erledigt ist die Aufgabe, wenn Fehler über RunE weitergereicht werden, die Behandlung auf Root-Ebene doppeltes Ausgeben vermeidet und der migrierte Befehl einen Unit-Test hat.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
go
Bereich
cli, testing
Issue-Typ
Refactoring
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Ruhig
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
48/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.