From 63e9c8bb5a25a4987ffdb765942b6ffd965d070e Mon Sep 17 00:00:00 2001 From: Michelle Staley <51251946+Sitlintac@users.noreply.github.com> Date: Thu, 7 Jul 2022 13:19:28 -0700 Subject: [PATCH 1/2] feat: add verbose ability to rooms command --- .../commands/locations/rooms.test.ts | 20 ++++++++++++++++- packages/cli/src/commands/locations/rooms.ts | 22 ++++++++++++++++--- .../src/lib/commands/locations/rooms-util.ts | 2 +- 3 files changed, 39 insertions(+), 5 deletions(-) diff --git a/packages/cli/src/__tests__/commands/locations/rooms.test.ts b/packages/cli/src/__tests__/commands/locations/rooms.test.ts index 39c02bf22..7ebdc88b9 100644 --- a/packages/cli/src/__tests__/commands/locations/rooms.test.ts +++ b/packages/cli/src/__tests__/commands/locations/rooms.test.ts @@ -1,4 +1,4 @@ -import { outputListing } from '@smartthings/cli-lib' +import { outputListing, withLocations } from '@smartthings/cli-lib' import { RoomsEndpoint, SmartThingsClient } from '@smartthings/core-sdk' import RoomsCommand from '../../../commands/locations/rooms' import { getRoomsByLocation, RoomWithLocation } from '../../../lib/commands/locations/rooms-util' @@ -12,6 +12,7 @@ describe('RoomsCommand', () => { const mockOutputListing = jest.mocked(outputListing) const mockGetRoomsByLocation = jest.mocked(getRoomsByLocation) const getSpy = jest.spyOn(RoomsEndpoint.prototype, 'get').mockImplementation() + const mockWithLocations = jest.mocked(withLocations) beforeAll(() => { mockGetRoomsByLocation.mockResolvedValue([]) @@ -121,4 +122,21 @@ describe('RoomsCommand', () => { await expect(RoomsCommand.run([`-l=${locationId}`])).resolves.not.toThrow() expect(mockGetRoomsByLocation).toBeCalledWith(expect.any(SmartThingsClient), locationId) }) + + it('includes location name when verbose flag is used', async () => { + await expect(RoomsCommand.run(['--verbose'])).resolves.not.toThrow() + + expect(mockOutputListing).toBeCalledWith( + expect.any(RoomsCommand), + expect.objectContaining({ + primaryKeyName: 'roomId', + sortKeyName: 'name', + listTableFieldDefinitions: expect.arrayContaining(['location']), + }), + undefined, + expect.any(Function), + expect.any(Function), + ) + expect(mockWithLocations).toBeCalled + }) }) diff --git a/packages/cli/src/commands/locations/rooms.ts b/packages/cli/src/commands/locations/rooms.ts index 343591fbc..c54509af5 100644 --- a/packages/cli/src/commands/locations/rooms.ts +++ b/packages/cli/src/commands/locations/rooms.ts @@ -1,5 +1,5 @@ import { Flags } from '@oclif/core' -import { APICommand, ListingOutputConfig, outputListing } from '@smartthings/cli-lib' +import { APICommand, ListingOutputConfig, outputListing, withLocations } from '@smartthings/cli-lib' import { Room } from '@smartthings/core-sdk' import { getRoomsByLocation, RoomWithLocation, tableFieldDefinitions } from '../../lib/commands/locations/rooms-util' @@ -13,6 +13,10 @@ export default class RoomsCommand extends APICommand char: 'l', description: 'a specific location to query', }), + verbose: Flags.boolean({ + description: 'include location name in output', + char: 'v', + }), ...outputListing.flags, } @@ -30,15 +34,27 @@ export default class RoomsCommand extends APICommand listTableFieldDefinitions: tableFieldDefinitions, tableFieldDefinitions, } + if (this.flags.verbose) { + config.listTableFieldDefinitions?.push('location') + } const rooms = await getRoomsByLocation(this.client, this.flags['location-id']) await outputListing(this, config, this.args.idOrIndex, - async () => rooms, + async () => { + if (this.flags.verbose) { + return await withLocations(this.client, rooms) + } + return rooms + }, async id => { const room = rooms.find(room => room.roomId === id) if (!room) { throw Error(`could not find room with id ${id}`) } - return this.client.rooms.get(id, room.locationId) + const chosenRoom = await this.client.rooms.get(id, room.locationId) + if (this.flags.verbose) { + return await withLocations(this.client, [chosenRoom]) as Room + } + return chosenRoom }) } } diff --git a/packages/cli/src/lib/commands/locations/rooms-util.ts b/packages/cli/src/lib/commands/locations/rooms-util.ts index c3daa21b5..987cf7428 100644 --- a/packages/cli/src/lib/commands/locations/rooms-util.ts +++ b/packages/cli/src/lib/commands/locations/rooms-util.ts @@ -4,7 +4,7 @@ import { LocationItem, Room, SmartThingsClient } from '@smartthings/core-sdk' import * as roomsUtil from './rooms-util' -export const tableFieldDefinitions = ['name', 'locationId', 'roomId'] +export const tableFieldDefinitions = ['name', 'roomId', 'locationId' ] export async function getRoomsByLocation(client: SmartThingsClient, locationId?: string): Promise { let locations: LocationItem[] = [] From a3166648259eb14c6b947f9ca1eca7626f9b3549 Mon Sep 17 00:00:00 2001 From: Michelle Staley <51251946+Sitlintac@users.noreply.github.com> Date: Thu, 7 Jul 2022 14:56:29 -0700 Subject: [PATCH 2/2] Create stale-suns-tan.md --- .changeset/stale-suns-tan.md | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 .changeset/stale-suns-tan.md diff --git a/.changeset/stale-suns-tan.md b/.changeset/stale-suns-tan.md new file mode 100644 index 000000000..5312c73ea --- /dev/null +++ b/.changeset/stale-suns-tan.md @@ -0,0 +1,7 @@ +--- +"@smartthings/cli": patch +--- + +feat: add verbose ability to rooms command + - reorder columns in table output + - add test