Skip to content
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

convert to dual outputs: pager and stdout #1388

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

teepark
Copy link

@teepark teepark commented Dec 2, 2022

regular output (mainly query results) goes to the pager. status output goes directly to stdout.
both implement a consistent interface for easy mocking in tests.

Description

Picking up where #1383 left off.

We agreed on the need to separate normal query result output from status output. The previous PR used stdout and stderr, but we decided instead to use the pager's stdin and pgcli's stdout as the two streams.

Further, because there's a need to maintain order in the output lines even across the two streams, this PR inverts control and takes a DI approach, pushing "output emitters" down into the PGCli object. The previously pure "format_output" function is now "emit_output" which takes the injected emitters. For tests, we mock out the emitters with an implementation that collects the output lines in order for later assertions.

Checklist

  • I've added this contribution to the changelog.rst.
  • I've added my name to the AUTHORS file (or it's already there).
  • I installed pre-commit hooks (pip install pre-commit && pre-commit install), and ran black on my code.
  • Please squash merge this pull request (uncheck if you'd like us to merge as multiple commits)

Copy link
Contributor

@j-bennet j-bennet left a comment

Choose a reason for hiding this comment

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

I'm not completely happy with the current approach, however, I do like how the testability was ensured. Nice work there.

I left more comments in the output.py, see what you think.

pgcli/main.py Show resolved Hide resolved
pgcli/output.py Show resolved Hide resolved
regular output (mainly query results) goes to the pager.
status output goes directly to stdout.

Both implement a consistent interface for easy mocking in tests.
Stdout and Pager can just be the same thing "OutputHandler" that just
wraps a function which handles output, this way the CLI can create
instances for each by passing `click.echo` and `self.echo_via_pager` and
the output module avoids unnecessary knowledge/coupling.

Log is a little trickier to de-couple from CLI, because it can't just
wrap a file path (the only thing it knew about on CLI) since that gets
changed all the time mid-process. So it grows a method for updating it's
target path and CLI can use that instead of changing `self.output_file`.
@j-bennet
Copy link
Contributor

j-bennet commented Oct 6, 2023

@teepark Would you be able to resolve the conflicts?

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.

None yet

2 participants