-
Notifications
You must be signed in to change notification settings - Fork 111
/
Copy pathschema.ts
79 lines (69 loc) · 2.68 KB
/
schema.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
77
78
79
import { Flags } from '@oclif/core'
import { SmartThingsClient } from '@smartthings/core-sdk'
import {
APICommand,
outputItemOrList,
OutputItemOrListConfig,
} from '@smartthings/cli-lib'
import { addAppDetails, getSingleInvite, InvitationWithAppDetails, tableFieldDefinitions } from '../../lib/commands/invites-utils'
export default class InvitesSchemaCommand extends APICommand<typeof InvitesSchemaCommand.flags> {
static description = 'list invitations for a schema app'
static flags = {
...APICommand.flags,
...outputItemOrList.flags,
// eslint-disable-next-line @typescript-eslint/naming-convention
'schema-app': Flags.string({
description: 'schema app id',
}),
}
static args = [{
name: 'idOrIndex',
description: 'the invitation id or number in list',
}]
static examples = [
{
description: 'prompt for a schema app and then list its invitations',
command: 'smartthings invites:schema',
},
{
description: 'list invitations for the specified schema app',
command: 'smartthings invites:schema --schema-app=viper_7db10232-3f97-4618-924b-807bf852c616',
},
{
description: 'display details about the third invitation listed in the previous example',
command: 'smartthings invites:schema --schema-app=viper_7db10232-3f97-4618-924b-807bf852c616 3',
},
{
description: 'list details of specified invitation',
command: 'smartthings invites:schema 97e44afd-845f-4da1-a7b3-fd2625fc9367',
},
]
async run(): Promise<void> {
type InvitationProviderFunction = () => Promise<InvitationWithAppDetails[]>
const listFn = (client: SmartThingsClient, appId?: string): InvitationProviderFunction =>
async (): Promise<InvitationWithAppDetails[]> => {
// We have to be careful to not use the method to get a single app. For more
// details see `getSchemaAppEnsuringOrganization` in schema-utils.
const apps = appId
? (await client.schema.list({ includeAllOrganizations: true }))
.filter(app => app.endpointAppId === appId)
: await client.schema.list()
return (await Promise.all(apps.map(async app => {
return app.endpointAppId
? (await client.invitesSchema.list(app.endpointAppId))
.map(invite => addAppDetails(invite, app))
: []
}))).flat()
}
const config: OutputItemOrListConfig<InvitationWithAppDetails> = {
primaryKeyName: 'id',
sortKeyName: 'sort',
itemName: 'schema app invitation',
listTableFieldDefinitions: [{ prop: 'id', label: 'Invitation Id' }, 'schemaAppName', 'description'],
tableFieldDefinitions,
}
await outputItemOrList(this, config, this.args.idOrIndex,
listFn(this.client, this.flags['schema-app']),
id => getSingleInvite(this.client, this.flags['schema-app'], id))
}
}