Commit ca850fa6 authored by Jérome Perrin's avatar Jérome Perrin Committed by Rafael Monnerat

proxy show: uses $PAGER to paginate output

parent 530f422b
...@@ -32,6 +32,9 @@ import hashlib ...@@ -32,6 +32,9 @@ import hashlib
import json import json
import logging import logging
import sys import sys
import os
import subprocess
import StringIO
import lxml.etree import lxml.etree
import prettytable import prettytable
...@@ -201,9 +204,13 @@ def log_network(logger, conn): ...@@ -201,9 +204,13 @@ def log_network(logger, conn):
def do_show(conf): def do_show(conf):
# Because this command uses logging facility to output, # Because this command uses logging facility to output,
# setup a logger which displays on stdout. # setup a logger which displays on stdout and uses a pager
# to paginate input, honoring $PAGER.
output = sys.stdout
if output.isatty():
output = StringIO.StringIO()
proxy_show_logger = logging.getLogger(__name__) proxy_show_logger = logging.getLogger(__name__)
handler = logging.StreamHandler(sys.stdout) handler = logging.StreamHandler(output)
handler.setLevel(logging.DEBUG) handler.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(message)s') formatter = logging.Formatter('%(message)s')
handler.setFormatter(formatter) handler.setFormatter(formatter)
...@@ -234,3 +241,11 @@ def do_show(conf): ...@@ -234,3 +241,11 @@ def do_show(conf):
func(proxy_show_logger, conn) func(proxy_show_logger, conn)
if idx < len(to_call) - 1: if idx < len(to_call) - 1:
proxy_show_logger.info(' ') proxy_show_logger.info(' ')
if sys.stdout.isatty():
pager = subprocess.Popen(
os.getenv('PAGER', 'less --quit-if-one-screen --no-init'),
close_fds=True,
shell=True,
stdin=subprocess.PIPE,)
pager.communicate(output.getvalue().encode('utf-8'))
...@@ -166,6 +166,29 @@ class TestCliProxyShow(CliMixin): ...@@ -166,6 +166,29 @@ class TestCliProxyShow(CliMixin):
self.assertIn('287375f0cba269902ba1bc50242839d7', stdout.getvalue()) self.assertIn('287375f0cba269902ba1bc50242839d7', stdout.getvalue())
self.assertEqual('', stderr.getvalue()) self.assertEqual('', stderr.getvalue())
def test_proxy_show_use_pager(self):
saved_stderr = sys.stderr
saved_stdout = sys.stdout
sys.stderr = stderr = StringIO.StringIO()
sys.stdout = stdout = StringIO.StringIO()
stdout.isatty = lambda *args: True
# use a pager that just output to a file.
tmp = tempfile.NamedTemporaryFile(delete=False)
self.addCleanup(os.unlink, tmp.name)
os.environ['PAGER'] = 'cat > {}'.format(tmp.name)
try:
slapos.cli.proxy_show.do_show(self.conf)
finally:
sys.stderr = saved_stderr
sys.stdout = saved_stdout
self.assertEqual('', stdout.getvalue())
self.assertEqual('', stderr.getvalue())
# our pager was set to output to this temporary file
self.assertIn('287375f0cba269902ba1bc50242839d7', open(tmp.name, 'r').read())
class TestCliNode(CliMixin): class TestCliNode(CliMixin):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment