Skip to content

fix: show --help message for CLI errors, add tests for delete #1403

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
May 19, 2022

Conversation

jsjoeio
Copy link
Contributor

@jsjoeio jsjoeio commented May 11, 2022

This PR shows a more helpful error message when calling coder delete.

Subtasks

  • added tests

Fixes #1233

H/T to @mafredri for pointers & suggestions 🎉

First time actually working in Go so please give any and all suggestions

@codecov
Copy link

codecov bot commented May 11, 2022

Codecov Report

Merging #1403 (8a5fc02) into main (f4da5d4) will increase coverage by 0.09%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##             main    #1403      +/-   ##
==========================================
+ Coverage   67.06%   67.15%   +0.09%     
==========================================
  Files         288      292       +4     
  Lines       19079    19533     +454     
  Branches      241      258      +17     
==========================================
+ Hits        12795    13118     +323     
- Misses       4978     5063      +85     
- Partials     1306     1352      +46     
Flag Coverage Δ
unittest-go-macos-latest 54.24% <100.00%> (+0.03%) ⬆️
unittest-go-postgres- 65.72% <100.00%> (+0.04%) ⬆️
unittest-go-ubuntu-latest 56.59% <100.00%> (+0.10%) ⬆️
unittest-go-windows-2022 52.68% <100.00%> (+0.05%) ⬆️
unittest-js 74.73% <ø> (+0.49%) ⬆️
Impacted Files Coverage Δ
cli/cliarg/cliarg.go 100.00% <100.00%> (ø)
cli/delete.go 57.14% <100.00%> (+32.14%) ⬆️
coderd/roles.go 68.96% <0.00%> (-31.04%) ⬇️
codersdk/roles.go 63.63% <0.00%> (-9.10%) ⬇️
cli/autostart.go 66.37% <0.00%> (-8.92%) ⬇️
cli/autostop.go 66.37% <0.00%> (-8.63%) ⬇️
coderd/httpapi/httpapi.go 71.25% <0.00%> (-6.25%) ⬇️
site/src/components/Section/Section.tsx 61.11% <0.00%> (-5.56%) ⬇️
site/src/xServices/auth/authXService.ts 78.12% <0.00%> (-4.49%) ⬇️
cli/ssh.go 39.53% <0.00%> (-1.24%) ⬇️
... and 47 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update f4da5d4...8a5fc02. Read the comment docs.

Copy link
Member

@mafredri mafredri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added some thoughts, in case they're helpful. I love that you're looking to fix this, it's not the best experience to have to dig into --help every time you've done something "wrong".

Perhaps an even better option than custom argument processing would be to automatically show the command help on error?

accepts 1 arg(s), received 0

Delete a workspace

Flags:
  -h, --help   help for delete

Use "coder delete [command] --help" for more information about a command.

The only problem I noticed here is that the --help is not helpful at all! The Use: description is not visible so it's impossible to know what the argument is 🤔.

@jsjoeio jsjoeio force-pushed the 1233-bug-ux-coder-workspaces-delete-error-message branch from d09bb5e to ba2f9b7 Compare May 12, 2022 22:20
@jsjoeio jsjoeio self-assigned this May 12, 2022
@jsjoeio jsjoeio requested review from a team and mafredri May 12, 2022 22:36
@jsjoeio jsjoeio marked this pull request as ready for review May 12, 2022 22:37
Copy link
Member

@mafredri mafredri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed a limitation of our custom template for command help that may have contributed to part of the usability problem with the CLI. I think we should get that fixed first to better see what's missing.

@jsjoeio
Copy link
Contributor Author

jsjoeio commented May 13, 2022

I noticed a limitation of our custom template for command help that may have contributed to part of the usability problem with the CLI. I think we should get that fixed first to better see what's missing.

Nice find! Even if we fix that, I believe this original issue will still exist. Try running gh pr close and you'll see they have the same issue.

What if we:

  • address changes in this PR and merge
  • tackle the template issue in a separate PR
    ?

@greyscaled greyscaled changed the title fix(cli): show error message for [delete] without parameter fix: show error message for [delete] without parameter May 14, 2022
mafredri added a commit that referenced this pull request May 16, 2022
@mafredri
Copy link
Member

Good idea to split into separate PR @jsjoeio, I created the PR for fixing the --help output in #1463.

mafredri added a commit that referenced this pull request May 16, 2022
@misskniss misskniss added this to the Community MVP milestone May 16, 2022
@misskniss misskniss removed the V2 BETA label May 16, 2022
@jsjoeio jsjoeio marked this pull request as draft May 19, 2022 16:06
This adds a new test for the `delete` command to ensure it works as
expected when provided the correct args.
@jsjoeio jsjoeio force-pushed the 1233-bug-ux-coder-workspaces-delete-error-message branch from 8a5fc02 to 758dceb Compare May 19, 2022 16:13
jsjoeio added 2 commits May 19, 2022 16:23
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.
This adds a new test to the `delete_test.go` suite to ensure the correct
behavior occurs when `delete` is called without an argument.
jsjoeio added 2 commits May 19, 2022 18:25
This adds an error message which shows when there is an error with any
commands called to improve the UX.
Comment on lines 22 to 23
helpErrMsg := fmt.Sprintf("Run '%s %s --help' for usage.", cmd.Root().Name(), cmd.Name())
_, _ = fmt.Fprintln(os.Stderr, cliui.Styles.Error.Render(err.Error()+"/n"+helpErrMsg))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most errors are not usage errors and shouldn't show the usage. Stuff like HTTP errors would trigger this for example

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True! @f0ssel mentioned the same thing. Maybe that's why SilenceErrors is set to true?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way we can detect a cobra error like errors.Is(err, cobra.ErrArgs) or something?

Copy link
Contributor Author

@jsjoeio jsjoeio May 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I looked around the docs but didn't find anything. Seems like it just returns a regular error. Maybe someone sees something though.

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`.
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)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: this behavior mimics what SilenceErrors: true does, which prints the --help message below any errors. See this comment from @mafredri for more context: #1403 (comment)

@@ -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 {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extracted into function to make it easier to test

@jsjoeio jsjoeio marked this pull request as ready for review May 19, 2022 19:16
@jsjoeio jsjoeio changed the title fix: show error message for [delete] without parameter fix: show --help message for CLI errors, add tests for delete May 19, 2022
pty.ExpectMatch("Cleaning Up")
<-doneChan
})
t.Run("WithoutParameters/FormatCobraError", func(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: nothing wrong with this test case that I can see, but since it's just testing the FormatCobraError function and doesn't have anything to do with the functionality of coder delete, it seems a bit odd for it to live in this file. How about creating root_test.go and putting it there?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point! @f0ssel and I were discussing that as well and we had landed on leaving here but happy to move it!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@jsjoeio jsjoeio enabled auto-merge (squash) May 19, 2022 22:25
@jsjoeio jsjoeio merged commit 6dae48a into main May 19, 2022
@jsjoeio jsjoeio deleted the 1233-bug-ux-coder-workspaces-delete-error-message branch May 19, 2022 22:36
kylecarbs pushed a commit that referenced this pull request Jun 10, 2022
* 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
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.

Bug (UX): ./coder workspaces delete -> error message
7 participants