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 | |
| parent | 7331da69aa3442d7edaed4160b5a6bd3239ed265 (diff) | |
| download | aggietimed-main.tar.gz aggietimed-main.zip | |
| -rw-r--r-- | aggietimed.service | 3 | ||||
| -rw-r--r-- | src/actions.js | 2 | ||||
| -rw-r--r-- | src/aggietime.js | 31 | ||||
| -rw-r--r-- | src/args.js | 44 | ||||
| -rw-r--r-- | src/main.js | 92 |
5 files changed, 88 insertions, 84 deletions
diff --git a/aggietimed.service b/aggietimed.service index 39b2cb5..e209535 100644 --- a/aggietimed.service +++ b/aggietimed.service @@ -7,6 +7,9 @@ Environment=DISPLAY=:0 Type=simple ExecStart=/usr/bin/aggietimed -d -s /tmp/aggietimed.sock Restart=always +StandardError=journal +StandardOutput=journal +StandardInput=null [Install] WantedBy=default.target diff --git a/src/actions.js b/src/actions.js index c1e8d23..a51376b 100644 --- a/src/actions.js +++ b/src/actions.js @@ -1,6 +1,6 @@ import * as aggietime from "./aggietime.js"; -const ACTIONS = { +export const ACTIONS = { "clock-in": aggietime.clock_in, "clock-out": aggietime.clock_out, "current-shift": aggietime.current_shift, diff --git a/src/aggietime.js b/src/aggietime.js index 8fa1880..b7ba63b 100644 --- a/src/aggietime.js +++ b/src/aggietime.js @@ -26,9 +26,10 @@ const replace_path_args = (path, map) => const get_user_position_or_specified = async (position_id) => { const { positions } = await get_user_info(); - if (position_id === undefined && positions.length != 1) { - throw "Must specify a position when there's not only one to choose from"; - } else if (position_id === undefined) { + if (position_id === undefined) { + if (positions.length != 1) { + throw "Must specify a position when there's not only one to choose from"; + } return positions[0]; } @@ -83,20 +84,16 @@ export const clock_out = async (rest) => do_clock_mutation(CLOCKOUT_PATH, rest); export const current_shift = async () => { const req_path = replace_path_args(OPEN_SHIFT_PATH, await get_user_info()); - const { - request: { - res: { responseUrl: response_url }, - }, - data, - } = await aggietime.get(req_path); - if (`${AGGIETIME_URI}/${req_path}` != response_url) { - // IMO a very weird decision - when there is no open shift the api redirects - // home instead of sending back a 404 or something - return null; + try { + const resp = await aggietime.get(req_path); + return resp.data; + } catch (e) { + if (e.response && e.response.status === 404) { + return null; + } + throw e; } - - return data; }; export const get_status_line = async () => { @@ -122,8 +119,8 @@ export const get_status_line = async () => { return { status: expireCache.get("status_line") }; }; -export const last_week = async ({ position_id }) => { - position_id = await get_user_position_or_specified(position_id); +export const last_week = async (args) => { + const position_id = await get_user_position_or_specified(args.position_id); const [start, end] = [ ((d) => { const day = d.getDay(); diff --git a/src/args.js b/src/args.js new file mode 100644 index 0000000..6f06e0b --- /dev/null +++ b/src/args.js @@ -0,0 +1,44 @@ +import { DEFAULT_SOCKET_PATH, DEFAULT_PASS_CMD } from "./constants.js"; +import { ACTIONS } from "./actions.js"; +import * as argparse from "argparse"; + +export const build_args = () => { + const parser = new argparse.ArgumentParser({ description: "AggieTime CLI" }); + + parser.add_argument("-cos", "--cookie-on-stdin", { + help: "set AggieTime cookie from 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: "your AggieTime Position ID (for usage with --action clock_in or clock_out). (default: first returned by AggieTime)", + default: undefined, + }); + + parser.add_argument("-s", "--socket_path", { + default: DEFAULT_SOCKET_PATH, + help: `set server socket path (default: ${DEFAULT_SOCKET_PATH})`, + }); + + parser.add_argument("-p", "--pass_cmd", { + default: DEFAULT_PASS_CMD, + help: `set anumber/password collection retrieval command (default: "${DEFAULT_PASS_CMD}")`, + }); + + parser.add_argument("-a", "--action", { + help: `ignored when daemon flag is set. possible actions are: ${Array.from( + Object.keys(ACTIONS), + ) + .map((x) => `"${x}"`) + .join(", ")}.`, + }); + + return parser.parse_args(); +}; 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}'`); + } + } +}; |
