mirror of
https://github.com/hoverkraft-tech/compose-action.git
synced 2026-01-13 16:13:08 +08:00
Some checks failed
Internal - Main - Continuous Integration / ci (push) Has been cancelled
Internal - Main - Continuous Integration / release (push) Has been cancelled
Need fix to Issue / main (push) Has been cancelled
Mark stale issues and pull requests / main (push) Has been cancelled
Signed-off-by: Emilien Escalle <emilien.escalle@escemi.com>
83 lines
2.0 KiB
TypeScript
83 lines
2.0 KiB
TypeScript
import {
|
|
down,
|
|
IDockerComposeLogOptions,
|
|
IDockerComposeOptions,
|
|
logs,
|
|
upAll,
|
|
upMany,
|
|
} from "docker-compose";
|
|
import { Inputs } from "./input.service";
|
|
|
|
type OptionsInputs = {
|
|
dockerFlags: Inputs["dockerFlags"];
|
|
composeFiles: Inputs["composeFiles"];
|
|
composeFlags: Inputs["composeFlags"];
|
|
cwd: Inputs["cwd"];
|
|
serviceLogger: (message: string) => void;
|
|
};
|
|
|
|
export type UpInputs = OptionsInputs & { upFlags: Inputs["upFlags"]; services: Inputs["services"] };
|
|
export type DownInputs = OptionsInputs & { downFlags: Inputs["downFlags"] };
|
|
export type LogsInputs = OptionsInputs & { services: Inputs["services"] };
|
|
|
|
export class DockerComposeService {
|
|
async up({ upFlags, services, ...optionsInputs }: UpInputs): Promise<void> {
|
|
const options: IDockerComposeOptions = {
|
|
...this.getCommonOptions(optionsInputs),
|
|
commandOptions: upFlags,
|
|
};
|
|
|
|
if (services.length > 0) {
|
|
await upMany(services, options);
|
|
return;
|
|
}
|
|
|
|
await upAll(options);
|
|
}
|
|
|
|
async down({ downFlags, ...optionsInputs }: DownInputs): Promise<void> {
|
|
const options: IDockerComposeOptions = {
|
|
...this.getCommonOptions(optionsInputs),
|
|
commandOptions: downFlags,
|
|
};
|
|
|
|
await down(options);
|
|
}
|
|
|
|
async logs({ services, ...optionsInputs }: LogsInputs): Promise<{
|
|
error: string;
|
|
output: string;
|
|
}> {
|
|
const options: IDockerComposeLogOptions = {
|
|
...this.getCommonOptions(optionsInputs),
|
|
follow: false,
|
|
};
|
|
|
|
const { err, out } = await logs(services, options);
|
|
|
|
return {
|
|
error: err,
|
|
output: out,
|
|
};
|
|
}
|
|
|
|
private getCommonOptions({
|
|
dockerFlags,
|
|
composeFiles,
|
|
composeFlags,
|
|
cwd,
|
|
serviceLogger,
|
|
}: OptionsInputs): IDockerComposeOptions {
|
|
return {
|
|
config: composeFiles,
|
|
composeOptions: composeFlags,
|
|
cwd: cwd,
|
|
callback: (chunk) => serviceLogger(chunk.toString()),
|
|
executable: {
|
|
executablePath: "docker",
|
|
options: dockerFlags,
|
|
},
|
|
};
|
|
}
|
|
}
|