Skip to content

Commit 9ae1a09

Browse files
committed
feat(cli): list devices
1 parent 5c3ba4b commit 9ae1a09

File tree

4 files changed

+174
-0
lines changed

4 files changed

+174
-0
lines changed

cli/cli.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { logsCommand } from './commands/logs.js'
1818
import { CloudWatchLogsClient } from '@aws-sdk/client-cloudwatch-logs'
1919
import { configureHello } from './commands/configure-hello.js'
2020
import { shareDevice } from './commands/share-device.js'
21+
import { listDevicesCommand } from './commands/listDevices.js'
2122

2223
const ssm = new SSMClient({})
2324
const db = new DynamoDBClient({})
@@ -79,6 +80,10 @@ const CLI = async ({ isCI }: { isCI: boolean }) => {
7980
publicDevicesTableName: backendOutputs.publicDevicesTableName,
8081
idIndex: backendOutputs.publicDevicesTableIdIndexName,
8182
}),
83+
listDevicesCommand({
84+
db,
85+
publicDevicesTableName: backendOutputs.publicDevicesTableName,
86+
}),
8287
)
8388
} catch (error) {
8489
console.warn(chalk.yellow('⚠️'), chalk.yellow((error as Error).message))

cli/commands/listDevices.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { ScanCommand, type DynamoDBClient } from '@aws-sdk/client-dynamodb'
2+
import { unmarshall } from '@aws-sdk/util-dynamodb'
3+
import type { CommandDefinition } from './CommandDefinition.js'
4+
import { table } from 'table'
5+
import chalk from 'chalk'
6+
7+
export const listDevicesCommand = ({
8+
db,
9+
publicDevicesTableName,
10+
}: {
11+
db: DynamoDBClient
12+
publicDevicesTableName: string
13+
}): CommandDefinition => ({
14+
command: 'list-devices',
15+
options: [
16+
{
17+
flags: '-n, --nextKey <key>',
18+
description: `Pagination key`,
19+
},
20+
],
21+
action: async ({ nextKey }) => {
22+
const { Items, LastEvaluatedKey } = await db.send(
23+
new ScanCommand({
24+
TableName: publicDevicesTableName,
25+
Limit: 100,
26+
ExclusiveStartKey:
27+
nextKey !== undefined
28+
? JSON.parse(Buffer.from(nextKey, 'base64').toString())
29+
: undefined,
30+
}),
31+
)
32+
33+
console.log(
34+
table([
35+
['Device ID', 'Public ID', 'Model', 'Owner Email', 'Confirmed until'],
36+
...(Items ?? [])
37+
.map((i) => unmarshall(i))
38+
.map(({ secret__deviceId: deviceId, id, model, ownerEmail, ttl }) => [
39+
chalk.green(deviceId),
40+
chalk.blue(id),
41+
model,
42+
ownerEmail,
43+
new Date(ttl * 1000).toISOString(),
44+
]),
45+
]),
46+
)
47+
48+
console.log(
49+
LastEvaluatedKey
50+
? `More devices available: ${Buffer.from(JSON.stringify(LastEvaluatedKey)).toString('base64')}`
51+
: 'No more devices',
52+
)
53+
},
54+
help: 'List public devices',
55+
})

package-lock.json

Lines changed: 113 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
"husky": "9.0.11",
6060
"jsonata": "2.0.5",
6161
"semver": "7.6.2",
62+
"table": "6.8.2",
6263
"tsmatchers": "5.0.2",
6364
"tsx": "4.16.2"
6465
},

0 commit comments

Comments
 (0)