Skip to content

Commit edf147b

Browse files
committed
refactor: convert deviceprofiles:update command to yargs
1 parent 6f4b34d commit edf147b

File tree

3 files changed

+73
-42
lines changed

3 files changed

+73
-42
lines changed

packages/cli/src/commands/deviceprofiles/update.ts

Lines changed: 0 additions & 42 deletions
This file was deleted.

src/commands/deviceprofiles/update.ts

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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

src/commands/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import deviceprofilesViewCommand from './deviceprofiles/view.js'
3838
import deviceprofilesTranslations from './deviceprofiles/translations.js'
3939
import deviceprofilesTranslationsDelete from './deviceprofiles/translations/delete.js'
4040
import deviceprofilesTranslationsUpsertCommand from './deviceprofiles/translations/upsert.js'
41+
import deviceprofilesUpdateCommand from './deviceprofiles/update.js'
4142
import deviceprofilesViewCreateCommand from './deviceprofiles/view/create.js'
4243
import deviceprofilesViewUpdateCommand from './deviceprofiles/view/update.js'
4344
import devicesCommand from './devices.js'
@@ -153,6 +154,7 @@ export const commands: CommandModule<object, any>[] = [
153154
deviceprofilesTranslations,
154155
deviceprofilesTranslationsDelete,
155156
deviceprofilesTranslationsUpsertCommand,
157+
deviceprofilesUpdateCommand,
156158
deviceprofilesViewCreateCommand,
157159
deviceprofilesViewUpdateCommand,
158160
devicesCommand,

0 commit comments

Comments
 (0)