compose-action/src/services/input.service.ts
Emilien Escalle 2bd57c29bd fix: ensure given docker-compose file(s) are valid and at least one is provided
Signed-off-by: Emilien Escalle <emilien.escalle@escemi.com>
2024-04-02 21:37:18 +02:00

84 lines
1.9 KiB
TypeScript

import { getInput, getMultilineInput } from "@actions/core";
import { existsSync } from "fs";
import { join } from "path";
export type Inputs = {
composeFiles: string[];
services: string[];
composeFlags: string[];
upFlags: string[];
downFlags: string[];
cwd: string;
};
export enum InputNames {
ComposeFile = "compose-file",
Services = "services",
ComposeFlags = "compose-flags",
UpFlags = "up-flags",
DownFlags = "down-flags",
Cwd = "cwd",
}
export class InputService {
getInputs(): Inputs {
return {
composeFiles: this.getComposeFiles(),
services: this.getServices(),
composeFlags: this.getComposeFlags(),
upFlags: this.getUpFlags(),
downFlags: this.getDownFlags(),
cwd: this.getCwd(),
};
}
private getComposeFiles(): string[] {
const cwd = this.getCwd();
const composeFiles = getMultilineInput(InputNames.ComposeFile).filter((composeFile) => {
if (!composeFile.length) {
return false;
}
if (!existsSync(join(cwd, composeFile))) {
throw new Error(`${composeFile} does not exist in ${cwd}`);
}
return true;
});
if (!composeFiles.length) {
throw new Error("No compose files found");
}
return composeFiles;
}
private getServices(): string[] {
return getMultilineInput(InputNames.Services, { required: false });
}
private getComposeFlags(): string[] {
return this.parseFlags(getInput(InputNames.ComposeFlags));
}
private getUpFlags(): string[] {
return this.parseFlags(getInput(InputNames.UpFlags));
}
private getDownFlags(): string[] {
return this.parseFlags(getInput(InputNames.DownFlags));
}
private parseFlags(flags: string | null): string[] {
if (!flags) {
return [];
}
return flags.trim().split(" ");
}
private getCwd(): string {
return getInput(InputNames.Cwd);
}
}