microcks / microcks/microcks-cli
Refactor CLI error handling to idiomatic Cobra RunE pattern for improved testability and maintainability
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 52
- Fork
- 68
- Merge medio
- 6h 54m
- PR unite (30g)
- 10
Descrizione
### 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
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia individuando il comando Cobra principale e le definizioni dei comandi stop, version e context che attualmente usano Run e errors.CheckError(). Leggi come vengono attualmente stampati gli errori e come vengono testati i comandi, quindi migra un comando a basso rischio preservando il suo output visibile all’utente. Il lavoro è completato quando gli errori si propagano tramite RunE, la gestione a livello root evita la stampa duplicata e il comando migrato dispone di un unit test.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- go
- Ambito
- cli, testing
- Tipo di issue
- Refactoring
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Tranquilla
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 48/100