-
Notifications
You must be signed in to change notification settings - Fork 112
/
Copy pathgenerate.ts
61 lines (50 loc) · 2.14 KB
/
generate.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
import { GenerateAppOAuthRequest, GenerateAppOAuthResponse } from '@smartthings/core-sdk'
import { APICommand, inputAndOutputItem, InputAndOutputItemConfig, inputProcessor, objectDef, stringDef, TableFieldDefinition, updateFromUserInput } from '@smartthings/cli-lib'
import { chooseApp, oauthAppScopeDef } from '../../../lib/commands/apps-util'
export default class AppOauthGenerateCommand extends APICommand<typeof AppOauthGenerateCommand.flags> {
static docNames = 'generateAppOauth'
static description = 'regenerate the OAuth clientId and clientSecret of an app' +
this.apiDocsURL(AppOauthGenerateCommand.docNames)
static flags = {
...APICommand.flags,
...inputAndOutputItem.flags,
}
static args = [{
name: 'id',
description: 'the app id',
}]
static examples = [
{
description: 'regenerate the OAuth clientId and clientSecret of the app with the given id',
command: 'smartthings apps:oauth:generate 392bcb11-e251-44f3-b58b-17f93015f3aa',
},
]
async run(): Promise<void> {
const appId = await chooseApp(this, this.args.id)
const tableFieldDefinitions: TableFieldDefinition<GenerateAppOAuthResponse>[] = [
{ path: 'oauthClientDetails.clientName' },
{ path: 'oauthClientDetails.scope' },
{ path: 'oauthClientDetails.redirectUris' },
'oauthClientId',
'oauthClientSecret',
]
const config: InputAndOutputItemConfig<GenerateAppOAuthResponse> = {
tableFieldDefinitions,
}
const getInputFromUser = async (): Promise<GenerateAppOAuthRequest> => {
const origOauth = await this.client.apps.getOauth(appId)
const startingRequest: GenerateAppOAuthRequest = {
clientName: origOauth.clientName,
scope: origOauth.scope,
}
const inputDef = objectDef('Generate Request', {
clientName: stringDef('Client Name'),
scope: oauthAppScopeDef,
}, { helpText: APICommand.itemInputHelpText(AppOauthGenerateCommand.docNames) })
return updateFromUserInput(this, inputDef, startingRequest, { dryRun: this.flags['dry-run'] })
}
await inputAndOutputItem(this, config,
(_, data: GenerateAppOAuthRequest) => this.client.apps.regenerateOauth(appId, data),
inputProcessor(() => true, getInputFromUser))
}
}