45 lines
1.5 KiB
TypeScript
45 lines
1.5 KiB
TypeScript
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
// Copyright (C) 2026 Bryan Joshua Pedini
|
|
|
|
import { COMPOSE_FILE, containerName, project, RUN_LABEL } from './env';
|
|
import { run, runOk, type ProcOptions, type ProcResult } from './proc';
|
|
|
|
export function docker(args: readonly string[], opts: ProcOptions = {}): Promise<ProcResult> {
|
|
return run('docker', args, opts);
|
|
}
|
|
|
|
export function dockerOk(args: readonly string[], opts: ProcOptions = {}): Promise<ProcResult> {
|
|
return runOk('docker', args, opts);
|
|
}
|
|
|
|
export function compose(args: readonly string[], opts: ProcOptions = {}): Promise<ProcResult> {
|
|
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<ProjectResources> {
|
|
const filter = `label=${RUN_LABEL}=${project()}`;
|
|
const [containers, volumes, networks] = await Promise.all([
|
|
dockerOk(['ps', '-a', '--filter', filter, '--format', '{{.Names}}']),
|
|
dockerOk(['volume', 'ls', '-q', '--filter', filter]),
|
|
dockerOk(['network', 'ls', '--filter', filter, '--format', '{{.Name}}']),
|
|
]);
|
|
return { containers: lines(containers.stdout), volumes: lines(volumes.stdout), networks: lines(networks.stdout) };
|
|
}
|