|
4 | 4 | from pathlib import Path |
5 | 5 |
|
6 | 6 |
|
7 | | -def get_compose_binary() -> str: |
8 | | - compose_binary = 'docker compose' |
| 7 | +def get_compose_binary() -> list[str]: |
| 8 | + compose_binary = ['docker', 'compose'] |
9 | 9 | try: |
10 | 10 | subprocess.run(['docker', 'help', 'compose'], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, check=True) |
11 | 11 | except subprocess.CalledProcessError: |
12 | 12 | try: |
13 | 13 | subprocess.run(['docker-compose'], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, check=True) |
14 | | - compose_binary = 'docker-compose' |
| 14 | + compose_binary = ['docker-compose'] |
15 | 15 | except subprocess.CalledProcessError: |
16 | 16 | print('ERROR: Neither "docker-compose" nor "docker compose" were found') |
17 | 17 | sys.exit(1) |
18 | 18 |
|
19 | 19 | return compose_binary |
20 | 20 |
|
21 | 21 |
|
22 | | -def check_file(file: str) -> int: |
| 22 | +def check_file(file: str) -> subprocess.CompletedProcess[bytes] | None: |
23 | 23 | try: |
24 | | - result = subprocess.run([get_compose_binary(), '--file', file, 'config', '--quiet'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=True) |
25 | | - except Exception: |
26 | | - return 1 |
| 24 | + commands = get_compose_binary() |
| 25 | + commands.append('--file') |
| 26 | + commands.append(file) |
| 27 | + commands.append('config') |
| 28 | + commands.append('--quiet') |
| 29 | + result = subprocess.run(commands, stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=False) |
| 30 | + except Exception as e: |
| 31 | + print(f'Exception happened during the execution of {commands!s}: {e!s}') |
| 32 | + return None |
27 | 33 | else: |
28 | | - return result.returncode |
| 34 | + return result |
29 | 35 |
|
30 | 36 |
|
31 | 37 | def check_files(all_files: list[str]) -> bool: |
32 | 38 | has_error = False |
33 | 39 | for file in all_files: |
34 | | - if Path(file).is_file() and check_file(file) != 0: |
35 | | - print(f'ERROR: {file}') |
36 | | - has_error = True |
| 40 | + if Path(file).is_file(): |
| 41 | + proc = check_file(file) |
| 42 | + if proc and proc.returncode != 0: |
| 43 | + print(f'> Error in {file}:') |
| 44 | + print(f'\t{proc.stdout.decode().strip()} {proc.stderr.decode().strip()}') |
| 45 | + has_error = True |
| 46 | + elif not proc: |
| 47 | + print() |
37 | 48 | return has_error |
38 | 49 |
|
39 | 50 |
|
40 | 51 | def main() -> None: |
41 | 52 | if check_files(sys.argv[1:]): |
42 | | - print('Some compose files failed') |
| 53 | + print('Docker compose validation resulted in failure!') |
43 | 54 | sys.exit(1) |
44 | 55 | else: |
| 56 | + print('All docker compose files successfully validated.') |
45 | 57 | sys.exit(0) |
46 | 58 |
|
47 | 59 |
|
|
0 commit comments