forked from SmartThingsCommunity/smartthings-cli
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathupdate.ts
More file actions
79 lines (66 loc) · 2.67 KB
/
update.ts
File metadata and controls
79 lines (66 loc) · 2.67 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
import { type ArgumentsCamelCase, type Argv, type CommandModule } from 'yargs'
import { AppOAuthRequest } from '@smartthings/core-sdk'
import { itemInputHelpText } from '../../../lib/help.js'
import {
apiCommand,
apiCommandBuilder,
APICommandFlags,
apiDocsURL,
} from '../../../lib/command/api-command.js'
import {
inputAndOutputItem,
inputAndOutputItemBuilder,
InputAndOutputItemFlags,
} from '../../../lib/command/input-and-output-item.js'
import { inputProcessor } from '../../../lib/command/input-processor.js'
import { chooseApp, oauthTableFieldDefinitions } from '../../../lib/command/util/apps-util.js'
import {
oauthAppScopeDef,
redirectUrisDef,
} from '../../../lib/command/util/apps-input-primitives.js'
import { objectDef, stringDef, updateFromUserInput } from '../../../lib/item-input/index.js'
export type CommandArgs = APICommandFlags & InputAndOutputItemFlags & {
id?: string
}
const command = 'apps:oauth:update [id]'
const describe = 'update the OAuth settings of an app'
const docNames = 'updateAppOauth'
const builder = (yargs: Argv): Argv<CommandArgs> =>
inputAndOutputItemBuilder(apiCommandBuilder(yargs))
.positional('id', { describe: 'app id', type: 'string' })
.example([
[
'$0 apps:oauth:update',
'prompt for an app and update its OAuth settings interactively"',
],
[
'$0 apps:oauth:update -i oauth-settings.json',
'prompt for an app and update its OAuth settings using the data in "oauth-settings.json',
],
[
'$0 apps:oauth:update 392bcb11-e251-44f3-b58b-17f93015f3aa -i oauth-settings.json',
'update OAuth settings for the app with the given id using the data in "oauth-settings.json"',
],
])
.epilog(apiDocsURL(docNames))
const handler = async (argv: ArgumentsCamelCase<CommandArgs>): Promise<void> => {
const command = await apiCommand(argv)
const appId = await chooseApp(command, argv.id)
const getInputFromUser = async (): Promise<AppOAuthRequest> => {
const startingRequest: AppOAuthRequest = await command.client.apps.getOauth(appId)
if (!startingRequest.scope) {
startingRequest.scope = []
}
const inputDef = objectDef('OAuth Settings', {
clientName: stringDef('Client Name'),
scope: oauthAppScopeDef,
redirectUris: redirectUrisDef,
}, { helpText: itemInputHelpText(docNames) })
return updateFromUserInput(command, inputDef, startingRequest, { dryRun: !!argv.dryRun })
}
await inputAndOutputItem(command, { tableFieldDefinitions: oauthTableFieldDefinitions },
(_, data: AppOAuthRequest) => command.client.apps.updateOauth(appId, data),
inputProcessor(() => true, getInputFromUser))
}
const cmd: CommandModule<object, CommandArgs> = { command, describe, builder, handler }
export default cmd