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:
Joe Previte
2022-05-19 22:35:59 +00:00
committed by GitHub
parent a64ab6538e
commit 6dae48a1a8
4 changed files with 70 additions and 2 deletions
+7
View File
@@ -1,6 +1,7 @@
package cli
import (
"fmt"
"net/url"
"os"
"time"
@@ -259,3 +260,9 @@ func versionTemplate() string {
template += "\r\n"
return template
}
// FormatCobraError colorizes and adds "--help" docs to cobra commands.
func FormatCobraError(err error, cmd *cobra.Command) string {
helpErrMsg := fmt.Sprintf("Run '%s %s --help' for usage.", cmd.Root().Name(), cmd.Name())
return cliui.Styles.Error.Render(err.Error() + "\n" + helpErrMsg)
}