Skip to content

Don't error on ls commands with no entries found - #5479

Merged
mislav merged 8 commits into
cli:trunkfrom
rpadaki:nonzero-empty-ls
Apr 25, 2022
Merged

mislav merged 8 commits into
cli:trunkfrom
rpadaki:nonzero-empty-ls

Conversation

@rpadaki

@rpadaki rpadaki commented Apr 18, 2022

Copy link
Copy Markdown
Contributor

This PR is the bare minimum needed to solve the linked issue -- it removes any outstanding errors generated on empty list/ls commands, and changes associated tests to not expect errors. The commands changed here are:

  • gh gpg-key list
  • gh repo deploy-key list
  • gh ssh-key list

While looking through these files, I noticed inconsistencies with the printed output between the various list commands when no entries were found.

  • Some (like gh secret list) return silently
  • Some (like the ones I touch in this PR) print to stderr before returning
  • Others (like gh repo list) print to stdout, not stderr
  • And the remaining ones (such as gh workflow list) seem to return silently when one of {stdout,stdin} is not a TTY; else, they print to stderr

Are these different behaviors worth standardizing and reconciling? Or could such changes cause too many unexpected changes to scripts and developer workflows?

@rpadaki
rpadaki requested a review from a team as a code owner April 18, 2022 20:05
@rpadaki
rpadaki requested review from samcoe and removed request for a team April 18, 2022 20:05
@cliAutomation cliAutomation added the external pull request originating outside of the CLI core team label Apr 18, 2022
@samcoe samcoe self-assigned this Apr 20, 2022

@samcoe samcoe left a comment •

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.

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?

@mislav

mislav commented Apr 20, 2022

Copy link
Copy Markdown
Contributor
  • And the remaining ones (such as gh workflow list) seem to return silently when one of {stdout,stdin} is not a TTY; else, they print to stderr

This last case is the one I would expect. Strictly speaking in the "UNIX" sense, a list command with no results should just print nothing, but that's not very user-friendly for people who aren't familiar with the unix tool philosophy. So let's keep stdout empty and print an informational notice to stderr if the person is running the command connected to their terminal. @rpadaki Would you be willing to work those changes into your PR?

@rpadaki

rpadaki commented Apr 20, 2022

Copy link
Copy Markdown
Contributor Author

Yep! It might take a day or so but I'm happy to work those into this PR. Drafting until then

@rpadaki
rpadaki marked this pull request as draft April 20, 2022 15:30

@terapi09 terapi09 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Slm ben terapi09

@rpadaki

rpadaki commented Apr 21, 2022 •

Copy link
Copy Markdown
Contributor Author

Just noting that gh run list never actually returns 0 runs -- it gets an HTTP error 404 if there are no runs for a repo. Should I try and specifically detect this? How could we differentiate between the repo not existing and no runs existing for a real repo?

For now I'll just put in the no result handling even though it does nothing.

The exact same statement is true for gh secret list.

Edit: Okay I just don't know how to type my username. Disregard!

@rpadaki
rpadaki marked this pull request as ready for review April 21, 2022 05:40
@rpadaki
rpadaki requested a review from a team as a code owner April 21, 2022 05:40
@rpadaki

rpadaki commented Apr 21, 2022 •

Copy link
Copy Markdown
Contributor Author

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 HandleNoResults, which takes an IOStreams object and a message string, and returns an error. If the conditions are correct (stdin and stdout are TTYs), it will print that message, followed by a newline. Since we don't want to throw errors on no results found, we always return nil. But if we wanted to change that for whatever reason, it's literally one line of code to throw a No Results Found error, which could be nice for testing/debugging in the future.

But I also thought it was syntactically nice to return utils.HandleNoResults(...), as it's very clear that we're exiting early to do this, and the linter actually catches if this function is called without handling the return type!

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.

samcoe
samcoe previously requested changes Apr 21, 2022

@samcoe samcoe left a comment

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.

@rpadaki This is almost there! I left a handful of comments, I don't think anything major though. Let me know if you have any questions.

Comment thread utils/utils.go Resolved
Comment thread utils/utils.go Outdated
}

func HandleNoResults(ios *iostreams.IOStreams, msg string) error {
if ios.CanPrompt() {

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.

I think ios.IsStdoutTTY() && ios.IsStderrTTY() is more appropriate here. ios.CanPrompt() also checks stdin which I don't think is necessary.

@rpadaki rpadaki Apr 22, 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.

This makes sense. I sorta blindly followed what we were doing below, but there's no reason to keep doing that

terminal := opts.IO.IsStdoutTTY() && opts.IO.IsStdinTTY()
opts.PlainOutput = !terminal

@rpadaki rpadaki Apr 22, 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.

Actually I'm unconvinced -- if I run:

gh pr list

and 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.log

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

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.

Agreed with leaving it as just io.IsStdoutTTY().

Comment thread pkg/cmd/run/list/list.go Outdated
}

if len(runs) == 0 {
return utils.HandleNoResults(opts.IO, "No runs found")

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.

Despite comments, this works correctly when a repo has no runs. Additionally, we should delete lines 129-134.

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.

Are there any other places with no longer necessary empty list handling?

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 think I hit most of it, but I'll make another pass through

@rpadaki rpadaki Apr 22, 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.

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.

@samcoe samcoe Apr 25, 2022 •

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.

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?

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.

not at all, i think i have that diff lying around already

Comment thread pkg/cmd/secret/list/list.go Resolved
Comment thread pkg/cmd/config/list/list.go Resolved
Comment thread pkg/cmd/issue/list/list.go Outdated
Comment on lines +177 to +179
if len(listResult.Issues) == 0 {
return prShared.ListNoResults(opts.IO, ghrepo.FullName(baseRepo), "issue", !filterOptions.IsDefault())
}

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.

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.

@rpadaki rpadaki Apr 22, 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.

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?

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.

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.

Comment thread pkg/cmd/pr/list/list.go Resolved
Comment thread pkg/cmd/search/shared/shared.go Resolved
@Griffin641498

This comment was marked as spam.

@rpadaki
rpadaki requested a review from samcoe April 22, 2022 04:41

@mislav mislav left a comment

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.

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.

@mislav

mislav commented Apr 25, 2022

Copy link
Copy Markdown
Contributor

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/--template export for empty sets will be broken.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

external pull request originating outside of the CLI core team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Do not return an error when list commands return an empty list

6 participants