Skip to content

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

Merged
jsjoeio merged 8 commits into
mainfrom
1233-bug-ux-coder-workspaces-delete-error-message
May 19, 2022
Merged

jsjoeio merged 8 commits into
mainfrom
1233-bug-ux-coder-workspaces-delete-error-message

Conversation

@jsjoeio

@jsjoeio jsjoeio commented May 11, 2022 •

Copy link
Copy Markdown
Contributor

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

codecov Bot commented May 11, 2022 •

Copy link
Copy Markdown

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.

@mafredri mafredri left a comment

Copy link
Copy Markdown
Member

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 🤔.

Comment thread cli/delete.go Resolved
Comment thread cli/delete.go Resolved
@jsjoeio
jsjoeio force-pushed the 1233-bug-ux-coder-workspaces-delete-error-message branch from d09bb5e to ba2f9b7 Compare May 12, 2022 22:20
Comment thread cli/delete.go Resolved
Comment thread cli/delete.go Resolved
Comment thread cli/cliarg/cliflag_test.go Resolved
Comment thread cli/cliarg/cliflag_test.go Resolved
Comment thread cli/delete_test.go Resolved
@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
Comment thread cli/cliarg/cliflag_test.go Resolved
Comment thread cli/cliarg/cliflag_test.go Resolved
Comment thread cli/delete_test.go Resolved
Comment thread cli/cliarg/cliarg.go Resolved

@mafredri mafredri left a comment

Copy link
Copy Markdown
Member

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.

Comment thread cli/delete.go Resolved
Comment thread cli/delete.go Resolved
@jsjoeio

jsjoeio commented May 13, 2022

Copy link
Copy Markdown
Contributor Author

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
Copy Markdown
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.
Comment thread cli/delete_test.go Resolved
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 thread cmd/coder/main.go Outdated
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
Copy Markdown
Collaborator

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
Copy Markdown
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
Copy Markdown
Member

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?

@jsjoeio jsjoeio May 19, 2022 •

Copy link
Copy Markdown
Contributor Author

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`.
Comment thread cmd/coder/main.go
os.Exit(1)
}
_, _ = fmt.Fprintln(os.Stderr, cliui.Styles.Error.Render(err.Error()))
cobraErr := cli.FormatCobraError(err, cmd)

Copy link
Copy Markdown
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)

Comment thread cli/root.go
}

// FormatCobraError colorizes and adds "--help" docs to cobra commands.
func FormatCobraError(err error, cmd *cobra.Command) string {

Copy link
Copy Markdown
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
Comment thread cli/delete_test.go Outdated
pty.ExpectMatch("Cleaning Up")
<-doneChan
})
t.Run("WithoutParameters/FormatCobraError", func(t *testing.T) {

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
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