diff --git a/packages/edge/src/commands/edge/channels/enroll.ts b/packages/edge/src/commands/edge/channels/enroll.ts deleted file mode 100644 index 18bb1072..00000000 --- a/packages/edge/src/commands/edge/channels/enroll.ts +++ /dev/null @@ -1,36 +0,0 @@ -import { Flags } from '@oclif/core' - -import { chooseChannel } from '../../../lib/commands/channels-util.js' -import { chooseHub } from '../../../lib/commands/drivers-util.js' -import { EdgeCommand } from '../../../lib/edge-command.js' - - -export class ChannelsEnrollCommand extends EdgeCommand { - static description = 'enroll a hub in a channel' - - static flags = { - ...EdgeCommand.flags, - channel: Flags.string({ - char: 'C', - description: 'channel id', - helpValue: '', - }), - } - - static args = [ - { - name: 'hubId', - description: 'hub id', - }, - ] - - async run(): Promise { - const channelId = await chooseChannel(this, 'Select a channel.', this.flags.channel, - { includeReadOnly: true }) - const hubId = await chooseHub(this, 'Select a hub.', this.args.hubId, { useConfigDefault: true }) - - await this.client.channels.enrollHub(channelId, hubId) - - this.log(`${hubId} enrolled in channel ${channelId}`) - } -} diff --git a/packages/edge/src/commands/edge/channels/unenroll.ts b/packages/edge/src/commands/edge/channels/unenroll.ts deleted file mode 100644 index e310d641..00000000 --- a/packages/edge/src/commands/edge/channels/unenroll.ts +++ /dev/null @@ -1,67 +0,0 @@ -import { Flags } from '@oclif/core' - -import { Device } from '@smartthings/core-sdk' - -import { chooseChannel } from '../../../lib/commands/channels-util.js' -import { chooseHub, listOwnedHubs } from '../../../lib/commands/drivers-util.js' -import { EdgeCommand } from '../../../lib/edge-command.js' - - -export const maxHubsToCheckForEnrollments = 15 - -export class ChannelsUnenrollCommand extends EdgeCommand { - static description = 'unenroll a hub from a channel' - - static flags = { - ...EdgeCommand.flags, - channel: Flags.string({ - char: 'C', - description: 'channel id', - helpValue: '', - }), - } - - static args = [ - { - name: 'hubId', - description: 'hub id', - }, - ] - - static examples = [ - { - description: 'prompt user for hub and channel and then unenroll hub from channel', - command: 'smartthings edge:channels:unenroll', - }, - { - description: 'prompt user for a channel the given hub is enrolled in and then unenroll it', - command: 'smartthings edge:channels:unenroll 2fa0912f-cb73-424e-97f9-ffff76ea4f2a', - }, - { - description: 'unenroll the specified hub from the specified channel', - command: 'smartthings edge:channels:unenroll 2fa0912f-cb73-424e-97f9-ffff76ea4f2a --channel fcd6ca9c-2764-4dbb-9bbe-d40c900c960f', - }, - ] - - async run(): Promise { - // A special listItems for `chooseHub` that will only include hubs which are enrolled in channels. - const listItems = async (): Promise => { - const hubs = await listOwnedHubs(this) - const hubsWithChannels = await Promise.all(hubs.map(async hub => - ({ hub, channels: await this.client.hubdevices.enrolledChannels(hub.deviceId) }))) - return hubsWithChannels - .flat() - .filter(({ channels }) => channels.length > 0) - .map(({ hub }) => hub) - } - const hubId = await chooseHub(this, 'Select a hub.', this.args.hubId, - { useConfigDefault: true, listItems }) - - const channelId = await chooseChannel(this, 'Select a channel.', this.flags.channel, - { includeReadOnly: true, listItems: () => this.client.hubdevices.enrolledChannels(hubId) }) - - await this.client.channels.unenrollHub(channelId, hubId) - - this.log(`${hubId} unenrolled from channel ${channelId}`) - } -} diff --git a/src/commands/edge/channels/enroll.ts b/src/commands/edge/channels/enroll.ts new file mode 100644 index 00000000..444b977e --- /dev/null +++ b/src/commands/edge/channels/enroll.ts @@ -0,0 +1,45 @@ +import { type ArgumentsCamelCase, type Argv, type CommandModule } from 'yargs' + +import { apiCommand, apiCommandBuilder, type APICommandFlags } from '../../../lib/command/api-command.js' +import { chooseChannelFn } from '../../../lib/command/util/edge/channels-choose.js' +import { chooseHub } from '../../../lib/command/util/hubs-choose.js' + + +export type CommandArgs = + & APICommandFlags + & { + hubId?: string + channel?: string + } + +const command = 'edge:channels:enroll [hub-id]' + +const describe = 'enroll a hub in a channel' + +const builder = (yargs: Argv): Argv => + apiCommandBuilder(yargs) + .positional('id', { describe: 'hub id', type: 'string' }) + .option('channel', { alias: 'C', describe: 'channel id', type: 'string' }) + .example([ + ['$0 edge:channels:enroll', 'prompt for channel and hub'], + [ + '$0 edge:channels:enroll 8bbc88c2-8e59-4cad-ade2-b57021a2dbfd' + + ' --channel b1a462c1-f63b-442c-8db9-4af7da31f187', + 'enroll the specified hub in the specified channel', + ], + ]) + + +const handler = async (argv: ArgumentsCamelCase): Promise => { + const command = await apiCommand(argv) + + const channelId = await chooseChannelFn({ includeReadOnly: true })( command, argv.channel ) + const hubId = await chooseHub(command, argv.hubId, { useConfigDefault: true }) + + await command.client.channels.enrollHub(channelId, hubId) + + console.log(`${hubId} enrolled in channel ${channelId}`) +} + +const cmd: CommandModule = { command, describe, builder, handler } +export default cmd diff --git a/src/commands/edge/channels/unenroll.ts b/src/commands/edge/channels/unenroll.ts new file mode 100644 index 00000000..3b65636d --- /dev/null +++ b/src/commands/edge/channels/unenroll.ts @@ -0,0 +1,73 @@ +import { type ArgumentsCamelCase, type Argv, type CommandModule } from 'yargs' + +import { type Device } from '@smartthings/core-sdk' + +import { apiCommand, apiCommandBuilder, type APICommandFlags } from '../../../lib/command/api-command.js' + +import { chooseChannelFn } from '../../../lib/command/util/edge/channels-choose.js' +import { listOwnedHubs } from '../../../lib/command/util/hubs.js' +import { chooseHub } from '../../../lib/command/util/hubs-choose.js' + + +export const maxHubsToCheckForEnrollments = 15 + +export type CommandArgs = + & APICommandFlags + & { + hubId?: string + channel?: string + } + +const command = 'edge:channels:unenroll [hub-id]' + +const describe = 'unenroll a hub from a channel' + +const builder = (yargs: Argv): Argv => + apiCommandBuilder(yargs) + .positional('id', { describe: 'hub id', type: 'string' }) + .option('channel', { alias: 'C', describe: 'channel id', type: 'string' }) + .example([ + ['$0 edge:channels:unenroll', 'prompt for channel and hub'], + [ + '$0 edge:channels:unenroll 2fa0912f-cb73-424e-97f9-ffff76ea4f2a', + 'prompt user for a channel the given hub is enrolled in and then unenroll it', + ], + [ + '$0 edge:channels:unenroll 2fa0912f-cb73-424e-97f9-ffff76ea4f2a ' + + '--channel fcd6ca9c-2764-4dbb-9bbe-d40c900c960f', + 'unenroll the specified hub from the specified channel', + ], + ]) + +const handler = async (argv: ArgumentsCamelCase): Promise => { + const command = await apiCommand(argv) + + // A special listItems for `chooseHub` that will only include hubs which are enrolled in channels. + const listItems = async (): Promise => { + const hubs = await listOwnedHubs(command) + const hubsWithChannels = await Promise.all(hubs.map(async hub => + ({ hub, channels: await command.client.hubdevices.enrolledChannels(hub.deviceId) }))) + return hubsWithChannels + .flat() + .filter(({ channels }) => channels.length > 0) + .map(({ hub }) => hub) + } + const hubId = await chooseHub( + command, + argv.hubId, + { useConfigDefault: true, listItems }, + ) + + const channelId = await chooseChannelFn({ includeReadOnly: true })( + command, + argv.channel, + { listItems: () => command.client.hubdevices.enrolledChannels(hubId) }, + ) + + await command.client.channels.unenrollHub(channelId, hubId) + + console.log(`Hub ${hubId} unenrolled from channel ${channelId}.`) +} + +const cmd: CommandModule = { command, describe, builder, handler } +export default cmd diff --git a/src/commands/index.ts b/src/commands/index.ts index e0222fc9..388b8497 100644 --- a/src/commands/index.ts +++ b/src/commands/index.ts @@ -61,6 +61,7 @@ import edgeChannelsAssignCommand from './edge/channels/assign.js' import edgeChannelsCreateCommand from './edge/channels/create.js' import edgeChannelsDeleteCommand from './edge/channels/delete.js' import edgeChannelsDriversCommand from './edge/channels/drivers.js' +import edgeChannelsEnrollCommand from './edge/channels/enroll.js' import edgeChannelsEnrollmentsCommand from './edge/channels/enrollments.js' import edgeChannelsInvitesCommand from './edge/channels/invites.js' import edgeChannelsInvitesAcceptCommand from './edge/channels/invites/accept.js' @@ -68,6 +69,7 @@ import edgeChannelsInvitesCreateCommand from './edge/channels/invites/create.js' import edgeChannelsInvitesDeleteCommand from './edge/channels/invites/delete.js' import edgeChannelsMetaInfoCommand from './edge/channels/metainfo.js' import edgeChannelsUnassignCommand from './edge/channels/unassign.js' +import edgeChannelsUnenrollCommand from './edge/channels/unenroll.js' import edgeDriversCommand from './edge/drivers.js' import edgeDriversDefaultCommand from './edge/drivers/default.js' import edgeDriversDeleteCommand from './edge/drivers/delete.js' @@ -191,6 +193,7 @@ export const commands: CommandModule[] = [ edgeChannelsCreateCommand, edgeChannelsDeleteCommand, edgeChannelsDriversCommand, + edgeChannelsEnrollCommand, edgeChannelsEnrollmentsCommand, edgeChannelsInvitesCommand, edgeChannelsInvitesAcceptCommand, @@ -198,6 +201,7 @@ export const commands: CommandModule[] = [ edgeChannelsInvitesDeleteCommand, edgeChannelsMetaInfoCommand, edgeChannelsUnassignCommand, + edgeChannelsUnenrollCommand, edgeDriversCommand, edgeDriversDefaultCommand, edgeDriversDeleteCommand,