-
Notifications
You must be signed in to change notification settings - Fork 111
/
Copy pathrooms.ts
59 lines (51 loc) · 1.82 KB
/
rooms.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
import { Flags } from '@oclif/core'
import { Room } from '@smartthings/core-sdk'
import { APICommand, OutputItemOrListConfig, outputItemOrList, withLocation, WithNamedLocation } from '@smartthings/cli-lib'
import { getRoomsByLocation, tableFieldDefinitions, tableFieldDefinitionsWithLocationName } from '../../lib/commands/locations/rooms-util'
export default class RoomsCommand extends APICommand<typeof RoomsCommand.flags> {
static description = 'list rooms or get information for a specific room' +
this.apiDocsURL('listRooms', 'getRoom')
static flags = {
...APICommand.flags,
location: Flags.string({
char: 'l',
description: 'a specific location to query',
helpValue: '<UUID>',
}),
verbose: Flags.boolean({
description: 'include location name in output',
char: 'v',
}),
...outputItemOrList.flags,
}
static args = [{
name: 'idOrIndex',
description: 'room UUID or index',
}]
async run(): Promise<void> {
const config: OutputItemOrListConfig<Room & WithNamedLocation> = {
primaryKeyName: 'roomId',
sortKeyName: 'name',
listTableFieldDefinitions: tableFieldDefinitions,
tableFieldDefinitions: tableFieldDefinitions,
}
if (this.flags.verbose) {
config.listTableFieldDefinitions = tableFieldDefinitionsWithLocationName
config.tableFieldDefinitions = tableFieldDefinitionsWithLocationName
}
const rooms = await getRoomsByLocation(this.client, this.flags.location)
await outputItemOrList(this, config, this.args.idOrIndex,
async () => rooms,
async id => {
const room = rooms.find(room => room.roomId === id)
if (!room) {
throw Error(`could not find room with id ${id}`)
}
const chosenRoom = await this.client.rooms.get(id, room.locationId)
if (this.flags.verbose) {
return await withLocation(this.client, chosenRoom)
}
return chosenRoom
})
}
}