2026-04-02
This commit is contained in:
parent
5f332f4068
commit
15371d2469
13 changed files with 295 additions and 125 deletions
|
|
@ -1,40 +1,98 @@
|
|||
import { $ } from "bun";
|
||||
import { Array, Option, Order, pipe } from "effect";
|
||||
import { Array, Console, Effect, Layer, ManagedRuntime, Option, Order, pipe, Schema, ServiceMap } from "effect";
|
||||
import { UnknownError } from "effect/Cause";
|
||||
import { readdir } from "node:fs/promises";
|
||||
|
||||
const launchContainers = async (): Promise<string> => {
|
||||
return await $`podman compose up -d`.text();
|
||||
};
|
||||
class PodmanError extends Schema.TaggedErrorClass<PodmanError>()("PodmanError", {
|
||||
cause: Schema.Error,
|
||||
}) {}
|
||||
|
||||
const getLatestDbExport = async (): Promise<string> => {
|
||||
return pipe(
|
||||
await readdir(`../db`),
|
||||
(paths: ReadonlyArray<string>) => Array.sort(paths, Order.string),
|
||||
(sortedPaths: ReadonlyArray<string>) => Array.last(sortedPaths),
|
||||
(last: Option.Option<string>) =>
|
||||
Option.getOrThrowWith(last, () => new Error("Aucun export de BDD n'est disponible.")),
|
||||
);
|
||||
};
|
||||
class FileSystemError extends Schema.TaggedErrorClass<FileSystemError>()("FileSystemError", {
|
||||
cause: Schema.Error,
|
||||
}) {}
|
||||
|
||||
const importLatestDbInWordpressContainer = async (exportPath: string) => {
|
||||
await $`podman exec -it haikuatelier.fr-wordpress fish -c "cd web && wp --allow-root db import ${exportPath}"`;
|
||||
};
|
||||
|
||||
try {
|
||||
// S'assure que les conteneurs soient lancées.
|
||||
await launchContainers();
|
||||
|
||||
const latestExportPath: string = `../db/${await getLatestDbExport()}`;
|
||||
console.log(`Dernier export : ${latestExportPath}`);
|
||||
|
||||
// Exécute l'opération d'import dans le conteneur WordPress via wp-cli.
|
||||
await importLatestDbInWordpressContainer(latestExportPath);
|
||||
} catch (error: unknown) {
|
||||
if (error instanceof $.ShellError) {
|
||||
console.error(`Commande échouée avec code d'erreur: ${error.exitCode}`);
|
||||
console.log(error.stdout.toString());
|
||||
console.log(error.stderr.toString());
|
||||
} else {
|
||||
console.error(error);
|
||||
class Podman extends ServiceMap.Service<
|
||||
Podman,
|
||||
{
|
||||
launchContainers(): Effect.Effect<string, PodmanError>;
|
||||
importLatestDbInWordPressContainer(exportPath: string): Effect.Effect<string, PodmanError>;
|
||||
}
|
||||
>()("haikuatelier.fr/scripts/importe-dernier-export-bdd/Podman") {
|
||||
static readonly layer = Layer.effect(
|
||||
Podman,
|
||||
// oxlint-disable-next-line require-yield
|
||||
Effect.gen(function* () {
|
||||
const launchContainers = Effect.fn("launchContainers")(function* () {
|
||||
return yield* pipe(
|
||||
Effect.tryPromise(() => $`podman compose up -d &> /dev/null`),
|
||||
Effect.map((shell: $.ShellOutput) => shell.text()),
|
||||
Effect.mapError((error: UnknownError) => new PodmanError({ cause: error })),
|
||||
);
|
||||
});
|
||||
|
||||
const importLatestDbInWordPressContainer = Effect.fn("importLatestDbInWordPressContainer")(function* (
|
||||
exportPath: string,
|
||||
) {
|
||||
return yield* pipe(
|
||||
Effect.tryPromise(
|
||||
() =>
|
||||
$`podman exec -it haikuatelier.fr-wordpress fish -c "cd web && wp --allow-root db import ${exportPath} > /dev/null"`,
|
||||
),
|
||||
Effect.map((shell: $.ShellOutput) => shell.text()),
|
||||
Effect.mapError((error: UnknownError) => new PodmanError({ cause: error })),
|
||||
);
|
||||
});
|
||||
|
||||
return Podman.of({
|
||||
launchContainers,
|
||||
importLatestDbInWordPressContainer,
|
||||
});
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
class FileSystem extends ServiceMap.Service<
|
||||
FileSystem,
|
||||
{
|
||||
getLatestDbExport(): Effect.Effect<string, FileSystemError>;
|
||||
}
|
||||
>()("haikuatelier.fr/scripts/importe-dernier-export-bdd/FileSystem") {
|
||||
static readonly layer = Layer.effect(
|
||||
FileSystem,
|
||||
// oxlint-disable-next-line require-yield
|
||||
Effect.gen(function* () {
|
||||
const getLatestDbExport = Effect.fn("getLatestDbExport")(function* () {
|
||||
return yield* pipe(
|
||||
Effect.tryPromise(() => readdir(`./db`)),
|
||||
Effect.map((paths: ReadonlyArray<string>) => Array.sort(paths, Order.String)),
|
||||
Effect.map((sortedPaths: ReadonlyArray<string>) => Array.last(sortedPaths)),
|
||||
Effect.flatMap((path: Option.Option<string>) => Effect.fromOption(path)),
|
||||
Effect.mapError((_) => new FileSystemError({ cause: new Error("Aucun export de BDD n'est disponible.") })),
|
||||
);
|
||||
});
|
||||
|
||||
return FileSystem.of({
|
||||
getLatestDbExport,
|
||||
});
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
const mainLayer = Layer.mergeAll(Podman.layer, FileSystem.layer);
|
||||
const runtime = ManagedRuntime.make(mainLayer);
|
||||
|
||||
const program = Effect.fn("program")(function* () {
|
||||
yield* Podman.use((podman) => podman.launchContainers());
|
||||
yield* Console.log("Containers are launched.");
|
||||
|
||||
const latestExportPath: string = pipe(
|
||||
yield* FileSystem.use((fs) => fs.getLatestDbExport()),
|
||||
(path) => `../db/${path}`,
|
||||
);
|
||||
yield* Console.log(latestExportPath);
|
||||
|
||||
yield* Podman.use((podman) => podman.importLatestDbInWordPressContainer(latestExportPath));
|
||||
yield* Console.log("Import done.");
|
||||
});
|
||||
|
||||
runtime.runFork(program().pipe(Effect.tapError(Console.error)));
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue