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

Use container_name as prefix if explicitly defined #6476

Open
wants to merge 3 commits into
base: master
from
Open
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.
+51 −7
Diff settings

Always

Just for now

@@ -19,18 +19,21 @@

class LogPresenter(object):

def __init__(self, prefix_width, color_func):
def __init__(self, prefix_width, color_func, config=None):
self.prefix_width = prefix_width
self.color_func = color_func
self.prefixes = {s['name']: s['container_name'] for s in filter(
lambda service: service.get('container_name', False), config.services if config else [])}

def present(self, container, line):
prefix = container.name_without_project.ljust(self.prefix_width)
prefix = self.prefixes.get(
container.service, container.name_without_project).ljust(self.prefix_width)
return '{prefix} {line}'.format(
prefix=self.color_func(prefix + ' |'),
line=line)


def build_log_presenters(service_names, monochrome):
def build_log_presenters(service_names, monochrome, config=None):

This comment has been minimized.

Copy link
@chris-crone

chris-crone Apr 9, 2019

Contributor

The prefix_width can no longer be calculated just from the service_names as the longest name could now be a container_name.

As part of this change, we may be able to avoid passing the whole config to __init__.

"""Return an iterable of functions.
Each function can be used to format the logs output of a container.
@@ -41,7 +44,7 @@ def no_color(text):
return text

for color_func in cycle([no_color] if monochrome else colors.rainbow()):
yield LogPresenter(prefix_width, color_func)
yield LogPresenter(prefix_width, color_func, config=config)


def max_name_width(service_names, max_index_width=3):
@@ -122,7 +122,9 @@ def perform_command(options, handler, command_options):
return

project = project_from_options('.', options)
command = TopLevelCommand(project, options=options)
config = get_config_from_options('.', options)

command = TopLevelCommand(project, config=config, options=options)
with errors.handle_connection_errors(project.client):
handler(command, command_options)

@@ -238,8 +240,9 @@ class TopLevelCommand(object):
version Show the Docker-Compose version information
"""

def __init__(self, project, options=None):
def __init__(self, project, config=None, options=None):

This comment has been minimized.

Copy link
@chris-crone

chris-crone Apr 9, 2019

Contributor

Please put new arguments at the end as this ensures that people using the arguments positionally aren't broken.

self.project = project
self.config = config
self.toplevel_options = options or {}

@property
@@ -652,6 +655,7 @@ def logs(self, options):
containers,
options['--no-color'],
log_args,
config=self.config,
event_stream=self.project.events(service_names=options['SERVICE'])).run()

def pause(self, options):
@@ -1106,6 +1110,7 @@ def up(rebuild):
options['--no-color'],
{'follow': True},
cascade_stop,
config=self.config,
event_stream=self.project.events(service_names=service_names))
print("Attaching to", list_containers(log_printer.containers))
cascade_starter = log_printer.run()
@@ -1386,11 +1391,12 @@ def log_printer_from_project(
monochrome,
log_args,
cascade_stop=False,
config=None,
event_stream=None,
):
return LogPrinter(
containers,
build_log_presenters(project.service_names, monochrome),
build_log_presenters(project.service_names, monochrome, config=config),
event_stream or project.events(),
cascade_stop=cascade_stop,
log_args=log_args)
@@ -2382,6 +2382,30 @@ def test_logs_tail(self):
assert 'w\n' not in result.stdout
assert 'x\n' not in result.stdout

def test_log_prefix_implicit(self):
self.base_dir = 'tests/fixtures/logs-composefile/foo'
self.dispatch(['up', 'simple'])

result = self.dispatch(['logs'])
assert 'simple_1' in result.stdout

def test_log_prefix_explicit(self):
self.base_dir = 'tests/fixtures/logs-composefile/foo'
self.dispatch(['up', 'another'])

result = self.dispatch(['logs'])
assert 'bar' in result.stdout
assert 'another' not in result.stdout

def test_log_prefix_explicit2(self):
# with project name prefixing the container_name
self.base_dir = 'tests/fixtures/logs-composefile/foo'
self.dispatch(['up', 'another2'])

result = self.dispatch(['logs'])
assert 'foo_another' in result.stdout
assert 'another2' not in result.stdout

def test_kill(self):
self.dispatch(['up', '-d'], None)
service = self.project.get_service('simple')
@@ -0,0 +1,11 @@
simple:
image: busybox:latest
command: sh -c "echo test"
another:
container_name: 'bar'
image: busybox:latest
command: sh -c "echo test"
another2:
container_name: 'foo_another'
image: busybox:latest
command: sh -c "echo test"
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.