-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathcli.ts
More file actions
342 lines (332 loc) · 12.4 KB
/
cli.ts
File metadata and controls
342 lines (332 loc) · 12.4 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
import envConfig, {
buildServeCommandConfig,
parseDataDirFromCli,
type ServeCommandCliArgs,
} from "@/config";
import { join, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import type { ArgumentsCamelCase, Argv } from "yargs";
import { hideBin } from "yargs/helpers";
import yargs from "yargs/yargs";
import { buildLabelSetId, buildLabelSetVersion } from "@ensnode/ensnode-sdk";
import { PortNumberSchema } from "@ensnode/ensnode-sdk/internal";
import { type ConvertSqlCommandCliArgs, convertCommand } from "@/commands/convert-command-sql";
import { type ConvertCsvCommandCliArgs, convertCsvCommand } from "@/commands/convert-csv-command";
import { entrypointCommand } from "@/commands/entrypoint-command";
import {
type IngestProtobufCommandCliArgs,
ingestProtobufCommand,
} from "@/commands/ingest-protobuf-command";
import { type PurgeCommandCliArgs, purgeCommand } from "@/commands/purge-command";
import { serverCommand } from "@/commands/server-command";
import { type ValidateCommandCliArgs, validateCommand } from "@/commands/validate-command";
export interface CLIOptions {
exitProcess?: boolean;
}
/**
* yargs-parsed argument shape for the `entrypoint` command.
*
* `label-set-id` and `label-set-version` are coerced to their branded types via
* `buildLabelSetId` / `buildLabelSetVersion`, so the CLI layer works with primitive types
* and hands branded values to {@link entrypointCommand}.
*/
interface EntrypointCommandCliArgs {
port: number;
"data-dir": string;
"db-schema-version": number;
"label-set-id": string;
"label-set-version": number;
"download-temp-dir"?: string;
}
export function createCLI(options: CLIOptions = {}) {
const { exitProcess = true } = options;
return (
yargs()
.scriptName("ensrainbow")
.exitProcess(exitProcess)
// .command(
// "ingest",
// "Ingest labels from SQL dump into LevelDB",
// (yargs: Argv) => {
// return yargs
// .option("input-file", {
// type: "string",
// description: "Path to the gzipped SQL dump file",
// default: join(process.cwd(), "ens_names.sql.gz"),
// })
// .option("data-dir", {
// type: "string",
// description: "Directory to store LevelDB data",
// default: getDefaultDataDir(),
// });
// },
// async (argv: ArgumentsCamelCase<IngestArgs>) => {
// await ingestCommand({
// inputFile: argv["input-file"],
// dataDir: argv["data-dir"],
// });
// },
// )
.command(
"ingest-ensrainbow",
"Ingest labels from protobuf file into LevelDB",
(yargs: Argv) => {
return yargs
.option("input-file", {
type: "string",
description: "Path to the protobuf file",
default: join(process.cwd(), "rainbow-records.pb"),
})
.option("data-dir", {
type: "string",
description: "Directory to store LevelDB data",
default: envConfig.dataDir,
});
},
async (argv: ArgumentsCamelCase<IngestProtobufCommandCliArgs>) => {
const dataDir = parseDataDirFromCli(argv["data-dir"]);
await ingestProtobufCommand({
inputFile: argv["input-file"],
dataDir,
});
},
)
.command(
"serve",
"Start the ENS Rainbow API server",
(yargs: Argv) => {
return yargs
.option("port", {
type: "number",
description: "Port to listen on (overrides PORT env var if both are set)",
default: envConfig.port,
coerce: (port: number) => {
const result = PortNumberSchema.safeParse(port);
if (!result.success) {
const firstError = result.error.issues[0];
throw new Error(`Invalid port: ${firstError?.message ?? "invalid port number"}`);
}
return result.data;
},
})
.option("data-dir", {
type: "string",
description: "Directory containing LevelDB data",
default: envConfig.dataDir,
});
},
async (argv: ArgumentsCamelCase<ServeCommandCliArgs>) => {
const serveCommandConfig = buildServeCommandConfig(argv);
await serverCommand(serveCommandConfig);
},
)
.command(
"entrypoint",
"Start the ENS Rainbow API server immediately and bootstrap the database in the background",
(yargs: Argv) => {
return yargs
.option("port", {
type: "number",
description: "Port to listen on (overrides PORT env var if both are set)",
default: envConfig.port,
coerce: (port: number) => {
const result = PortNumberSchema.safeParse(port);
if (!result.success) {
const firstError = result.error.issues[0];
throw new Error(`Invalid port: ${firstError?.message ?? "invalid port number"}`);
}
return result.data;
},
})
.option("data-dir", {
type: "string",
description: "Directory containing LevelDB data",
default: envConfig.dataDir,
})
.option("db-schema-version", {
type: "number",
description:
"Expected database schema version (falls back to DB_SCHEMA_VERSION env var)",
default: envConfig.dbSchemaVersion,
})
.option("label-set-id", {
type: "string",
description: "Label set id to download (falls back to LABEL_SET_ID env var)",
default: process.env.LABEL_SET_ID,
demandOption: !process.env.LABEL_SET_ID,
})
.coerce("label-set-id", buildLabelSetId)
.option("label-set-version", {
type: "number",
description:
"Label set version to download (falls back to LABEL_SET_VERSION env var)",
default: process.env.LABEL_SET_VERSION,
demandOption: !process.env.LABEL_SET_VERSION,
})
.coerce("label-set-version", buildLabelSetVersion)
.option("download-temp-dir", {
type: "string",
description:
"Temporary directory used to stage downloaded archives before extraction " +
"(defaults to <data-dir>/.download-temp)",
default: process.env.DOWNLOAD_TEMP_DIR,
});
},
async (argv: ArgumentsCamelCase<EntrypointCommandCliArgs>) => {
const dataDir = parseDataDirFromCli(argv["data-dir"]);
await entrypointCommand({
port: argv.port,
dataDir,
dbSchemaVersion: argv["db-schema-version"],
labelSetId: argv["label-set-id"],
labelSetVersion: argv["label-set-version"],
downloadTempDir: argv["download-temp-dir"],
labelsetServerUrl: process.env.ENSRAINBOW_LABELSET_SERVER_URL,
});
},
)
.command(
"validate",
"Validate the integrity of the LevelDB database",
(yargs: Argv) => {
return yargs
.option("data-dir", {
type: "string",
description: "Directory containing LevelDB data",
default: envConfig.dataDir,
})
.option("lite", {
type: "boolean",
description:
"Perform a faster, less thorough validation by skipping hash verification and record count validation",
default: false,
});
},
async (argv: ArgumentsCamelCase<ValidateCommandCliArgs>) => {
const dataDir = parseDataDirFromCli(argv["data-dir"]);
await validateCommand({
dataDir,
lite: argv.lite,
});
},
)
.command(
"purge",
"Completely wipe all files from the specified data directory",
(yargs: Argv) => {
return yargs.option("data-dir", {
type: "string",
description: "Directory containing LevelDB data",
default: envConfig.dataDir,
});
},
async (argv: ArgumentsCamelCase<PurgeCommandCliArgs>) => {
const dataDir = parseDataDirFromCli(argv["data-dir"]);
await purgeCommand({
dataDir,
});
},
)
.command(
"convert",
"Convert rainbow tables from CSV format to ensrainbow format",
(yargs: Argv) => {
return yargs
.option("input-file", {
type: "string",
description: "Path to the CSV input file",
demandOption: true,
})
.option("label-set-id", {
type: "string",
description: "Label set id for the generated ensrainbow file",
demandOption: true,
})
.coerce("label-set-id", buildLabelSetId)
.option("output-file", {
type: "string",
description:
"Path to where the resulting ensrainbow file will be output (if not provided, will be generated automatically)",
})
.option("progress-interval", {
type: "number",
description: "Number of records to process before logging progress",
default: 50000,
})
.option("existing-db-path", {
type: "string",
description:
"Path to existing ENSRainbow database to filter out existing labels and determine the next label set version (if not provided, version will be 0)",
})
.option("silent", {
type: "boolean",
description: "Disable progress bar (useful for scripts)",
default: false,
});
},
async (argv: ArgumentsCamelCase<ConvertCsvCommandCliArgs>) => {
await convertCsvCommand({
inputFile: argv["input-file"],
outputFile: argv["output-file"],
labelSetId: argv["label-set-id"],
progressInterval: argv["progress-interval"],
existingDbPath: argv["existing-db-path"],
silent: argv.silent,
});
},
)
.command(
"convert-sql",
"Convert rainbow tables from legacy SQL dump to ensrainbow format",
(yargs: Argv) => {
return yargs
.option("input-file", {
type: "string",
description: "Path to the gzipped SQL dump file",
default: join(process.cwd(), "ens_names.sql.gz"),
})
.option("label-set-id", {
type: "string",
description: "Label set id for the generated ensrainbow file",
demandOption: true,
})
.coerce("label-set-id", buildLabelSetId)
.option("output-file", {
type: "string",
description: "Path to where the resulting ensrainbow file will be output",
});
},
async (argv: ArgumentsCamelCase<ConvertSqlCommandCliArgs>) => {
const outputFile =
argv["output-file"] ?? join(process.cwd(), `${argv["label-set-id"]}_0.ensrainbow`);
await convertCommand({
inputFile: argv["input-file"],
outputFile,
labelSetId: argv["label-set-id"],
labelSetVersion: 0,
});
},
)
.demandCommand(1, "You must specify a command")
.fail((msg, err, yargs) => {
if (process.env.VITEST) {
// the test functions expect the default behavior of cli.parse to throw
if (err) throw err;
if (msg) throw new Error(msg);
} else {
// but we want to override yargs' default printing to stdout/stderr with console printing,
// such that it can be silenced with vitest --silent
yargs.showHelp();
if (msg) console.error(msg);
if (err) console.error(err);
}
})
.strict()
.help()
);
}
// Only execute if this is the main module
const isMainModule = resolve(process.argv[1]) === fileURLToPath(import.meta.url);
if (isMainModule) {
createCLI().parse(hideBin(process.argv));
}