-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathcontroller-compose-runtime.ts
More file actions
67 lines (62 loc) · 2.55 KB
/
Copy pathcontroller-compose-runtime.ts
File metadata and controls
67 lines (62 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import type { PlatformError } from "@effect/platform/Error"
import * as FileSystem from "@effect/platform/FileSystem"
import * as Path from "@effect/platform/Path"
import { Effect } from "effect"
import {
type ControllerDockerRuntime,
controllerDockerRuntimeEnvKey,
parseControllerDockerRuntime
} from "./controller-runtime.js"
import { type ControllerBootstrapError, controllerBootstrapError } from "./host-errors.js"
const mapComposePathError = (error: PlatformError): ControllerBootstrapError =>
controllerBootstrapError(`Failed to resolve docker-compose.yml path.\nDetails: ${String(error)}`)
export const loadControllerDockerRuntime = (): Effect.Effect<ControllerDockerRuntime, ControllerBootstrapError> => {
const raw = process.env[controllerDockerRuntimeEnvKey]
const parsed = parseControllerDockerRuntime(raw)
if (parsed !== null) {
return Effect.succeed(parsed)
}
return Effect.fail(
controllerBootstrapError(
`${controllerDockerRuntimeEnvKey} must be unset or one of: host, isolated. Received: ${raw ?? ""}`
)
)
}
const isolatedOverlayFileName = (composeFileName: string): Effect.Effect<string, ControllerBootstrapError> => {
if (composeFileName.endsWith(".yaml")) {
return Effect.succeed(`${composeFileName.slice(0, -".yaml".length)}.isolated.yaml`)
}
if (composeFileName.endsWith(".yml")) {
return Effect.succeed(`${composeFileName.slice(0, -".yml".length)}.isolated.yml`)
}
return Effect.fail(
controllerBootstrapError(
`${controllerDockerRuntimeEnvKey}=isolated requires a .yml or .yaml compose file. Received: ${composeFileName}`
)
)
}
export const resolveControllerRuntimeOverlayPath = (
composePath: string,
dockerRuntime: ControllerDockerRuntime
): Effect.Effect<string | null, ControllerBootstrapError, FileSystem.FileSystem | Path.Path> =>
dockerRuntime === "host"
? Effect.succeed(null)
: Effect.gen(function*(_) {
const fs = yield* _(FileSystem.FileSystem)
const path = yield* _(Path.Path)
const overlayFileName = yield* _(isolatedOverlayFileName(path.basename(composePath)))
const runtimeOverlayPath = path.join(
path.dirname(composePath),
overlayFileName
)
const exists = yield* _(fs.exists(runtimeOverlayPath).pipe(Effect.mapError(mapComposePathError)))
return exists
? runtimeOverlayPath
: yield* _(
Effect.fail(
controllerBootstrapError(
`${controllerDockerRuntimeEnvKey}=isolated requires ${runtimeOverlayPath}, but it was not found.`
)
)
)
})