mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
fix: show --help message for CLI errors, add tests for delete (#1403)
* feat(cli): add test for delete This adds a new test for the `delete` command to ensure it works as expected when provided the correct args. * fix(cli): use ExecuteC() to match Cobra This modifies the `cli.Root().Execute()` to `cli.Root).ExecuteC()` to match the default behavior of Cobra. We do this so errors will always print the "run --help" line. * feat(cli): add WithoutParameters test for delete This adds a new test to the `delete_test.go` suite to ensure the correct behavior occurs when `delete` is called without an argument. * fixup! feat(cli): add WithoutParameters test for delete * refactor(cli): show --help error message on main This adds an error message which shows when there is an error with any commands called to improve the UX. * fixup! refactor(cli): show --help error message on main * refactor(cli): handle err with FormatCobraError This adds a new helper function called `FormatCobraError` to `root.go` so that we can colorize and add "--help" message to cobra command errors like calling `delete`. * refactor(cli): add root_test.go, move delete test
This commit is contained in:
+3
-2
@@ -14,12 +14,13 @@ import (
|
||||
|
||||
func main() {
|
||||
dadjoke()
|
||||
err := cli.Root().Execute()
|
||||
cmd, err := cli.Root().ExecuteC()
|
||||
if err != nil {
|
||||
if errors.Is(err, cliui.Canceled) {
|
||||
os.Exit(1)
|
||||
}
|
||||
_, _ = fmt.Fprintln(os.Stderr, cliui.Styles.Error.Render(err.Error()))
|
||||
cobraErr := cli.FormatCobraError(err, cmd)
|
||||
_, _ = fmt.Fprintln(os.Stderr, cobraErr)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user