Skip to content

Commit acbb01e

Browse files
committed
refactor: convert edge:channels:assign command to yargs
1 parent 9088df0 commit acbb01e

3 files changed

Lines changed: 66 additions & 44 deletions

File tree

packages/edge/src/commands/edge/channels/assign.ts

Lines changed: 0 additions & 44 deletions
This file was deleted.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { type ArgumentsCamelCase, type Argv, type CommandModule } from 'yargs'
2+
3+
import { apiCommand, apiCommandBuilder, apiDocsURL, type APICommandFlags } from '../../../lib/command/api-command.js'
4+
5+
import { chooseChannel } from '../../../lib/command/util/edge/channels-choose.js'
6+
import { chooseDriver } from '../../../lib/command/util/drivers-choose.js'
7+
8+
9+
export type CommandArgs =
10+
& APICommandFlags
11+
& {
12+
driverId?: string
13+
driverVersion?: string
14+
channel?: string
15+
}
16+
17+
const command = 'edge:channels:assign [driver-id] [driver-version]'
18+
19+
const describe = 'assign a driver to a channel'
20+
21+
const builder = (yargs: Argv): Argv<CommandArgs> =>
22+
apiCommandBuilder(yargs)
23+
.positional('driver-id', { describe: 'driver id', type: 'string' })
24+
.positional('driver-version', { describe: 'driver version', type: 'string' })
25+
.option('channel', { alias: 'C', describe: 'channel to assigned to', type: 'string' })
26+
.example([
27+
['$0 edge:channels:assign', 'prompt for a channel and driver'],
28+
[
29+
'$0 edge:channels:assign 636169e4-8b9f-4438-a941-953b0d617231',
30+
'prompt for a channel and assign the specified driver to it',
31+
],
32+
[
33+
'$0 edge:channels:assign --channel 6ea857c6-23d4-4aae-aaf7-7a36daf42f92',
34+
'prompt for a driver and assign it to the specified channel',
35+
],
36+
[
37+
'$0 edge:channels:assign 636169e4-8b9f-4438-a941-953b0d617231' +
38+
' --channel 6ea857c6-23d4-4aae-aaf7-7a36daf42f92',
39+
'assign the specified driver to the specified channel',
40+
],
41+
])
42+
.epilog(apiDocsURL('createDriverChannel'))
43+
44+
45+
const handler = async (argv: ArgumentsCamelCase<CommandArgs>): Promise<void> => {
46+
const command = await apiCommand(argv)
47+
48+
const channelId = await chooseChannel(
49+
command,
50+
argv.channel,
51+
{ promptMessage: 'Select a channel for the driver.', useConfigDefault: true },
52+
)
53+
const driverId = await chooseDriver(command, argv.driverId, { promptMessage: 'Select a driver to assign.' })
54+
55+
// If the version wasn't specified, grab it from the driver.
56+
const driverVersion = argv.driverVersion ?? (await command.client.drivers.get(driverId)).version
57+
58+
await command.client.channels.assignDriver(channelId, driverId, driverVersion)
59+
60+
console.log(`${driverId}${argv.version ? ` (version ${argv.version})` : ''} assigned to channel ${channelId}`)
61+
}
62+
63+
const cmd: CommandModule<object, CommandArgs> = { command, describe, builder, handler }
64+
export default cmd

src/commands/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ import devicesRenameCommand from './devices/rename.js'
5757
import devicesStatusCommand from './devices/status.js'
5858
import devicesUpdateCommand from './devices/update.js'
5959
import edgeChannelsCommand from './edge/channels.js'
60+
import edgeChannelsAssignCommand from './edge/channels/assign.js'
6061
import edgeChannelsCreateCommand from './edge/channels/create.js'
6162
import edgeChannelsDeleteCommand from './edge/channels/delete.js'
6263
import edgeChannelsDriversCommand from './edge/channels/drivers.js'
@@ -181,6 +182,7 @@ export const commands: CommandModule<object, any>[] = [
181182
devicesStatusCommand,
182183
devicesUpdateCommand,
183184
edgeChannelsCommand,
185+
edgeChannelsAssignCommand,
184186
edgeChannelsCreateCommand,
185187
edgeChannelsDeleteCommand,
186188
edgeChannelsDriversCommand,

0 commit comments

Comments
 (0)