Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/stale-suns-tan.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@smartthings/cli": patch
---

feat: add verbose ability to rooms command
- reorder columns in table output
- add test
20 changes: 19 additions & 1 deletion packages/cli/src/__tests__/commands/locations/rooms.test.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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([])
Expand Down Expand Up @@ -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
})
})
22 changes: 19 additions & 3 deletions packages/cli/src/commands/locations/rooms.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand All @@ -13,6 +13,10 @@ export default class RoomsCommand extends APICommand<typeof RoomsCommand.flags>
char: 'l',
description: 'a specific location to query',
}),
verbose: Flags.boolean({
description: 'include location name in output',
char: 'v',
}),
...outputListing.flags,
}

Expand All @@ -30,15 +34,27 @@ export default class RoomsCommand extends APICommand<typeof RoomsCommand.flags>
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
})
}
}
2 changes: 1 addition & 1 deletion packages/cli/src/lib/commands/locations/rooms-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<RoomWithLocation[]> {
let locations: LocationItem[] = []
Expand Down