From 686f0bec4fab0410b2fcfecba93831566e5e845c Mon Sep 17 00:00:00 2001 From: AGitBoy Date: Sat, 26 Jan 2019 18:30:29 -0500 Subject: [PATCH] Added length argument --- README.md | 4 +++- bin/cli.js | 12 ++++++++---- src/index.js | 29 ++++++++++++----------------- 3 files changed, 23 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 5bde041..ae67180 100644 --- a/README.md +++ b/README.md @@ -18,9 +18,11 @@ The DMENU_PATH environment variable can be used to point to an alternative dmenu Options: --clear-clipboard Number of seconds to keep selected field in the clipboard. Defaults to 15s. + -l Sets the -l parameter value passed to dmenu. + Defaults to 0 --session-timeout Number of seconds after an unlock that the menu can be accessed without providing a password again. Defaults to 0s. - --stdout Prints the password & username to stdout + --stdout Prints the password and username to stdout --sync-vault-after Number of seconds allowable between last bitwarden sync and current time. Defaults to 0s. --on-error Arbitrary command to run if the program fails. The thrown error diff --git a/bin/cli.js b/bin/cli.js index be5e0e7..0a57c7d 100755 --- a/bin/cli.js +++ b/bin/cli.js @@ -8,10 +8,11 @@ const menu = require('../src') const scheduleCleanup = require('../src/schedule-cleanup') const cachePasswordDefault = 15 +const lengthDefault = 0 const sessionTimeoutDefault = 0 const syncVaultAfterDefault = 0 -const urlFilterDefault = null const stdoutDefault = false +const urlFilterDefault = null const args = minimist(process.argv.slice(2)) if (args.help) { @@ -23,9 +24,11 @@ The DMENU_PATH environment variable can be used to point to an alternative dmenu Options: --clear-clipboard Number of seconds to keep selected field in the clipboard. Defaults to ${cachePasswordDefault}s. + -l Sets the -l parameter value passed to dmenu. + Defaults to ${lengthDefault} --session-timeout Number of seconds after an unlock that the menu can be accessed without providing a password again. Defaults to ${sessionTimeoutDefault}s. - --stdout Prints the password & username to stdout + --stdout Prints the password and username to stdout --sync-vault-after Number of seconds allowable between last bitwarden sync and current time. Defaults to ${syncVaultAfterDefault}s. --on-error Arbitrary command to run if the program fails. The thrown error @@ -39,11 +42,12 @@ Options: } const clearClipboardAfter = args['clear-clipboard'] || cachePasswordDefault +const length = args['l'] || lengthDefault const sessionTimeout = args['session-timeout'] || sessionTimeoutDefault const syncVaultAfter = args['sync-vault-after'] || syncVaultAfterDefault const onErrorCommand = args['on-error'] -const urlFilter = args['url'] || urlFilterDefault const stdout = args['stdout'] || stdoutDefault +const urlFilter = args['url'] || urlFilterDefault console.debug = args['verbose'] ? (...msgs) => console.log(...msgs, '\n') @@ -53,7 +57,7 @@ const oldestAllowedVaultSync = syncVaultAfter const saveSession = Boolean(sessionTimeout) const sessionFile = path.resolve(os.tmpdir(), 'bitwarden-session.txt') -menu({ saveSession, sessionFile, oldestAllowedVaultSync, urlFilter, stdout }) +menu({ length, saveSession, sessionFile, stdout, oldestAllowedVaultSync, urlFilter }) .then(() => scheduleCleanup({ lockBitwardenAfter: sessionTimeout, diff --git a/src/index.js b/src/index.js index f28887c..8068c3b 100644 --- a/src/index.js +++ b/src/index.js @@ -48,28 +48,22 @@ const syncIfNecessary = ({ session, oldestAllowedVaultSync }) => { // get the list all password accounts in the vault const getAccounts = ({ session, urlFilter }) => { - var listStr = "" - if(urlFilter) { - listStr = bwRun('list', 'items', `--url=${urlFilter}`, `--session=${session}`) - } else { - listStr = bwRun('list', 'items', `--session=${session}`) - } - // const listStr = urlFilter - // ? bwRun('list', 'items', `--url=${urlFilter}`, `--session=${session}`) - // : bwRun('list', 'items', `--session=${session}`) + const listStr = urlFilter + ? bwRun('list', 'items', `--url=${urlFilter}`, `--session=${session}`) + : bwRun('list', 'items', `--session=${session}`) const list = JSON.parse(listStr) return list } // choose one account with dmenu -const chooseAccount = async ({ list }) => { +const chooseAccount = async ({ list, length }) => { const LOGIN_TYPE = 1 const loginList = list.filter(a => a.type === LOGIN_TYPE) const accountNames = loginList.map(a => `${a.name}: ${a.login.username}`) // -i allows case insensitive matching - const selected = await dmenuRun()(accountNames.join('\n')) + const selected = await dmenuRun(`-l ${length}`)(accountNames.join('\n')) const index = accountNames.indexOf(selected) // accountNames indexes match loginList indexes const selectedAccount = loginList[index] @@ -78,7 +72,7 @@ const chooseAccount = async ({ list }) => { } // choose one field with dmenu -const chooseField = async ({ selectedAccount }) => { +const chooseField = async ({ selectedAccount, length }) => { if (!selectedAccount) throw new Error('no account selected!') const copyable = { password: selectedAccount.login.password, @@ -92,35 +86,36 @@ const chooseField = async ({ selectedAccount }) => { {} ) } - const field = await dmenuRun()(Object.keys(copyable).join('\n')) + const field = await dmenuRun(`-l ${length}`)(Object.keys(copyable).join('\n')) console.debug(`selected field '${field}'`) const valueToCopy = copyable[field] return valueToCopy } module.exports = async ({ + length, saveSession, sessionFile, + stdout, oldestAllowedVaultSync, urlFilter, - stdout }) => { const session = await getSessionVar({ saveSession, sessionFile }) // bw sync if necessary syncIfNecessary({ session, oldestAllowedVaultSync }) - + // bw list const list = getAccounts({ session, urlFilter }) // choose account in dmenu - const selectedAccount = await chooseAccount({ list }) + const selectedAccount = await chooseAccount({ list, length }) if(stdout) { console.log(`${selectedAccount.login.username}\n${selectedAccount.login.password}`) } else { // choose field to copy in dmenu - const valueToCopy = await chooseField({ selectedAccount }) + const valueToCopy = await chooseField({ selectedAccount, length }) // copy to clipboard clipboardy.writeSync(valueToCopy)