|
| 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 |
0 commit comments