-
Notifications
You must be signed in to change notification settings - Fork 115
/
Copy pathencodeMessage.ts
36 lines (32 loc) · 1.22 KB
/
encodeMessage.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
import { flags } from '@oclif/command'
import { metadataToBytes } from '../../helpers/serialization'
import { getInputJson } from '../../helpers/InputOutput'
import ExitCodes from '../../ExitCodes'
import ProtobufMessageCommandBase from '../../base/ProtobufMessageCommandBase'
export default class EncodeMessageCommand extends ProtobufMessageCommandBase {
static description = 'Encode a protobuf message (from json to hex)'
static flags = {
...super.flags,
jsonString: flags.string({
required: false,
description: `JSON-encoded message input (eg. '${JSON.stringify({ videoId: 1 })}'`,
exclusive: ['input'],
}),
input: flags.string({
char: 'i',
required: false,
description: `Path to a file containing a JSON-encoded message`,
exclusive: ['jsonString'],
}),
}
async run(): Promise<void> {
const { jsonString, input } = this.parse(EncodeMessageCommand).flags
const jsonObj = input
? await getInputJson(input)
: jsonString
? JSON.parse(jsonString)
: this.error('Either --input or --jsonString must be provided!', { exit: ExitCodes.InvalidInput })
const bytes = metadataToBytes(this.MessageClass, jsonObj)
this.output(bytes.toHex())
}
}