-
Notifications
You must be signed in to change notification settings - Fork 458
/
invoke.ts
69 lines (59 loc) 路 1.77 KB
/
invoke.ts
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
import * as fs from 'fs-extra';
import { Flags as flagParser } from '@oclif/core';
import { BaseIPCClientCommand } from '../base_ipc_client';
export class InvokeCommand extends BaseIPCClientCommand {
static description = 'Invokes the provided endpoint.';
static examples = [
'endpoint:invoke {endpoint} {parameters}',
'endpoint:invoke --data-path --file',
`endpoint:invoke generator_getAllKeys`,
`endpoint:invoke consensus_getBFTParameters '{"height": 2}' -d ~/.lisk/pos-mainchain --pretty`,
`endpoint:invoke consensus_getBFTParameters -f ./input.json`,
];
static flags = {
...BaseIPCClientCommand.flags,
file: flagParser.string({
char: 'f',
description: 'Input file.',
}),
};
static args = [
{
name: 'endpoint',
description: 'Endpoint to invoke',
required: true,
},
{
name: 'params',
description: 'Endpoint parameters (Optional)',
required: false,
},
];
async run(): Promise<void> {
const { args, flags } = await this.parse(InvokeCommand);
if (!this._client) {
this.error('APIClient is not initialized.');
}
let response: Record<string, unknown>;
let endpointArguments;
try {
if (flags.file !== undefined) {
if (fs.existsSync(flags.file)) {
endpointArguments = JSON.parse(fs.readFileSync(flags.file, 'utf8')) as {};
} else {
this.error(`${flags.file} does not exist.`);
}
} else if (args.params !== undefined) {
endpointArguments = JSON.parse(args.params) as {};
}
if (endpointArguments === undefined) {
response = await this._client.invoke(args.endpoint);
} else {
response = await this._client.invoke(args.endpoint, endpointArguments);
}
this.printJSON(response as unknown as Record<string, unknown>);
} catch (error) {
this.error((error as Error).message);
}
}
}