forked from ProverCoderAI/docker-git
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser-options.ts
More file actions
160 lines (143 loc) · 5.58 KB
/
parser-options.ts
File metadata and controls
160 lines (143 loc) · 5.58 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import { Either } from "effect"
import type { RawOptions } from "@effect-template/lib/core/command-options"
import type { ParseError } from "@effect-template/lib/core/domain"
interface ValueOptionSpec {
readonly flag: string
readonly key:
| "repoUrl"
| "repoRef"
| "targetDir"
| "sshPort"
| "sshUser"
| "containerName"
| "serviceName"
| "volumeName"
| "secretsRoot"
| "authorizedKeysPath"
| "envGlobalPath"
| "envProjectPath"
| "codexAuthPath"
| "codexHome"
| "archivePath"
| "scrapMode"
| "label"
| "token"
| "scopes"
| "message"
| "outDir"
| "projectDir"
| "lines"
}
const valueOptionSpecs: ReadonlyArray<ValueOptionSpec> = [
{ flag: "--repo-url", key: "repoUrl" },
{ flag: "--repo-ref", key: "repoRef" },
{ flag: "--branch", key: "repoRef" },
{ flag: "-b", key: "repoRef" },
{ flag: "--target-dir", key: "targetDir" },
{ flag: "--ssh-port", key: "sshPort" },
{ flag: "--ssh-user", key: "sshUser" },
{ flag: "--container-name", key: "containerName" },
{ flag: "--service-name", key: "serviceName" },
{ flag: "--volume-name", key: "volumeName" },
{ flag: "--secrets-root", key: "secretsRoot" },
{ flag: "--authorized-keys", key: "authorizedKeysPath" },
{ flag: "--env-global", key: "envGlobalPath" },
{ flag: "--env-project", key: "envProjectPath" },
{ flag: "--codex-auth", key: "codexAuthPath" },
{ flag: "--codex-home", key: "codexHome" },
{ flag: "--archive", key: "archivePath" },
{ flag: "--mode", key: "scrapMode" },
{ flag: "--label", key: "label" },
{ flag: "--token", key: "token" },
{ flag: "--scopes", key: "scopes" },
{ flag: "--message", key: "message" },
{ flag: "-m", key: "message" },
{ flag: "--out-dir", key: "outDir" },
{ flag: "--project-dir", key: "projectDir" },
{ flag: "--lines", key: "lines" }
]
const valueOptionSpecByFlag: ReadonlyMap<string, ValueOptionSpec> = new Map(
valueOptionSpecs.map((spec) => [spec.flag, spec])
)
type ValueKey = ValueOptionSpec["key"]
const booleanFlagUpdaters: Readonly<Record<string, (raw: RawOptions) => RawOptions>> = {
"--up": (raw) => ({ ...raw, up: true }),
"--no-up": (raw) => ({ ...raw, up: false }),
"--force": (raw) => ({ ...raw, force: true }),
"--force-env": (raw) => ({ ...raw, forceEnv: true }),
"--mcp-playwright": (raw) => ({ ...raw, enableMcpPlaywright: true }),
"--no-mcp-playwright": (raw) => ({ ...raw, enableMcpPlaywright: false }),
"--wipe": (raw) => ({ ...raw, wipe: true }),
"--no-wipe": (raw) => ({ ...raw, wipe: false }),
"--web": (raw) => ({ ...raw, authWeb: true }),
"--include-default": (raw) => ({ ...raw, includeDefault: true })
}
const valueFlagUpdaters: { readonly [K in ValueKey]: (raw: RawOptions, value: string) => RawOptions } = {
repoUrl: (raw, value) => ({ ...raw, repoUrl: value }),
repoRef: (raw, value) => ({ ...raw, repoRef: value }),
targetDir: (raw, value) => ({ ...raw, targetDir: value }),
sshPort: (raw, value) => ({ ...raw, sshPort: value }),
sshUser: (raw, value) => ({ ...raw, sshUser: value }),
containerName: (raw, value) => ({ ...raw, containerName: value }),
serviceName: (raw, value) => ({ ...raw, serviceName: value }),
volumeName: (raw, value) => ({ ...raw, volumeName: value }),
secretsRoot: (raw, value) => ({ ...raw, secretsRoot: value }),
authorizedKeysPath: (raw, value) => ({ ...raw, authorizedKeysPath: value }),
envGlobalPath: (raw, value) => ({ ...raw, envGlobalPath: value }),
envProjectPath: (raw, value) => ({ ...raw, envProjectPath: value }),
codexAuthPath: (raw, value) => ({ ...raw, codexAuthPath: value }),
codexHome: (raw, value) => ({ ...raw, codexHome: value }),
archivePath: (raw, value) => ({ ...raw, archivePath: value }),
scrapMode: (raw, value) => ({ ...raw, scrapMode: value }),
label: (raw, value) => ({ ...raw, label: value }),
token: (raw, value) => ({ ...raw, token: value }),
scopes: (raw, value) => ({ ...raw, scopes: value }),
message: (raw, value) => ({ ...raw, message: value }),
outDir: (raw, value) => ({ ...raw, outDir: value }),
projectDir: (raw, value) => ({ ...raw, projectDir: value }),
lines: (raw, value) => ({ ...raw, lines: value })
}
export const applyCommandBooleanFlag = (raw: RawOptions, token: string): RawOptions | null => {
const updater = booleanFlagUpdaters[token]
return updater ? updater(raw) : null
}
export const applyCommandValueFlag = (
raw: RawOptions,
token: string,
value: string
): Either.Either<RawOptions, ParseError> => {
const valueSpec = valueOptionSpecByFlag.get(token)
if (valueSpec === undefined) {
return Either.left({ _tag: "UnknownOption", option: token })
}
const update = valueFlagUpdaters[valueSpec.key]
return Either.right(update(raw, value))
}
export const parseRawOptions = (args: ReadonlyArray<string>): Either.Either<RawOptions, ParseError> => {
let index = 0
let raw: RawOptions = {}
while (index < args.length) {
const token = args[index] ?? ""
const booleanApplied = applyCommandBooleanFlag(raw, token)
if (booleanApplied !== null) {
raw = booleanApplied
index += 1
continue
}
if (!token.startsWith("-")) {
return Either.left({ _tag: "UnexpectedArgument", value: token })
}
const value = args[index + 1]
if (value === undefined) {
return Either.left({ _tag: "MissingOptionValue", option: token })
}
const nextRaw = applyCommandValueFlag(raw, token, value)
if (Either.isLeft(nextRaw)) {
return Either.left(nextRaw.left)
}
raw = nextRaw.right
index += 2
}
return Either.right(raw)
}
export { type RawOptions } from "@effect-template/lib/core/command-options"