-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathcommand.ts
More file actions
63 lines (59 loc) · 2.54 KB
/
command.ts
File metadata and controls
63 lines (59 loc) · 2.54 KB
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
import { DEFAULT_ENVIRONMENT_ID, ENVIRONMENT_IDS, type EnvironmentId, type StreamrClientConfig, DEFAULT_KEY_TYPE } from '@streamr/sdk'
import { KeyType, KEY_TYPES } from '@streamr/utils'
import commander, { Command } from 'commander'
import pkg from '../package.json'
import { createClient } from './client'
import { createFnParseEnum, formEnumArgValueDescription } from './common'
export interface Options {
publicKey?: string
privateKey?: string
keyType?: KeyType
config?: string
env?: EnvironmentId
quantum?: boolean
}
export const createCommand = (): commander.Command => {
return new Command()
.version(pkg.version)
.showHelpAfterError()
.allowExcessArguments(false)
}
export interface CommandOpts {
autoDestroyClient?: boolean
clientOptionsFactory?: (options: any) => StreamrClientConfig
}
export const createClientCommand = (
action: (...handleArgs: any[]) => Promise<void>,
opts: CommandOpts = {
autoDestroyClient: true,
clientOptionsFactory: () => ({})
}
): commander.Command => {
return createCommand()
.option('--private-key <key>', 'a private key to establish your identity')
.option('--key-type [key-type]', `type of public/private key (${formEnumArgValueDescription(KEY_TYPES, DEFAULT_KEY_TYPE)})`,
createFnParseEnum('key-type', KEY_TYPES))
.option('--public-key [public-key]', 'a public key - required by some key types')
.option('--config <file>', 'read connection and identity settings from a config file')
.option('--env <environmentId>', `use pre-defined environment (${formEnumArgValueDescription(ENVIRONMENT_IDS, DEFAULT_ENVIRONMENT_ID)})`,
createFnParseEnum('env', ENVIRONMENT_IDS))
.option('--quantum', 'require quantum resistant key exchange and signature algorithms to be used')
.action(async (...args: any[]) => {
const commandOptions = args[args.length - 1].opts()
try {
const client = createClient(commandOptions, opts.clientOptionsFactory!(commandOptions))
try {
await action(...[client].concat(args))
} finally {
if (opts.autoDestroyClient) {
await client.destroy()
}
}
// Exit cleanly after command completes - worker threads may keep event loop alive
process.exit(0)
} catch (e: any) {
console.error(e)
process.exit(1)
}
})
}