Skip to content

Commit 9ab26e8

Browse files
committed
Minor script rethink
1 parent 743162e commit 9ab26e8

3 files changed

Lines changed: 26 additions & 14 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Add the following to your `.pre-commit-config.yaml` file
88

99
```yaml
1010
- repo: https://github.com/NtWriteCode/docker-pre-commit-universal
11-
rev: v0.0.1
11+
rev: v4.0.2
1212
hooks:
1313
- id: docker-compose-check
1414
```

‎docker_pre_commit_universal/compose_check.py‎

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,44 +4,56 @@
44
from pathlib import Path
55

66

7-
def get_compose_binary() -> str:
8-
compose_binary = 'docker compose'
7+
def get_compose_binary() -> list[str]:
8+
compose_binary = ['docker', 'compose']
99
try:
1010
subprocess.run(['docker', 'help', 'compose'], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, check=True)
1111
except subprocess.CalledProcessError:
1212
try:
1313
subprocess.run(['docker-compose'], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, check=True)
14-
compose_binary = 'docker-compose'
14+
compose_binary = ['docker-compose']
1515
except subprocess.CalledProcessError:
1616
print('ERROR: Neither "docker-compose" nor "docker compose" were found')
1717
sys.exit(1)
1818

1919
return compose_binary
2020

2121

22-
def check_file(file: str) -> int:
22+
def check_file(file: str) -> subprocess.CompletedProcess[bytes] | None:
2323
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
2733
else:
28-
return result.returncode
34+
return result
2935

3036

3137
def check_files(all_files: list[str]) -> bool:
3238
has_error = False
3339
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()
3748
return has_error
3849

3950

4051
def main() -> None:
4152
if check_files(sys.argv[1:]):
42-
print('Some compose files failed')
53+
print('Docker compose validation resulted in failure!')
4354
sys.exit(1)
4455
else:
56+
print('All docker compose files successfully validated.')
4557
sys.exit(0)
4658

4759

‎pyproject.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ packages = ["docker_pre_commit_universal"]
77

88
[project]
99
name = "docker_pre_commit_universal"
10-
version = "4.0.1"
10+
version = "4.0.2"
1111

1212
[project.scripts]
1313
compose_check = "docker_pre_commit_universal.compose_check:main"

0 commit comments

Comments
 (0)