diff options
author | Harald Albers <github@albersweb.de> | 2021-01-22 13:01:53 +0000 |
---|---|---|
committer | Harald Albers <github@albersweb.de> | 2021-01-22 19:24:15 +0000 |
commit | 862107a32a20793f87fd012a9242875c91a67479 (patch) | |
tree | edbb38587a701f4176313661557768b0fcb1e79a | |
parent | 8f2bb66e733d9204050fcee57e03022cbb135e52 (diff) |
Add option for listing service profiles
For command completion of `docker-compose --profile`, we need a portable
way to get a list of profiles used in the current config.
This commit adds a new option `docker-compose config --profiles`.
Signed-off-by: Harald Albers <github@albersweb.de>
-rw-r--r-- | compose/cli/main.py | 10 | ||||
-rw-r--r-- | contrib/completion/bash/docker-compose | 2 | ||||
-rw-r--r-- | tests/acceptance/cli_test.py | 5 | ||||
-rw-r--r-- | tests/fixtures/config-profiles/docker-compose.yml | 15 |
4 files changed, 31 insertions, 1 deletions
diff --git a/compose/cli/main.py b/compose/cli/main.py index 4cea03be..75e2a51f 100644 --- a/compose/cli/main.py +++ b/compose/cli/main.py @@ -381,6 +381,7 @@ class TopLevelCommand: --no-interpolate Don't interpolate environment variables. -q, --quiet Only validate the configuration, don't print anything. + --profiles Print the profile names, one per line. --services Print the service names, one per line. --volumes Print the volume names, one per line. --hash="*" Print the service config hash, one per line. @@ -400,6 +401,15 @@ class TopLevelCommand: if options['--quiet']: return + if options['--profiles']: + profiles = set() + for service in compose_config.services: + if 'profiles' in service: + for profile in service['profiles']: + profiles.add(profile) + print('\n'.join(sorted(profiles))) + return + if options['--services']: print('\n'.join(service['name'] for service in compose_config.services)) return diff --git a/contrib/completion/bash/docker-compose b/contrib/completion/bash/docker-compose index 677bbdbb..bf43644c 100644 --- a/contrib/completion/bash/docker-compose +++ b/contrib/completion/bash/docker-compose @@ -138,7 +138,7 @@ _docker_compose_config() { ;; esac - COMPREPLY=( $( compgen -W "--hash --help --no-interpolate --quiet -q --resolve-image-digests --services --volumes" -- "$cur" ) ) + COMPREPLY=( $( compgen -W "--hash --help --no-interpolate --profiles --quiet -q --resolve-image-digests --services --volumes" -- "$cur" ) ) } diff --git a/tests/acceptance/cli_test.py b/tests/acceptance/cli_test.py index 211579bd..ab8d2c9d 100644 --- a/tests/acceptance/cli_test.py +++ b/tests/acceptance/cli_test.py @@ -237,6 +237,11 @@ class CLITestCase(DockerClientTestCase): result = self.dispatch(['-H=tcp://doesnotexist:8000', 'ps'], returncode=1) assert "Couldn't connect to Docker daemon" in result.stderr + def test_config_list_profiles(self): + self.base_dir = 'tests/fixtures/config-profiles' + result = self.dispatch(['config', '--profiles']) + assert set(result.stdout.rstrip().split('\n')) == {'debug', 'frontend', 'gui'} + def test_config_list_services(self): self.base_dir = 'tests/fixtures/v2-full' result = self.dispatch(['config', '--services']) diff --git a/tests/fixtures/config-profiles/docker-compose.yml b/tests/fixtures/config-profiles/docker-compose.yml new file mode 100644 index 00000000..a445e7e0 --- /dev/null +++ b/tests/fixtures/config-profiles/docker-compose.yml @@ -0,0 +1,15 @@ +version: '3.8' +services: + frontend: + image: frontend + profiles: ["frontend", "gui"] + phpmyadmin: + image: phpmyadmin + depends_on: + - db + profiles: + - debug + backend: + image: backend + db: + image: mysql |