-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathcontainer-engine.ts
More file actions
68 lines (57 loc) · 1.53 KB
/
container-engine.ts
File metadata and controls
68 lines (57 loc) · 1.53 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
68
import { exec } from 'node:child_process'
import { promisify } from 'node:util'
import { platform } from 'node:os'
import log from './logger'
import { createEnhancedPath } from './utils/enhanced-path'
const execAsync = promisify(exec)
interface ContainerEngineStatus {
docker: boolean
podman: boolean
available: boolean
}
const tryCommand = async (command: string): Promise<boolean> => {
try {
await execAsync(command)
return true
} catch {
try {
await execAsync(command, {
env: { ...process.env, PATH: createEnhancedPath() },
})
return true
} catch {
return false
}
}
}
const getCommandName = (base: string): string =>
platform() === 'win32' ? `${base}.exe` : base
const checkDocker = (): Promise<boolean> =>
tryCommand(`${getCommandName('docker')} ps`)
const checkPodman = (): Promise<boolean> =>
tryCommand(`${getCommandName('podman')} ps`)
const createStatus = (
docker: boolean,
podman: boolean
): ContainerEngineStatus => ({
docker,
podman,
available: docker || podman,
})
export const checkContainerEngine =
async (): Promise<ContainerEngineStatus> => {
const [docker, podman] = await Promise.allSettled([
checkDocker(),
checkPodman(),
]).then((results) =>
results.map((result) => {
if (result.status === 'rejected') return false
return result.value
})
)
const status = createStatus(!!docker, !!podman)
if (!status.available) {
log.warn('No container engines detected')
}
return status
}