mirror of
https://github.com/docker/compose.git
synced 2025-07-20 12:14:41 +02:00
Merge pull request #384 from timfreund/master
Enable monochrome output in the 'up' and 'logs' commands
This commit is contained in:
commit
f2bf7f9e0d
@ -10,11 +10,11 @@ from .utils import split_buffer
|
|||||||
|
|
||||||
|
|
||||||
class LogPrinter(object):
|
class LogPrinter(object):
|
||||||
def __init__(self, containers, attach_params=None, output=sys.stdout):
|
def __init__(self, containers, attach_params=None, output=sys.stdout, monochrome=False):
|
||||||
self.containers = containers
|
self.containers = containers
|
||||||
self.attach_params = attach_params or {}
|
self.attach_params = attach_params or {}
|
||||||
self.prefix_width = self._calculate_prefix_width(containers)
|
self.prefix_width = self._calculate_prefix_width(containers)
|
||||||
self.generators = self._make_log_generators()
|
self.generators = self._make_log_generators(monochrome)
|
||||||
self.output = output
|
self.output = output
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
@ -35,12 +35,15 @@ class LogPrinter(object):
|
|||||||
prefix_width = max(prefix_width, len(container.name_without_project))
|
prefix_width = max(prefix_width, len(container.name_without_project))
|
||||||
return prefix_width
|
return prefix_width
|
||||||
|
|
||||||
def _make_log_generators(self):
|
def _make_log_generators(self, monochrome):
|
||||||
color_fns = cycle(colors.rainbow())
|
color_fns = cycle(colors.rainbow())
|
||||||
generators = []
|
generators = []
|
||||||
|
|
||||||
for container in self.containers:
|
for container in self.containers:
|
||||||
color_fn = color_fns.next()
|
if monochrome:
|
||||||
|
color_fn = lambda s: s
|
||||||
|
else:
|
||||||
|
color_fn = color_fns.next()
|
||||||
generators.append(self._make_log_generator(container, color_fn))
|
generators.append(self._make_log_generator(container, color_fn))
|
||||||
|
|
||||||
return generators
|
return generators
|
||||||
|
@ -137,11 +137,17 @@ class TopLevelCommand(Command):
|
|||||||
"""
|
"""
|
||||||
View output from containers.
|
View output from containers.
|
||||||
|
|
||||||
Usage: logs [SERVICE...]
|
Usage: logs [options] [SERVICE...]
|
||||||
|
|
||||||
|
Options:
|
||||||
|
--no-color Produce monochrome output.
|
||||||
"""
|
"""
|
||||||
containers = self.project.containers(service_names=options['SERVICE'], stopped=True)
|
containers = self.project.containers(service_names=options['SERVICE'], stopped=True)
|
||||||
|
|
||||||
|
monochrome = options['--no-color']
|
||||||
|
|
||||||
print("Attaching to", list_containers(containers))
|
print("Attaching to", list_containers(containers))
|
||||||
LogPrinter(containers, attach_params={'logs': True}).run()
|
LogPrinter(containers, attach_params={'logs': True}, monochrome=monochrome).run()
|
||||||
|
|
||||||
def ps(self, options):
|
def ps(self, options):
|
||||||
"""
|
"""
|
||||||
@ -325,11 +331,14 @@ class TopLevelCommand(Command):
|
|||||||
Options:
|
Options:
|
||||||
-d Detached mode: Run containers in the background,
|
-d Detached mode: Run containers in the background,
|
||||||
print new container names.
|
print new container names.
|
||||||
|
--no-color Produce monochrome output.
|
||||||
--no-deps Don't start linked services.
|
--no-deps Don't start linked services.
|
||||||
--no-recreate If containers already exist, don't recreate them.
|
--no-recreate If containers already exist, don't recreate them.
|
||||||
"""
|
"""
|
||||||
detached = options['-d']
|
detached = options['-d']
|
||||||
|
|
||||||
|
monochrome = options['--no-color']
|
||||||
|
|
||||||
start_links = not options['--no-deps']
|
start_links = not options['--no-deps']
|
||||||
recreate = not options['--no-recreate']
|
recreate = not options['--no-recreate']
|
||||||
service_names = options['SERVICE']
|
service_names = options['SERVICE']
|
||||||
@ -344,7 +353,7 @@ class TopLevelCommand(Command):
|
|||||||
|
|
||||||
if not detached:
|
if not detached:
|
||||||
print("Attaching to", list_containers(to_attach))
|
print("Attaching to", list_containers(to_attach))
|
||||||
log_printer = LogPrinter(to_attach, attach_params={"logs": True})
|
log_printer = LogPrinter(to_attach, attach_params={"logs": True}, monochrome=monochrome)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
log_printer.run()
|
log_printer.run()
|
||||||
|
@ -7,16 +7,28 @@ from .. import unittest
|
|||||||
|
|
||||||
|
|
||||||
class LogPrinterTest(unittest.TestCase):
|
class LogPrinterTest(unittest.TestCase):
|
||||||
def test_single_container(self):
|
def get_default_output(self, monochrome=False):
|
||||||
def reader(*args, **kwargs):
|
def reader(*args, **kwargs):
|
||||||
yield "hello\nworld"
|
yield "hello\nworld"
|
||||||
|
|
||||||
container = MockContainer(reader)
|
container = MockContainer(reader)
|
||||||
output = run_log_printer([container])
|
output = run_log_printer([container], monochrome=monochrome)
|
||||||
|
return output
|
||||||
|
|
||||||
|
def test_single_container(self):
|
||||||
|
output = self.get_default_output()
|
||||||
|
|
||||||
self.assertIn('hello', output)
|
self.assertIn('hello', output)
|
||||||
self.assertIn('world', output)
|
self.assertIn('world', output)
|
||||||
|
|
||||||
|
def test_monochrome(self):
|
||||||
|
output = self.get_default_output(monochrome=True)
|
||||||
|
self.assertNotIn('\033[', output)
|
||||||
|
|
||||||
|
def test_polychrome(self):
|
||||||
|
output = self.get_default_output()
|
||||||
|
self.assertIn('\033[', output)
|
||||||
|
|
||||||
def test_unicode(self):
|
def test_unicode(self):
|
||||||
glyph = u'\u2022'.encode('utf-8')
|
glyph = u'\u2022'.encode('utf-8')
|
||||||
|
|
||||||
@ -29,10 +41,10 @@ class LogPrinterTest(unittest.TestCase):
|
|||||||
self.assertIn(glyph, output)
|
self.assertIn(glyph, output)
|
||||||
|
|
||||||
|
|
||||||
def run_log_printer(containers):
|
def run_log_printer(containers, monochrome=False):
|
||||||
r, w = os.pipe()
|
r, w = os.pipe()
|
||||||
reader, writer = os.fdopen(r, 'r'), os.fdopen(w, 'w')
|
reader, writer = os.fdopen(r, 'r'), os.fdopen(w, 'w')
|
||||||
printer = LogPrinter(containers, output=writer)
|
printer = LogPrinter(containers, output=writer, monochrome=monochrome)
|
||||||
printer.run()
|
printer.run()
|
||||||
writer.close()
|
writer.close()
|
||||||
return reader.read()
|
return reader.read()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user