-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathresources-read.ts
More file actions
48 lines (41 loc) · 1.52 KB
/
resources-read.ts
File metadata and controls
48 lines (41 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import { Arg, Command, Env, Opt } from "./lib/command.js";
import { stdout, stderr } from "process";
import { getDevice } from "./util.js";
import fs from "fs";
const cmd = new Command("Read a resource from device", {
action: async (options: Record<string, string | boolean>, args: Record<string, string>, env: Env) => {
const port = options["port"] as string;
const baudrate = options["baudrate"] as string;
const socket = options["socket"] as string;
const ble = options["ble"] as string | undefined;
const name = args["name"] as string;
const outfile = options["outfile"] as string | undefined;
const device = await getDevice(port, baudrate, socket, ble, env);
await device.controller.lock().catch((err) => {
stderr.write("Error locking device: " + err + "\n");
throw 1;
});
const data = await device.uploader.readResource(name).catch((err) => {
stderr.write("Error: " + err + "\n");
throw 1;
});
await device.controller.unlock().catch((err) => {
stderr.write("Error unlocking device: " + err + "\n");
throw 1;
});
if (outfile) {
fs.writeFileSync(outfile, data);
}
else {
stdout.write(data);
}
},
options: {
"outfile": new Opt("file to save the data into")
},
args: [
new Arg("name", "Name of the resource", { required: true }),
],
chainable: true
});
export default cmd;