This repository has been archived by the owner on Sep 22, 2022. It is now read-only.
/
unassign.ts
76 lines (62 loc) · 2.33 KB
/
unassign.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
70
71
72
73
74
75
76
import { Flags } from '@oclif/core'
import { DriverChannelDetails } from '@smartthings/core-sdk'
import { APICommand, ChooseOptions, chooseOptionsWithDefaults, selectFromList, stringTranslateToId } from '@smartthings/cli-lib'
import { chooseChannel } from '../../../lib/commands/channels-util'
import { EdgeCommand } from '../../../lib/edge-command'
export interface NamedDriverChannelDetails extends DriverChannelDetails {
name: string
}
export async function chooseAssignedDriver(command: APICommand, promptMessage: string,
channelId: string, commandLineDriverId?: string, options?: Partial<ChooseOptions>): Promise<string> {
const opts = chooseOptionsWithDefaults(options)
const config = {
itemName: 'driver',
primaryKeyName: 'driverId',
sortKeyName: 'name',
}
const listDrivers = async (): Promise<NamedDriverChannelDetails[]> => {
const driverDetails = await command.client.channels.listAssignedDrivers(channelId)
return (await Promise.all(driverDetails.map(async details => {
try {
const driver = await command.client.drivers.get(details.driverId)
return { ...details, name: driver.name }
} catch (error) {
if (error.response?.status === 404) {
return { ...details, name: '<deleted driver>' }
}
throw error
}
})))
}
const preselectedId = opts.allowIndex
? await stringTranslateToId(config, commandLineDriverId, listDrivers)
: commandLineDriverId
return selectFromList(command, config, preselectedId, listDrivers, promptMessage)
}
export class ChannelsUnassignCommand extends EdgeCommand {
static description = 'remove a driver from a channel'
static flags = {
...EdgeCommand.flags,
'channel': Flags.string({
char: 'C',
description: 'channel id',
}),
}
static args = [
{
name: 'driverId',
description: 'driver id',
},
]
static aliases = ['edge:drivers:unpublish']
async run(): Promise<void> {
const { args, argv, flags } = await this.parse(ChannelsUnassignCommand)
await super.setup(args, argv, flags)
const channelId = await chooseChannel(this, 'Select a channel for the driver.',
flags.channel, this.defaultChannelId)
const driverId = await chooseAssignedDriver(this, 'Select a driver to remove from channel.',
channelId, args.driverId)
await this.client.channels.unassignDriver(channelId, driverId)
this.log(`${driverId} removed from channel ${channelId}`)
}
}