summaryrefslogtreecommitdiff
path: root/src/main.js
diff options
context:
space:
mode:
authorLizzy Hunt <logan.hunt@usu.edu>2023-02-16 13:55:40 -0700
committerLizzy Hunt <logan.hunt@usu.edu>2023-02-16 13:55:40 -0700
commit312af9bc10d9cf2982e66153fbc1cd949ad1f169 (patch)
tree933033ceeb0a678a73b44a552a4d2da9812597bb /src/main.js
parent32803c441678cd640e46153688d26c4c0746d7b3 (diff)
downloadaggietimed-312af9bc10d9cf2982e66153fbc1cd949ad1f169.tar.gz
aggietimed-312af9bc10d9cf2982e66153fbc1cd949ad1f169.zip
Add flag to connect over socket and retrieve action response, simple systemd service, and more actions
Diffstat (limited to 'src/main.js')
-rw-r--r--src/main.js42
1 files changed, 37 insertions, 5 deletions
diff --git a/src/main.js b/src/main.js
index 099e4af..bc81f15 100644
--- a/src/main.js
+++ b/src/main.js
@@ -1,3 +1,5 @@
+#!/usr/bin/env node
+
import {
DEFAULT_SOCKET_PATH,
KILL_SIGNALS,
@@ -10,7 +12,7 @@ import * as net from "net";
import * as dotenv from "dotenv";
import * as fs from "fs";
-const main = async () => {
+export default async () => {
dotenv.config();
const args = build_args();
@@ -20,16 +22,35 @@ const main = async () => {
} catch {
fs.unlinkSync(args.socket_path);
}
+ } else if (args.action) {
+ if (fs.existsSync(args.socket_path)) {
+ run_action(args.socket_path, args.action);
+ } else {
+ console.error(`ERR: No such socket '${args.socket_path}'`);
+ }
}
};
+const run_action = (socket_path, action) => {
+ const connection = net.connect(socket_path);
+ connection.on("data", (data) => {
+ if (Buffer.isBuffer(data)) {
+ console.log(data.toString().trim());
+ } else {
+ console.log(data.trim());
+ }
+ connection.end();
+ });
+ connection.write(JSON.stringify({ action }));
+};
+
const build_args = () => {
const parser = new argparse.ArgumentParser({ description: "AggieTime CLI" });
parser.add_argument("-d", "--daemon", {
help: "Start server as a process blocking daemon",
action: argparse.BooleanOptionalAction,
- default: true,
+ default: false,
});
parser.add_argument("-s", "--socket_path", {
@@ -37,6 +58,10 @@ const build_args = () => {
help: `Set server socket path, defaults to ${DEFAULT_SOCKET_PATH}`,
});
+ parser.add_argument("-a", "--action", {
+ help: `Ignored when daemon flag is set. Returns the value of action (see actions.js) when sent over the socket.`,
+ });
+
return parser.parse_args();
};
@@ -76,7 +101,16 @@ specify another socket path with --socket_path`
return;
}
- actions.do_action(body);
+ actions
+ .do_action(body)
+ .then((resp) => {
+ client.write(JSON.stringify(resp) + "\r\n");
+ })
+ .catch((e) => {
+ console.error(e);
+
+ client.write(JSON.stringify({ err: true }) + "\r\n");
+ });
});
});
@@ -90,5 +124,3 @@ specify another socket path with --socket_path`
process.on(signal, () => kill_server(unix_server, socket_path))
);
};
-
-main();