Skip to content
Open
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
37 changes: 28 additions & 9 deletions packages/tools/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,46 @@ import { logger } from "./logger.js";
const jac = new Program("jac", "Tools for controlling devices running Jaculus", {
globalOptions: {
"log-level": new Opt("Set log level", { defaultValue: "info" }),
help: new Opt("Print this help message", { isFlag: true }),
port: new Opt("Serial port to use (default: first available)"),
baudrate: new Opt("Baudrate to use", { defaultValue: "921600" }),
socket: new Opt("host:port to use"),
},
action: async (options: Record<string, string | boolean>) => {
if (options["help"]) {
stdout.write(jac.help() + "\n");
throw 0;
}
logger.level = options["log-level"] as string;
},
});

// Help command
jac.addCommand(
"help",
new Command("Print help for given command", {
new Command("Print help for given command or subcommand", {
action: async (options: Record<string, string | boolean>, args: Record<string, string>) => {
const command = args["command"];
const subcommand = args["subcommand"];
if (command) {
const cmd = jac.getCommand(command);
if (cmd) {
stdout.write(cmd.help(command) + "\n");
if (subcommand) {
const subcmd = cmd.getSubcommand(subcommand);
if (subcmd) {
stdout.write(subcmd.help(`${command} ${subcommand}`) + "\n");
} else {
stdout.write(`Unknown subcommand: ${subcommand}` + "\n");
}
} else {
stdout.write(cmd.help(command) + "\n");
}
} else {
stdout.write(`Unknown command: ${command}` + "\n");
}
} else {
stdout.write(jac.help() + "\n");
}
},
args: [new Arg("command", "The command to get help for", { required: false })],
args: [
new Arg("command", "The command to get help for", { required: false }),
new Arg("subcommand", "The subcommand to get help for", { required: false }),
],
})
);

Expand All @@ -50,8 +58,19 @@ if (args.length === 0) {
}

jac.run(args)
.then(() => {
.then((result) => {
jac.end();

if (result.type === "help") {
stdout.write(result.text + "\n");
process.exit(0);
}

if (result.type === "exit") {
process.exit(result.code);
}

// type === "continue"
stderr.write("\nDone\n");
process.exit(0);
})
Expand Down
34 changes: 25 additions & 9 deletions packages/tools/src/commands/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Program } from "./lib/command.js";
import { Program, Command } from "./lib/command.js";

import listPorts from "./list-ports.js";
import serialSocket from "./serial-socket.js";
Expand Down Expand Up @@ -42,8 +42,16 @@ export function registerJaculusCommands(jac: Program) {
jac.addCommand("upload", upload);
jac.addCommand("format", formatCmd);

jac.addCommand("project-create", projectCreate);
jac.addCommand("project-update", projectUpdate);
const projectCommand = new Command("Project management commands", {
description: "Manage projects, create and update projects.",
chainable: true,
subcommands: {
create: projectCreate,
update: projectUpdate,
},
});
jac.addCommand("project", projectCommand);

jac.addCommand("resources-ls", resourcesLs);
jac.addCommand("resources-read", resourcesRead);

Expand All @@ -52,12 +60,20 @@ export function registerJaculusCommands(jac: Program) {
jac.addCommand("status", status);
jac.addCommand("monitor", monitor);

jac.addCommand("wifi-get", wifiGet);
jac.addCommand("wifi-ap", wifiSetAp);
jac.addCommand("wifi-add", wifiAdd);
jac.addCommand("wifi-rm", wifiRemove);
jac.addCommand("wifi-sta", wifiSetSta);
jac.addCommand("wifi-disable", wifiDisable);
const wifiCommand = new Command("WiFi configuration commands", {
description:
"Manage WiFi settings, configure networks, and switch between AP and Station modes.",
chainable: true,
subcommands: {
get: wifiGet,
ap: wifiSetAp,
add: wifiAdd,
rm: wifiRemove,
sta: wifiSetSta,
disable: wifiDisable,
},
});
jac.addCommand("wifi", wifiCommand);

jac.addCommand("serial-socket", serialSocket);
}
Loading