-
Notifications
You must be signed in to change notification settings - Fork 112
/
Copy pathcapabilities.ts
70 lines (62 loc) · 2.29 KB
/
capabilities.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
import { Flags } from '@oclif/core'
import { Capability } from '@smartthings/core-sdk'
import {
APIOrganizationCommand,
outputItemOrListGeneric,
allOrganizationsFlags,
forAllOrganizations,
OutputItemOrListConfig,
WithOrganization,
} from '@smartthings/cli-lib'
import {
buildTableOutput,
CapabilityId,
capabilityIdOrIndexInputArgs,
CapabilitySummaryWithNamespace,
getCustomByNamespace,
getStandard,
translateToId,
} from '../lib/commands/capabilities-util'
export default class CapabilitiesCommand extends APIOrganizationCommand<typeof CapabilitiesCommand.flags> {
static description = 'get a specific capability or a list of capabilities' +
this.apiDocsURL('listNamespacedCapabilities', 'listCapabilities', 'getCapability')
static flags = {
...APIOrganizationCommand.flags,
...outputItemOrListGeneric.flags,
...allOrganizationsFlags,
namespace: Flags.string({
char: 'n',
description: 'a specific namespace to query; will use all by default',
}),
standard: Flags.boolean({
char: 's',
description: 'show standard SmartThings capabilities',
}),
}
static args = capabilityIdOrIndexInputArgs
async run(): Promise<void> {
const idOrIndex = this.args.version ? { id: this.args.id, version: this.args.version } : this.args.id
const sortKeyName = 'id'
const config: OutputItemOrListConfig<Capability, CapabilitySummaryWithNamespace & WithOrganization> = {
primaryKeyName: 'id',
sortKeyName,
itemName: 'capability',
pluralItemName: 'capabilities',
listTableFieldDefinitions: ['id', 'version', 'status'],
buildTableOutput: (data: Capability) => buildTableOutput(this.tableGenerator, data),
}
const listItems = (): Promise<(CapabilitySummaryWithNamespace & WithOrganization)[]> => {
if (this.flags.standard) {
return getStandard(this.client)
} else if (this.flags['all-organizations']) {
config.listTableFieldDefinitions.push('organization')
return forAllOrganizations(this.client, orgClient => getCustomByNamespace(orgClient, this.flags.namespace))
}
return getCustomByNamespace(this.client, this.flags.namespace)
}
await outputItemOrListGeneric(this, config, idOrIndex,
listItems,
(id: CapabilityId) => this.client.capabilities.get(id.id, id.version),
(idOrIndex, listFunction) => translateToId(sortKeyName, idOrIndex, listFunction))
}
}