diff --git a/packages/cli/src/commands/deviceprofiles/device-config.ts b/packages/cli/src/commands/deviceprofiles/device-config.ts deleted file mode 100644 index e0092927..00000000 --- a/packages/cli/src/commands/deviceprofiles/device-config.ts +++ /dev/null @@ -1,33 +0,0 @@ -import { Errors } from '@oclif/core' - -import { APIOrganizationCommand, formatAndWriteItem } from '@smartthings/cli-lib' - -import { buildTableOutput } from '../presentation/device-config.js' -import { chooseDeviceProfile } from '../../lib/commands/deviceprofiles-util.js' - - -export default class DeviceProfileDeviceConfigCommand extends APIOrganizationCommand { - static description = 'get the device configuration associated with a device profile' + - this.apiDocsURL('getDeviceProfile', 'getDeviceConfiguration') - - static flags = { - ...APIOrganizationCommand.flags, - ...formatAndWriteItem.flags, - } - - static args = [{ - name: 'id', - description: 'device profile id or the number in list', - }] - - async run(): Promise { - const id = await chooseDeviceProfile(this, this.args.id, { allowIndex: true }) - - const profile = await this.client.deviceProfiles.get(id) - if (!profile.metadata) { - throw new Errors.CLIError('No presentation defined for device profile') - } - const deviceConfig = await this.client.presentation.get(profile.metadata.vid, profile.metadata.mnmn) - await formatAndWriteItem(this, { buildTableOutput: data => buildTableOutput(this.tableGenerator, data) }, deviceConfig) - } -} diff --git a/src/commands/deviceprofiles/device-config.ts b/src/commands/deviceprofiles/device-config.ts new file mode 100644 index 00000000..d721dc42 --- /dev/null +++ b/src/commands/deviceprofiles/device-config.ts @@ -0,0 +1,68 @@ +import { type ArgumentsCamelCase, type Argv, type CommandModule } from 'yargs' + +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 { + formatAndWriteItem, + formatAndWriteItemBuilder, + type FormatAndWriteItemFlags, +} from '../../lib/command/format.js' +import { chooseDeviceProfile } from '../../lib/command/util/deviceprofiles-choose.js' +import { buildTableOutput } from '../../lib/command/util/presentation-device-config-table.js' + + +export type CommandArgs = + & APIOrganizationCommandFlags + & FormatAndWriteItemFlags + & { + idOrIndex?: string + } + +const command = 'deviceprofiles:device-config [id-or-index]' + +const describe = 'get the device configuration associated with a device profile' + +export const builder = (yargs: Argv): Argv => + formatAndWriteItemBuilder(apiOrganizationCommandBuilder(yargs)) + .positional('id-or-index', { describe: 'device profile id or number from list', type: 'string' }) + .example([ + [ + '$0 deviceprofiles:device-config', + 'prompt for a device profile and display its device config', + ], + [ + '$0 deviceprofiles:device-config 2', + 'display device config for the second device profile listed when running' + + ' "smartthings deviceprofiles"', + ], + [ + '$0 deviceprofiles:device-config c4cb671a-c538-45fa-b076-24b2616181de', + 'display device config for the specified device profile', + ], + ]) + .epilog(apiDocsURL('getDeviceProfile', 'getDeviceConfiguration')) + +const handler = async (argv: ArgumentsCamelCase): Promise => { + const command = await apiOrganizationCommand(argv) + + const id = await chooseDeviceProfile(command, argv.idOrIndex, { allowIndex: true }) + + const profile = await command.client.deviceProfiles.get(id) + if (!profile.metadata) { + return fatalError('No presentation defined for device profile') + } + const deviceConfig = await command.client.presentation.get(profile.metadata.vid, profile.metadata.mnmn) + await formatAndWriteItem( + command, + { buildTableOutput: data => buildTableOutput(command.tableGenerator, data) }, + deviceConfig, + ) +} + +const cmd: CommandModule = { command, describe, builder, handler } +export default cmd diff --git a/src/commands/index.ts b/src/commands/index.ts index 337e3b0c..1dafeaed 100644 --- a/src/commands/index.ts +++ b/src/commands/index.ts @@ -33,6 +33,7 @@ import devicepreferencesTranslationsCreateCommand from './devicepreferences/tran import devicepreferencesTranslationsUpdateCommand from './devicepreferences/translations/update.js' import deviceprofilesCommand from './deviceprofiles.js' import deviceprofilesCreateCommand from './deviceprofiles/create.js' +import deviceprofilesDeviceConfigCommand from './deviceprofiles/device-config.js' import deviceprofilesPresentationCommand from './deviceprofiles/presentation.js' import deviceprofilesPublishCommand from './deviceprofiles/publish.js' import deviceprofilesViewCommand from './deviceprofiles/view.js' @@ -150,6 +151,7 @@ export const commands: CommandModule[] = [ devicepreferencesTranslationsUpdateCommand, deviceprofilesCommand, deviceprofilesCreateCommand, + deviceprofilesDeviceConfigCommand, deviceprofilesPresentationCommand, deviceprofilesPublishCommand, deviceprofilesViewCommand,