forked from ProverCoderAI/docker-git
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand-builders-shared.ts
More file actions
53 lines (48 loc) · 1.5 KB
/
command-builders-shared.ts
File metadata and controls
53 lines (48 loc) · 1.5 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
import { Either } from "effect"
import { type CreateCommand, defaultTemplateConfig, isDockerNetworkMode, type ParseError } from "./domain.js"
const parsePort = (value: string): Either.Either<number, ParseError> => {
const parsed = Number(value)
if (!Number.isInteger(parsed)) {
return Either.left({
_tag: "InvalidOption",
option: "--ssh-port",
reason: `expected integer, got: ${value}`
})
}
if (parsed < 1 || parsed > 65_535) {
return Either.left({
_tag: "InvalidOption",
option: "--ssh-port",
reason: "must be between 1 and 65535"
})
}
return Either.right(parsed)
}
export const parseSshPort = (value: string): Either.Either<number, ParseError> => parsePort(value)
export const parseDockerNetworkMode = (
value: string | undefined
): Either.Either<CreateCommand["config"]["dockerNetworkMode"], ParseError> => {
const candidate = value?.trim() ?? defaultTemplateConfig.dockerNetworkMode
if (isDockerNetworkMode(candidate)) {
return Either.right(candidate)
}
return Either.left({
_tag: "InvalidOption",
option: "--network-mode",
reason: "expected one of: shared, project"
})
}
export const nonEmpty = (
option: string,
value: string | undefined,
fallback?: string
): Either.Either<string, ParseError> => {
const candidate = value?.trim() ?? fallback
if (candidate === undefined || candidate.length === 0) {
return Either.left({
_tag: "MissingRequiredOption",
option
})
}
return Either.right(candidate)
}