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.

76 lines
2.5 KiB

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