diff --git a/packages/cli/src/commands/deviceprofiles/update.ts b/packages/cli/src/commands/deviceprofiles/update.ts deleted file mode 100644 index 4c275118..00000000 --- a/packages/cli/src/commands/deviceprofiles/update.ts +++ /dev/null @@ -1,42 +0,0 @@ -import { Errors } from '@oclif/core' - -import { DeviceProfile } from '@smartthings/core-sdk' - -import { ActionFunction, APIOrganizationCommand, inputAndOutputItem } from '@smartthings/cli-lib' - -import { - buildTableOutput, - chooseDeviceProfile, - cleanupForUpdate, - DeviceDefinitionRequest, -} from '../../lib/commands/deviceprofiles-util.js' - - -export default class DeviceProfileUpdateCommand extends APIOrganizationCommand { - static description = 'update a device profile' + - this.apiDocsURL('updateDeviceProfile') - - static flags = { - ...APIOrganizationCommand.flags, - ...inputAndOutputItem.flags, - } - - static args = [{ - name: 'id', - description: 'device profile UUID or number in the list', - }] - - async run(): Promise { - const id = await chooseDeviceProfile(this, this.args.id) - const executeUpdate: ActionFunction = async (_, data) => { - if (data.view) { - throw new Errors.CLIError('Input contains "view" property. Use deviceprofiles:view:update instead.') - } - - return this.client.deviceProfiles.update(id, cleanupForUpdate(data)) - } - await inputAndOutputItem(this, { - buildTableOutput: data => buildTableOutput(this.tableGenerator, data, { includePreferences: true }), - }, executeUpdate) - } -} diff --git a/src/commands/deviceprofiles/update.ts b/src/commands/deviceprofiles/update.ts new file mode 100644 index 00000000..d190af55 --- /dev/null +++ b/src/commands/deviceprofiles/update.ts @@ -0,0 +1,71 @@ +import { type ArgumentsCamelCase, type Argv, type CommandModule } from 'yargs' + +import { type DeviceProfile } from '@smartthings/core-sdk' + +import { fatalError } from '../../lib/util.js' +import { apiDocsURL } from '../../lib/command/api-command.js' +import { + apiOrganizationCommand, + apiOrganizationCommandBuilder, + type APIOrganizationCommandFlags, +} from '../../lib/command/api-organization-command.js' +import { + inputAndOutputItem, + inputAndOutputItemBuilder, + type InputAndOutputItemFlags, +} from '../../lib/command/input-and-output-item.js' +import { type ActionFunction } from '../../lib/command/io-defs.js' +import { + buildTableOutput, + cleanupForUpdate, + type DeviceDefinitionRequest, +} from '../../lib/command/util/deviceprofiles-util.js' +import { chooseDeviceProfile } from '../../lib/command/util/deviceprofiles-choose.js' + + +export type CommandArgs = + & APIOrganizationCommandFlags + & InputAndOutputItemFlags + & { + idOrIndex?: string + } + +const command = 'deviceprofiles:update [id-or-index]' + +const describe = 'update a device profile' + +const builder = (yargs: Argv): Argv => + inputAndOutputItemBuilder(apiOrganizationCommandBuilder(yargs)) + .positional('id-or-index', { describe: 'device profile id or number from list', type: 'string' }) + .example([ + [ + '$0 deviceprofiles:update --input my-profile.yaml', + 'prompt for a device profile and update it using the data in my-profile.yaml', + ], + [ + '$0 deviceprofiles:update --input my-profile.json 204cf401-bb4b-4dfc-91ec-729f5d8075ef', + 'update the specified device profile using the data in my-profile.json', + ], + ]) + .epilog(apiDocsURL('updateDeviceProfile')) + +const handler = async (argv: ArgumentsCamelCase): Promise => { + const command = await apiOrganizationCommand(argv) + + const id = await chooseDeviceProfile(command, argv.idOrIndex) + const executeUpdate: ActionFunction = async (_, data) => { + if (data.view) { + return fatalError('Input contains "view" property. Use deviceprofiles:view:update instead.') + } + + return command.client.deviceProfiles.update(id, cleanupForUpdate(data)) + } + await inputAndOutputItem( + command, + { buildTableOutput: data => buildTableOutput(command.tableGenerator, data, { includePreferences: true }) }, + executeUpdate, + ) +} + +const cmd: CommandModule = { command, describe, builder, handler } +export default cmd diff --git a/src/commands/index.ts b/src/commands/index.ts index 45737617..af93ff4b 100644 --- a/src/commands/index.ts +++ b/src/commands/index.ts @@ -38,6 +38,7 @@ import deviceprofilesViewCommand from './deviceprofiles/view.js' import deviceprofilesTranslations from './deviceprofiles/translations.js' import deviceprofilesTranslationsDelete from './deviceprofiles/translations/delete.js' import deviceprofilesTranslationsUpsertCommand from './deviceprofiles/translations/upsert.js' +import deviceprofilesUpdateCommand from './deviceprofiles/update.js' import deviceprofilesViewCreateCommand from './deviceprofiles/view/create.js' import deviceprofilesViewUpdateCommand from './deviceprofiles/view/update.js' import devicesCommand from './devices.js' @@ -153,6 +154,7 @@ export const commands: CommandModule[] = [ deviceprofilesTranslations, deviceprofilesTranslationsDelete, deviceprofilesTranslationsUpsertCommand, + deviceprofilesUpdateCommand, deviceprofilesViewCreateCommand, deviceprofilesViewUpdateCommand, devicesCommand,