diff options
| author | Lizzy Hunt <lizzy.hunt@usu.edu> | 2024-02-23 11:48:48 -0700 |
|---|---|---|
| committer | Lizzy Hunt <lizzy.hunt@usu.edu> | 2024-02-23 11:48:48 -0700 |
| commit | 76bca61a5ca50a5033486b51d7b7829ec831fd98 (patch) | |
| tree | 443a9def75bc5bf37e89fc1bbe7bb3a9795b484a /src/main.js | |
| parent | 7331da69aa3442d7edaed4160b5a6bd3239ed265 (diff) | |
| download | aggietimed-76bca61a5ca50a5033486b51d7b7829ec831fd98.tar.gz aggietimed-76bca61a5ca50a5033486b51d7b7829ec831fd98.zip | |
Diffstat (limited to 'src/main.js')
| -rw-r--r-- | src/main.js | 92 |
1 files changed, 26 insertions, 66 deletions
diff --git a/src/main.js b/src/main.js index 2e1c975..34aea7b 100644 --- a/src/main.js +++ b/src/main.js @@ -1,41 +1,15 @@ #!/usr/bin/env node -import { - DEFAULT_SOCKET_PATH, - DEFAULT_PASS_CMD, - KILL_SIGNALS, - REFRESH_JWT_MS, -} from "./constants.js"; +import { KILL_SIGNALS, REFRESH_JWT_MS } from "./constants.js"; + +import { build_args } from "./args.js"; import * as actions from "./actions.js"; import * as session from "./session.js"; import retrieve_creds from "./retrieve_creds.js"; -import * as argparse from "argparse"; import * as net from "net"; import * as dotenv from "dotenv"; import * as fs from "fs"; -export default async () => { - dotenv.config(); - const args = build_args(); - - if (args.daemon) { - try { - start_server(args, session.logout); - } catch (e) { - console.error(e); - if (fs.existsSync(args.socket_path)) { - fs.unlinkSync(args.socket_path); - } - } - } else if (args.action) { - if (fs.existsSync(args.socket_path)) { - run_action(args); - } else { - console.error(`ERR: No such socket '${args.socket_path}'`); - } - } -}; - const run_action = (args) => { const { socket_path, action, position_id } = args; const connection = net.connect(socket_path); @@ -52,43 +26,6 @@ const run_action = (args) => { connection.write(JSON.stringify({ action, rest: { position_id } })); }; -const build_args = () => { - const parser = new argparse.ArgumentParser({ description: "AggieTime CLI" }); - - parser.add_argument("-cos", "--cookie-on-stdin", { - help: "Set a cookie from whatever is on stdin", - action: argparse.BooleanOptionalAction, - default: false, - }); - - parser.add_argument("-d", "--daemon", { - help: "Start server as a process blocking daemon", - action: argparse.BooleanOptionalAction, - default: false, - }); - - parser.add_argument("-pos", "--position-id", { - help: "Position ID (for usage with --action clock_in or clock_out)", - default: undefined, - }); - - parser.add_argument("-s", "--socket_path", { - default: DEFAULT_SOCKET_PATH, - help: `Set server socket path, defaults to ${DEFAULT_SOCKET_PATH}`, - }); - - parser.add_argument("-p", "--pass_cmd", { - default: DEFAULT_PASS_CMD, - help: `Set GNU pass retrieval command, defaults to ${DEFAULT_PASS_CMD}`, - }); - - 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(); -}; - const kill_server = (server, socket_path) => { server.close(); try { @@ -142,6 +79,7 @@ specify another socket path with --socket_path`, }) .catch((e) => { console.error(e); + console.log(e); client.write(JSON.stringify({ err: true }) + "\r\n"); }); @@ -158,3 +96,25 @@ specify another socket path with --socket_path`, process.on(signal, () => kill_server(unix_server, socket_path)), ); }; + +export default async () => { + dotenv.config(); + const args = build_args(); + + if (args.daemon) { + try { + start_server(args, session.logout); + } catch (e) { + console.error(e); + if (fs.existsSync(args.socket_path)) { + fs.unlinkSync(args.socket_path); + } + } + } else if (args.action) { + if (fs.existsSync(args.socket_path)) { + run_action(args); + } else { + console.error(`ERR: No such socket '${args.socket_path}'`); + } + } +}; |
