-
Notifications
You must be signed in to change notification settings - Fork 131
Expand file tree
/
Copy pathdevicepreferences.ts
More file actions
84 lines (73 loc) · 3.5 KB
/
devicepreferences.ts
File metadata and controls
84 lines (73 loc) · 3.5 KB
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
77
78
79
80
81
82
83
84
import { Flags } from '@oclif/core'
import { DevicePreference } from '@smartthings/core-sdk'
import { APIOrganizationCommand, outputItemOrList, allOrganizationsFlags, forAllOrganizations, OutputItemOrListConfig } from '@smartthings/cli-lib'
import { tableFieldDefinitions } from '../lib/commands/devicepreferences-util'
export async function standardPreferences(command: APIOrganizationCommand<typeof APIOrganizationCommand.flags>): Promise<DevicePreference[]> {
return (await command.client.devicePreferences.list())
.filter(preference => preference.preferenceId.split('.').length === 1)
}
export async function customPreferences(command: APIOrganizationCommand<typeof APIOrganizationCommand.flags>): Promise<DevicePreference[]> {
return (await command.client.devicePreferences.list())
.filter(preference => preference.preferenceId.split('.').length > 1)
}
export async function preferencesForAllOrganizations(command: APIOrganizationCommand<typeof APIOrganizationCommand.flags>): Promise<DevicePreference[]> {
return forAllOrganizations(command.client, (orgClient, org) => {
// TODO - Once it is possible to create device preferences in namespaces other than the
// organization's default one, we should restore this logic
// return forAllNamespaces(orgClient, (namespace) => {
// return orgClient.devicePreferences.list(namespace.name)
// })
return orgClient.devicePreferences.list(org.name)
})
}
export default class DevicePreferencesCommand extends APIOrganizationCommand<typeof DevicePreferencesCommand.flags> {
static description = 'list device preferences or get information for a specific device preference'
static flags = {
...APIOrganizationCommand.flags,
...outputItemOrList.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 device preferences',
}),
}
static args = [
{ name: 'idOrIndex', description: 'device preference id or index' },
]
static aliases = ['device-preferences']
static examples = [
'$ smartthings devicepreferences # list all device preferences, sorted by title',
'$ smartthings devicepreferences device-preference-id # display details for preference with specified id',
'$ smartthings devicepreferences 2 # display details for second preference when sorted by title',
'$ smartthings devicepreferences 3 -y # display details for third preference as YAML',
'$ smartthings devicepreferences 3 -o dp.json # write details as JSON for third preference to dp.json',
]
async run(): Promise<void> {
const listTableFieldDefinitions = ['preferenceId', 'title', 'name', 'description', 'required', 'preferenceType']
const config: OutputItemOrListConfig<DevicePreference> = {
itemName: 'device preference',
primaryKeyName: 'preferenceId',
sortKeyName: 'preferenceId',
tableFieldDefinitions,
listTableFieldDefinitions,
}
await outputItemOrList(this, config, this.args.idOrIndex,
async () => {
if (this.flags.standard) {
return standardPreferences(this)
} else if (this.flags.namespace) {
return this.client.devicePreferences.list(this.flags.namespace)
}
else if (this.flags['all-organizations']) {
listTableFieldDefinitions.push('organization')
return preferencesForAllOrganizations(this)
}
return customPreferences(this)
},
id => this.client.devicePreferences.get(id))
}
}