|
| 1 | +#!/usr/bin/env -S COLI_CONC_BASE=. deno run --allow-env --allow-read --allow-sys --ext=ts |
| 2 | + |
| 3 | +/** |
| 4 | + * Checks docker-compose.yml files for issues: |
| 5 | + * |
| 6 | + * - Duplicate service names |
| 7 | + * |
| 8 | + * Ideas for other issues to check: |
| 9 | + * |
| 10 | + * - restart directive |
| 11 | + * - Docker networks |
| 12 | + * - volume paths |
| 13 | + */ |
| 14 | + |
| 15 | +Deno.env.set("FORCE_COLOR", "2") |
| 16 | +import { existsSync } from "https://deno.land/std/fs/mod.ts" |
| 17 | + |
| 18 | +// Determine available targets by reading docker-compose.yml files in service subfolders |
| 19 | +import { parse as parseYaml } from "https://deno.land/[email protected]/yaml/mod.ts" |
| 20 | +import { getEnv } from "../src/utils.ts" |
| 21 | +const { servicePath } = getEnv("") |
| 22 | + |
| 23 | +const serviceNamesToComposeFiles = {} |
| 24 | +let ok = true |
| 25 | + |
| 26 | +for await (const { name, isDirectory } of Deno.readDir(servicePath)) { |
| 27 | + if (!isDirectory) { |
| 28 | + continue |
| 29 | + } |
| 30 | + try { |
| 31 | + // Try to read docker-compose.yml |
| 32 | + const file = `${servicePath}/${name}/docker-compose.yml` |
| 33 | + const fileShort = file.replace(servicePath, "") |
| 34 | + const compose = parseYaml(await Deno.readTextFile(file)) |
| 35 | + const errors: String[] = [] |
| 36 | + for (const service of Object.keys(compose?.services || {})) { |
| 37 | + if (serviceNamesToComposeFiles[service]) { |
| 38 | + errors.push(`!!! Service "${service}" is already defined in ${serviceNamesToComposeFiles[service]}`) |
| 39 | + ok = false |
| 40 | + } else { |
| 41 | + serviceNamesToComposeFiles[service] = fileShort |
| 42 | + } |
| 43 | + } |
| 44 | + if (errors.length) { |
| 45 | + console.error(`%c${fileShort}`, "color: red") |
| 46 | + errors.forEach(error => console.error(`%c ${error}`, "color: red")) |
| 47 | + } else { |
| 48 | + console.log(`%c${fileShort}`, "color: green") |
| 49 | + console.log("%c All good.", "color: green") |
| 50 | + } |
| 51 | + } catch (_) { |
| 52 | + // Just ignore errors as we are expecting them |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +if (!ok) { |
| 57 | + Deno.exit(1) |
| 58 | +} |
0 commit comments