|
| 1 | +import { type ArgumentsCamelCase, type Argv, type CommandModule } from 'yargs' |
| 2 | + |
| 3 | +import { type DeviceProfile } from '@smartthings/core-sdk' |
| 4 | + |
| 5 | +import { fatalError } from '../../lib/util.js' |
| 6 | +import { apiDocsURL } from '../../lib/command/api-command.js' |
| 7 | +import { |
| 8 | + apiOrganizationCommand, |
| 9 | + apiOrganizationCommandBuilder, |
| 10 | + type APIOrganizationCommandFlags, |
| 11 | +} from '../../lib/command/api-organization-command.js' |
| 12 | +import { |
| 13 | + inputAndOutputItem, |
| 14 | + inputAndOutputItemBuilder, |
| 15 | + type InputAndOutputItemFlags, |
| 16 | +} from '../../lib/command/input-and-output-item.js' |
| 17 | +import { type ActionFunction } from '../../lib/command/io-defs.js' |
| 18 | +import { |
| 19 | + buildTableOutput, |
| 20 | + cleanupForUpdate, |
| 21 | + type DeviceDefinitionRequest, |
| 22 | +} from '../../lib/command/util/deviceprofiles-util.js' |
| 23 | +import { chooseDeviceProfile } from '../../lib/command/util/deviceprofiles-choose.js' |
| 24 | + |
| 25 | + |
| 26 | +export type CommandArgs = |
| 27 | + & APIOrganizationCommandFlags |
| 28 | + & InputAndOutputItemFlags |
| 29 | + & { |
| 30 | + idOrIndex?: string |
| 31 | + } |
| 32 | + |
| 33 | +const command = 'deviceprofiles:update [id-or-index]' |
| 34 | + |
| 35 | +const describe = 'update a device profile' |
| 36 | + |
| 37 | +const builder = (yargs: Argv): Argv<CommandArgs> => |
| 38 | + inputAndOutputItemBuilder(apiOrganizationCommandBuilder(yargs)) |
| 39 | + .positional('id-or-index', { describe: 'device profile id or number from list', type: 'string' }) |
| 40 | + .example([ |
| 41 | + [ |
| 42 | + '$0 deviceprofiles:update --input my-profile.yaml', |
| 43 | + 'prompt for a device profile and update it using the data in my-profile.yaml', |
| 44 | + ], |
| 45 | + [ |
| 46 | + '$0 deviceprofiles:update --input my-profile.json 204cf401-bb4b-4dfc-91ec-729f5d8075ef', |
| 47 | + 'update the specified device profile using the data in my-profile.json', |
| 48 | + ], |
| 49 | + ]) |
| 50 | + .epilog(apiDocsURL('updateDeviceProfile')) |
| 51 | + |
| 52 | +const handler = async (argv: ArgumentsCamelCase<CommandArgs>): Promise<void> => { |
| 53 | + const command = await apiOrganizationCommand(argv) |
| 54 | + |
| 55 | + const id = await chooseDeviceProfile(command, argv.idOrIndex) |
| 56 | + const executeUpdate: ActionFunction<void, DeviceDefinitionRequest, DeviceProfile> = async (_, data) => { |
| 57 | + if (data.view) { |
| 58 | + return fatalError('Input contains "view" property. Use deviceprofiles:view:update instead.') |
| 59 | + } |
| 60 | + |
| 61 | + return command.client.deviceProfiles.update(id, cleanupForUpdate(data)) |
| 62 | + } |
| 63 | + await inputAndOutputItem( |
| 64 | + command, |
| 65 | + { buildTableOutput: data => buildTableOutput(command.tableGenerator, data, { includePreferences: true }) }, |
| 66 | + executeUpdate, |
| 67 | + ) |
| 68 | +} |
| 69 | + |
| 70 | +const cmd: CommandModule<object, CommandArgs> = { command, describe, builder, handler } |
| 71 | +export default cmd |
0 commit comments