// SPDX-License-Identifier: GPL-2.0-or-later // Copyright (C) 2026 Bryan Joshua Pedini import { COMPOSE_FILE, containerName, project, RESOURCE_PREFIX, RUN_LABEL } from './env'; import { run, runOk, type ProcOptions, type ProcResult } from './proc'; export function docker(args: readonly string[], opts: ProcOptions = {}): Promise { return run('docker', args, opts); } export function dockerOk(args: readonly string[], opts: ProcOptions = {}): Promise { return runOk('docker', args, opts); } export function compose(args: readonly string[], opts: ProcOptions = {}): Promise { return runOk('docker', ['compose', '--project-name', project(), '--file', COMPOSE_FILE, ...args], opts); } export function runnerContainer(): string { return containerName('runner'); } function lines(s: string): string[] { return s .split('\n') .map((l) => l.trim()) .filter(Boolean); } export interface ProjectResources { containers: string[]; volumes: string[]; networks: string[]; } export async function projectResources(): Promise { const p = project(); const byLabel = await dockerOk(['ps', '-a', '--filter', `label=${RUN_LABEL}=${p}`, '--format', '{{.Names}}']); const byName = await dockerOk(['ps', '-a', '--filter', `name=^${p}`, '--format', '{{.Names}}']); const byCompose = await dockerOk(['ps', '-a', '--filter', `label=com.docker.compose.project=${p}`, '--format', '{{.Names}}']); const self = new Set([runnerContainer()]); const containers = [...new Set([...lines(byLabel.stdout), ...lines(byName.stdout), ...lines(byCompose.stdout)])].filter( (n) => !self.has(n) && !/^.+-runner-run-[0-9a-f]+$/.test(n), ); const volumes = new Set(); for (const filter of [`label=${RUN_LABEL}=${p}`, `label=com.docker.compose.project=${p}`, `name=${p}`]) { const r = await dockerOk(['volume', 'ls', '-q', '--filter', filter]); for (const v of lines(r.stdout)) volumes.add(v); } const networks = new Set(); for (const filter of [`label=${RUN_LABEL}=${p}`, `name=${p}`]) { const r = await dockerOk(['network', 'ls', '--format', '{{.Name}}', '--filter', filter]); for (const n of lines(r.stdout)) networks.add(n); } return { containers, volumes: [...volumes], networks: [...networks] }; } export function isOwnedName(name: string): boolean { return name.startsWith(RESOURCE_PREFIX); }