Repository navigation
Don't error on ls commands with no entries found - #5479
Conversation
There was a problem hiding this comment.
These changes look good to me.
I would definitely like to see the different behaviors be standardized. I think the correct behavior is on an empty list return a success exit code and output to stderr, regardless of tty status, that there were no results and output nothing on stdout. I don't see this causing any backwards compatibility issues except for scripts that are parsing stdout and explicitly searching for no result strings. Any work on standardization should also include the search commands as they return results in a list format. @mislav what are your thoughts?
This last case is the one I would expect. Strictly speaking in the "UNIX" sense, a |
|
Yep! It might take a day or so but I'm happy to work those into this PR. Drafting until then |
60cb2e2 to
1ed0e61
Compare
|
Just noting that For now I'll just put in the no result handling even though it does nothing. The exact same statement is true for Edit: Okay I just don't know how to type my username. Disregard! |
85058f0 to
a54d5b5
Compare
|
Whelp, looks like I broke the pr automation CI by drafting and undrafting xD. But it fixed after I pushed again. In this implementation, I added a utility function But I also thought it was syntactically nice to The exit messages I chose to print were either adapted from existing messages or made up by me. Let me know if you'd like to see more descriptive error messages for some of these! Also let me know if you think the utility function should go somewhere else. I'm pretty new to this repo, so I kinda went with something that made sense to me at the moment. |
| } | ||
|
|
||
| func HandleNoResults(ios *iostreams.IOStreams, msg string) error { | ||
| if ios.CanPrompt() { |
There was a problem hiding this comment.
I think ios.IsStdoutTTY() && ios.IsStderrTTY() is more appropriate here. ios.CanPrompt() also checks stdin which I don't think is necessary.
There was a problem hiding this comment.
This makes sense. I sorta blindly followed what we were doing below, but there's no reason to keep doing that
cli/pkg/cmd/workflow/list/list.go
Lines 45 to 46 in 7678274
There was a problem hiding this comment.
Actually I'm unconvinced -- if I run:
gh pr listand see an stderr message "There are no open pull requests in user/repo", then part of me would expect to be able to do:
gh pr list 2> err.logand properly see the same stderr message. Maybe we should only be checking io.IsStdoutTTY()?
I'll push this approach in my update. Should be one line (and a couple of changes to unit tests) to change either way.
There was a problem hiding this comment.
Agreed with leaving it as just io.IsStdoutTTY().
| } | ||
|
|
||
| if len(runs) == 0 { | ||
| return utils.HandleNoResults(opts.IO, "No runs found") |
There was a problem hiding this comment.
Despite comments, this works correctly when a repo has no runs. Additionally, we should delete lines 129-134.
There was a problem hiding this comment.
Are there any other places with no longer necessary empty list handling?
There was a problem hiding this comment.
I think I hit most of it, but I'll make another pass through
There was a problem hiding this comment.
Ooh, gh pr checks is a really annoying case. Cause the null check is too deep, so it'd require some refactoring.
One option would be to turn NoResultsError into an actual error which bundles a message string, and then suppress the error and return ExitOK in main.go, by adding to the block:
if cmd, err := rootCmd.ExecuteC(); err != nil {
var pagerPipeError *iostreams.ErrClosedPagerPipe
if err == cmdutil.SilentError {
return exitError
} else if cmdutil.IsUserCancellation(err) {
if errors.Is(err, terminal.InterruptErr) {
// ensure the next shell prompt will start on its own line
fmt.Fprint(stderr, "\n")
}
return exitCancel
} else if errors.Is(err, authError) {
return exitAuth
} else if errors.As(err, &pagerPipeError) {
// ignore the error raised when piping to a closed pager
return exitOK
} else if errors.As(err, &noResultsError) {
// print the error message to stderr if cmdFactory.IOStreams.IsStdoutTTY(), else silence
return exitOK
}But maybe this belongs in a follow-up PR.
There was a problem hiding this comment.
I actually much prefer this approach of capturing the NoResultsError here in the main block and then returning exitOK. I think it is cleaner and allows the use of cmdutil.NoResultError without having to provide iostreams.IOStreams. Would it be asking to much to refactor this PR to use this new approach?
There was a problem hiding this comment.
not at all, i think i have that diff lying around already
| if len(listResult.Issues) == 0 { | ||
| return prShared.ListNoResults(opts.IO, ghrepo.FullName(baseRepo), "issue", !filterOptions.IsDefault()) | ||
| } |
There was a problem hiding this comment.
While this pertains to listing empty result sets I would like to see this change and the one to pr list extracted into a separate PR as it doesn't use HandleNoResults and I would like to keep this one more focussed since it has grown in size.
There was a problem hiding this comment.
The function I added shared.ListNoResults does use HandleNoResults (now renamed to NoResultsError) in its implementation. I just kept them in a shared component to keep the logic for which error message to display factored out.
Happy to revert those changes if you'd like?
There was a problem hiding this comment.
That's a good point. I would say that shared.ListHeader should remove the no results found logic then since we now have a dedicated function for that.
This comment was marked as spam.
This comment was marked as spam.
mislav
left a comment
There was a problem hiding this comment.
Thank you for the comprehensive PR! I like the approach with calling a single unified error helper for all of these cases, but when I looked at the implementation of NoResultsError() error, I was surprised to learn that it always returns nil, which was a bit counter-intuitive.
I've pushed a change to this PR that returns a new error type for "no results" mode and handles that new type accordingly in our main package based on whether or not stdout/stderr are connected to a terminal. I feel like this has the same behavior but with a bit more Go-like error handling mechanism.
|
I've also pushed a fix for broken JSON export functionality: under no circumstances should NoResultsError be thrown before the Exporter had a chance to do its job, otherwise the JSON/ |
This PR is the bare minimum needed to solve the linked issue -- it removes any outstanding errors generated on empty
list/lscommands, and changes associated tests to not expect errors. The commands changed here are:gh gpg-key listgh repo deploy-key listgh ssh-key listWhile looking through these files, I noticed inconsistencies with the printed output between the various
listcommands when no entries were found.gh secret list) return silentlystderrbefore returninggh repo list) print tostdout, notstderrgh workflow list) seem to return silently when one of{stdout,stdin}is not a TTY; else, they print tostderrAre these different behaviors worth standardizing and reconciling? Or could such changes cause too many unexpected changes to scripts and developer workflows?