Skip to content

Commit 34e7560

Browse files
committed
chore: add cli to create identity
1 parent 7ac5965 commit 34e7560

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

src/cli/commands/identity/init.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { AccountController } from "@nmshd/transport";
2+
import { CommandModule } from "yargs";
3+
import { BaseCommand, ConfigFileOptions, configOptionBuilder } from "../../BaseCommand";
4+
5+
export const identityInitHandler = async ({ config }: ConfigFileOptions): Promise<void> => {
6+
await new IdentityInit().run(config);
7+
};
8+
export const yargsIdentityInitCommand: CommandModule<{}, ConfigFileOptions> = {
9+
command: "init",
10+
describe: "initialize the identity",
11+
handler: identityInitHandler,
12+
builder: configOptionBuilder
13+
};
14+
15+
export default class IdentityInit extends BaseCommand {
16+
public async runInternal(): Promise<{ message: string }> {
17+
if (!this.transport || !this.connectorConfig) {
18+
throw new Error("Transport or connectorConfig not initialized");
19+
}
20+
const db = await this.transport.createDatabase(`${this.connectorConfig.database.dbNamePrefix}${this.connectorConfig.database.dbName}`);
21+
const identityCollection = await db.getMap("AccountInfo");
22+
const identity = await identityCollection.get("identity");
23+
if (identity) {
24+
this.log.log("Identity already created!");
25+
return { message: "Identity already created!" };
26+
}
27+
const accountController = new AccountController(this.transport, db, this.transport.config);
28+
await accountController.init();
29+
30+
this.log.log("Identity created successfully!");
31+
return { message: "Identity created successfully!" };
32+
}
33+
}

src/cli/index.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,20 @@
22

33
import yargs from "yargs";
44
import { configOptionBuilder } from "./BaseCommand";
5+
import { yargsIdentityInitCommand } from "./commands/identity/init";
56
import { startConnectorHandler } from "./commands/start";
67

78
const argv = yargs(process.argv.slice(2))
9+
.command({
10+
command: "identity [command]",
11+
describe: "identity related commands ",
12+
builder: (yargs) => {
13+
return yargs.command(yargsIdentityInitCommand);
14+
},
15+
handler: () => {
16+
yargs.showHelp();
17+
}
18+
})
819
.command({
920
command: "start",
1021
describe: "start the connector",

0 commit comments

Comments
 (0)