@sourceregistry/node-ovsdb
Version:
TypeScript OVSDB client for Node.js
168 lines (167 loc) • 6.42 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.runCli = runCli;
const promises_1 = require("node:fs/promises");
const node_path_1 = require("node:path");
const generator_1 = require("./generator");
/**
* Runs the OVSDB type generation CLI.
*/
async function runCli(argv) {
const args = parseArgs(argv);
if (args.help) {
process.stdout.write(getHelpText());
return 0;
}
if (!args.schemaPath && !args.socketPath && !args.host) {
process.stderr.write("Missing input. Provide --schema <file>, --socket <path>, or --host <name>.\n");
process.stderr.write(getHelpText());
return 1;
}
if (!args.stdout && !args.outputPath) {
process.stderr.write("Missing destination. Provide --out <file> or use --stdout.\n");
process.stderr.write(getHelpText());
return 1;
}
try {
const outputPath = args.outputPath ? (0, node_path_1.resolve)(args.outputPath) : undefined;
if (outputPath) {
await (0, promises_1.mkdir)((0, node_path_1.dirname)(outputPath), { recursive: true });
}
const generated = await (0, generator_1.generateTypesFile)({
schemaPath: args.schemaPath ? (0, node_path_1.resolve)(args.schemaPath) : undefined,
socketPath: args.socketPath,
host: args.host,
port: args.port,
tls: args.tls,
tlsInsecure: args.tlsInsecure,
tlsServername: args.tlsServername,
tlsCaFile: args.tlsCaFile ? (0, node_path_1.resolve)(args.tlsCaFile) : undefined,
tlsCertFile: args.tlsCertFile ? (0, node_path_1.resolve)(args.tlsCertFile) : undefined,
tlsKeyFile: args.tlsKeyFile ? (0, node_path_1.resolve)(args.tlsKeyFile) : undefined,
databaseName: args.databaseName,
outputPath,
databaseTypeName: args.databaseTypeName,
importFrom: args.importFrom
});
if (args.stdout) {
process.stdout.write(generated);
}
else if (outputPath) {
process.stdout.write(`Generated TypeScript types at ${outputPath}\n`);
}
return 0;
}
catch (error) {
process.stderr.write(`${formatError(error)}\n`);
return 1;
}
}
function parseArgs(argv) {
const parsed = {};
for (let index = 0; index < argv.length; index += 1) {
const arg = argv[index];
switch (arg) {
case "--schema":
parsed.schemaPath = requireValue(argv, ++index, arg);
break;
case "--socket":
parsed.socketPath = requireValue(argv, ++index, arg);
break;
case "--host":
parsed.host = requireValue(argv, ++index, arg);
break;
case "--port":
parsed.port = Number.parseInt(requireValue(argv, ++index, arg), 10);
if (Number.isNaN(parsed.port)) {
throw new Error("Expected --port to be a number");
}
break;
case "--db":
case "--database":
parsed.databaseName = requireValue(argv, ++index, arg);
break;
case "--tls":
parsed.tls = true;
break;
case "--tls-insecure":
parsed.tlsInsecure = true;
break;
case "--tls-servername":
parsed.tlsServername = requireValue(argv, ++index, arg);
break;
case "--tls-ca-file":
parsed.tlsCaFile = requireValue(argv, ++index, arg);
break;
case "--tls-cert-file":
parsed.tlsCertFile = requireValue(argv, ++index, arg);
break;
case "--tls-key-file":
parsed.tlsKeyFile = requireValue(argv, ++index, arg);
break;
case "--out":
case "--output":
parsed.outputPath = requireValue(argv, ++index, arg);
break;
case "--name":
parsed.databaseTypeName = requireValue(argv, ++index, arg);
break;
case "--import-from":
parsed.importFrom = requireValue(argv, ++index, arg);
break;
case "--stdout":
parsed.stdout = true;
break;
case "--help":
case "-h":
parsed.help = true;
break;
default:
throw new Error(`Unknown argument: ${arg}`);
}
}
return parsed;
}
function requireValue(argv, index, flag) {
const value = argv[index];
if (!value || value.startsWith("--")) {
throw new Error(`Expected a value after ${flag}`);
}
return value;
}
function getHelpText() {
return [
"Usage: ovsdb-generate [options]",
"",
"Inputs:",
" --schema <file> Read an OVSDB schema JSON file",
" --socket <path> Read schema from a live OVSDB Unix socket",
" --host <name> Read schema from a live OVSDB TCP/TLS endpoint",
" --port <number> Port for TCP/TLS introspection (default: 6640)",
" --db <name> Database name for live introspection",
" --tls Enable TLS for live TCP connections",
" --tls-insecure Disable TLS certificate verification",
" --tls-servername <sni> Override TLS server name",
" --tls-ca-file <file> PEM CA bundle for TLS verification",
" --tls-cert-file <file> PEM client certificate for mTLS",
" --tls-key-file <file> PEM client private key for mTLS",
"",
"Output:",
" --out <file> Write generated TypeScript to a file",
" --stdout Print generated TypeScript to stdout",
"",
"Generation:",
" --name <typeName> Override generated top-level database type name",
" --import-from <module> Override imported OVSDB type module",
"",
"Other:",
" -h, --help Show this help text",
""
].join("\n");
}
function formatError(error) {
if (error instanceof Error) {
return error.message;
}
return String(error);
}