diff options
author | gumi <git@gumi.ca> | 2020-02-14 12:18:00 -0500 |
---|---|---|
committer | gumi <git@gumi.ca> | 2020-03-02 15:37:17 -0500 |
commit | 2c25f53ddf418bdedd94c6142b03c80e49fc584d (patch) | |
tree | c15c45c16b7b649fb267241ffe0fe90aacf6fae5 /src/routers/vault/utils/flatfile.js | |
parent | 872288426090839f2f23e60187a58ee51d6fe4ea (diff) | |
download | api-2c25f53ddf418bdedd94c6142b03c80e49fc584d.tar.gz api-2c25f53ddf418bdedd94c6142b03c80e49fc584d.tar.bz2 api-2c25f53ddf418bdedd94c6142b03c80e49fc584d.tar.xz api-2c25f53ddf418bdedd94c6142b03c80e49fc584d.zip |
add support for Vault + major refactor
Diffstat (limited to 'src/routers/vault/utils/flatfile.js')
-rw-r--r-- | src/routers/vault/utils/flatfile.js | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/src/routers/vault/utils/flatfile.js b/src/routers/vault/utils/flatfile.js new file mode 100644 index 0000000..e9d6fee --- /dev/null +++ b/src/routers/vault/utils/flatfile.js @@ -0,0 +1,31 @@ +const execFile = require("child_process").execFile; +const ripgrep = require("ripgrep-bin"); + +const execAsync = (cmd, par) => + new Promise((resolve, reject) => + execFile(cmd, par, (error, stdout, stderr) => + resolve(error ? "" : (stdout ? stdout : stderr)))); + +const tmwa_account_regex = new RegExp("^(?<id>[0-9]+)\t(?<name>[^\t]+)\t(?<password>[^\t]+)\t"); + +const parseAccountLine = (line) => { + const { groups: account } = tmwa_account_regex.exec(line); + return { + id: +account.id, + name: account.name, + password: account.password, + }; +} + +const findAccount = async (account_id, name) => { + const regex = `^${account_id}\t${name}\t`; + const stdout = await execAsync(ripgrep, ["--case-sensitive", `--max-count=1`, regex, "account.txt"]); + let account = null; + if (stdout.length) + account = parseAccountLine(stdout.slice(0, -1).split("\n")[0]); + return account; +}; + +module.exports = { + findAccount, +}; |