-
Notifications
You must be signed in to change notification settings - Fork 9
/
set.ts
69 lines (61 loc) · 2.11 KB
/
set.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 {readFile} from 'node:fs/promises'
import {Args, Flags} from '@oclif/core'
import Command from '../../base-command'
import {discoverModemIp, ModemDiscovery} from '../../modem/discovery'
import {modemFactory} from '../../modem/factory'
import {Log} from '../../logger'
import {HostExposureSettings} from '../../modem/modem'
export async function setHostExposureSettings(
settings: HostExposureSettings,
password: string,
logger: Log,
): Promise<HostExposureSettings> {
const modemIp = await discoverModemIp()
const discoveredModem = await new ModemDiscovery(modemIp, logger).discover()
const modem = modemFactory(discoveredModem, logger)
try {
await modem.login(password)
await modem.setHostExposure(settings)
return settings
} catch (error) {
logger.error('Could not get host exposure settings from modem.', error)
throw error
} finally {
await modem.logout()
}
}
export default class SetHostExposure extends Command {
static description = 'Set the current IPV6 host exposure settings from a JSON file'
static examples = [`$ vodafone-station-cli host-exposure:set -p PASSWORD <FILE>`]
static args = {
file: Args.string({
description: 'input JSON file',
required: true,
}),
}
static flags = {
password: Flags.string({
char: 'p',
description: 'router/modem password',
}),
}
async run(): Promise<void> {
const {args, flags} = await this.parse(SetHostExposure)
const password = flags.password ?? process.env.VODAFONE_ROUTER_PASSWORD
if (!password || password === '') {
this.log(
'You must provide a password either using -p or by setting the environment variable VODAFONE_ROUTER_PASSWORD',
)
this.exit()
}
try {
const newSettingsJSON = await readFile(args.file, {encoding: 'utf8'})
const newSettings = JSON.parse(newSettingsJSON) as HostExposureSettings
await setHostExposureSettings(newSettings, password!, this.logger)
this.log('New host exposure settings set.')
} catch (error) {
this.error(error as Error, {message: 'Something went wrong.'})
}
this.exit()
}
}