Browse Source

Added length argument

master
AGitBoy 6 years ago
parent
commit
686f0bec4f
3 changed files with 23 additions and 22 deletions
  1. +3
    -1
      README.md
  2. +8
    -4
      bin/cli.js
  3. +12
    -17
      src/index.js

+ 3
- 1
README.md View File

@ -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


+ 8
- 4
bin/cli.js View File

@ -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,


+ 12
- 17
src/index.js View File

@ -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)


Loading…
Cancel
Save