feat(cli): move user and guild registration from API to local CLI
This commit is contained in:
60
src/cli.ts
Normal file
60
src/cli.ts
Normal file
@@ -0,0 +1,60 @@
|
||||
import 'reflect-metadata';
|
||||
import { NestFactory } from '@nestjs/core';
|
||||
import { AppModule } from './app.module';
|
||||
import { AuthService } from './auth/auth.service';
|
||||
import { NodeAdminService } from './nodes/node-admin.service';
|
||||
|
||||
function getArg(flag: string): string | null {
|
||||
const idx = process.argv.indexOf(flag);
|
||||
if (idx === -1) return null;
|
||||
return process.argv[idx + 1] ?? null;
|
||||
}
|
||||
|
||||
function printUsageAndExit(): never {
|
||||
console.error('Usage:');
|
||||
console.error(' node dist/cli.js user create --email <email> --password <password>');
|
||||
console.error(' node dist/cli.js node register --node-id <id> --name <name> --endpoint <url>');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
async function main() {
|
||||
const [subject, action] = process.argv.slice(2);
|
||||
if (!subject || !action) printUsageAndExit();
|
||||
|
||||
const app = await NestFactory.createApplicationContext(AppModule, { logger: ['error', 'warn'] });
|
||||
try {
|
||||
if (subject === 'user' && action === 'create') {
|
||||
const email = getArg('--email');
|
||||
const password = getArg('--password');
|
||||
if (!email || !password) printUsageAndExit();
|
||||
|
||||
const auth = app.get(AuthService);
|
||||
const user = await auth.register({ email, password });
|
||||
process.stdout.write(JSON.stringify({ ok: true, user }) + '\n');
|
||||
return;
|
||||
}
|
||||
|
||||
if (subject === 'node' && action === 'register') {
|
||||
const nodeId = getArg('--node-id');
|
||||
const name = getArg('--name');
|
||||
const endpoint = getArg('--endpoint');
|
||||
if (!nodeId || !name || !endpoint) printUsageAndExit();
|
||||
|
||||
const nodes = app.get(NodeAdminService);
|
||||
const result = await nodes.registerNode({ nodeId, name, endpoint });
|
||||
process.stdout.write(JSON.stringify({ ok: true, ...result }) + '\n');
|
||||
return;
|
||||
}
|
||||
|
||||
printUsageAndExit();
|
||||
} finally {
|
||||
await app.close();
|
||||
}
|
||||
}
|
||||
|
||||
void main().catch((error: unknown) => {
|
||||
const message = error instanceof Error ? error.message : 'unknown error';
|
||||
process.stderr.write(JSON.stringify({ ok: false, error: message }) + '\n');
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user