dmenu for bitwarden-cli
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
2.4 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. #!/usr/bin/env node
  2. const os = require('os')
  3. const path = require('path')
  4. const { exec } = require('child_process')
  5. const minimist = require('minimist')
  6. const menu = require('../src')
  7. const scheduleCleanup = require('../src/schedule-cleanup')
  8. const cachePasswordDefault = 15
  9. const sessionTimeoutDefault = 0
  10. const syncVaultAfterDefault = 0
  11. const args = minimist(process.argv.slice(2))
  12. if (args.help) {
  13. console.log(
  14. `Usage: bitwarden-dmenu [options]
  15. Options:
  16. --clear-clipboard Number of seconds to keep selected field in the clipboard.
  17. Defaults to ${cachePasswordDefault}s.
  18. --session-timeout Number of seconds after an unlock that the menu can be accessed
  19. without providing a password again. Defaults to ${sessionTimeoutDefault}s.
  20. --sync-vault-after Number of seconds allowable between last bitwarden sync and
  21. current time. Defaults to ${syncVaultAfterDefault}s.
  22. --on-error Arbitrary command to run if the program fails. The thrown error
  23. is piped to the given command. Defaults to none.
  24. --verbose Show extra logs useful for debugging.
  25. `
  26. )
  27. process.exit()
  28. }
  29. const clearClipboardAfter = args['clear-clipboard'] || cachePasswordDefault
  30. const sessionTimeout = args['session-timeout'] || sessionTimeoutDefault
  31. const syncVaultAfter = args['sync-vault-after'] || syncVaultAfterDefault
  32. const onErrorCommand = args['on-error']
  33. console.debug = args['verbose']
  34. ? (...msgs) => console.log(...msgs, '\n')
  35. : () => {}
  36. const oldestAllowedVaultSync = syncVaultAfter
  37. const saveSession = Boolean(sessionTimeout)
  38. const sessionFile = path.resolve(os.tmpdir(), 'bitwarden-session.txt')
  39. menu({ saveSession, sessionFile, oldestAllowedVaultSync })
  40. .then(() =>
  41. scheduleCleanup({
  42. lockBitwardenAfter: sessionTimeout,
  43. clearClipboardAfter,
  44. sessionFile
  45. })
  46. )
  47. .catch(e => {
  48. console.error(e)
  49. // if something goes wrong, immediately clear the clipboard & lock bitwarden,
  50. // then run error command
  51. scheduleCleanup({
  52. lockBitwardenAfter: 0,
  53. clearClipboardAfter: 0,
  54. sessionFile
  55. })
  56. .catch(e => {
  57. // simply log an error with cleanup
  58. console.error(e)
  59. })
  60. .then(() => {
  61. if (onErrorCommand) {
  62. const errorCommand = exec(onErrorCommand)
  63. errorCommand.stdin.write(`'${e.toString()}'`)
  64. errorCommand.stdin.end()
  65. }
  66. })
  67. })