-
Notifications
You must be signed in to change notification settings - Fork 104
/
locations.ts
59 lines (48 loc) · 1.78 KB
/
locations.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 { Location, LocationItem } from '@smartthings/core-sdk'
import { APICommand, outputItemOrList, OutputItemOrListConfig, selectFromList, SelectFromListConfig, stringTranslateToId, TableFieldDefinition } from '@smartthings/cli-lib'
export const tableFieldDefinitions: TableFieldDefinition<Location>[] = [
'name', 'locationId', 'countryCode', 'timeZoneId', 'backgroundImage',
'latitude', 'longitude', 'regionRadius', 'temperatureScale', 'locale',
]
export async function chooseLocation(
command: APICommand<typeof APICommand.flags>,
locationFromArg?: string,
autoChoose?: boolean,
allowIndex?: boolean): Promise<string> {
const config: SelectFromListConfig<LocationItem> = {
itemName: 'location',
primaryKeyName: 'locationId',
sortKeyName: 'name',
}
const listItems = (): Promise<LocationItem[]> => command.client.locations.list()
const preselectedId = allowIndex
? await stringTranslateToId(config, locationFromArg, listItems)
: locationFromArg
return selectFromList(command, config, {
preselectedId,
autoChoose,
listItems,
})
}
export default class LocationsCommand extends APICommand<typeof LocationsCommand.flags> {
static description = 'list locations or get information for a specific Location' +
this.apiDocsURL('listLocations', 'getLocation')
static flags = {
...APICommand.flags,
...outputItemOrList.flags,
}
static args = [{
name: 'idOrIndex',
description: 'the location id or number in list',
}]
async run(): Promise<void> {
const config: OutputItemOrListConfig<Location, LocationItem> = {
primaryKeyName: 'locationId',
sortKeyName: 'name',
tableFieldDefinitions,
}
await outputItemOrList<Location, LocationItem>(this, config, this.args.idOrIndex,
() => this.client.locations.list(),
id => this.client.locations.get(id))
}
}