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.

69 lines
2.3 KiB

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/index')
  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. `
  25. )
  26. process.exit()
  27. }
  28. const clearClipboardAfter = args['clear-clipboard'] || cachePasswordDefault
  29. const sessionTimeout = args['session-timeout'] || sessionTimeoutDefault
  30. const syncVaultAfter = args['sync-vault-after'] || syncVaultAfterDefault
  31. const onErrorCommand = args['on-error']
  32. const oldestAllowedVaultSync = syncVaultAfter
  33. const saveSession = Boolean(sessionTimeout)
  34. const sessionFile = path.resolve(os.tmpdir(), 'bitwarden-session.txt')
  35. menu({ saveSession, sessionFile, oldestAllowedVaultSync })
  36. .then(() =>
  37. scheduleCleanup({
  38. lockBitwardenAfter: sessionTimeout,
  39. clearClipboardAfter,
  40. sessionFile
  41. })
  42. )
  43. .catch(e => {
  44. console.error(e)
  45. // if something goes wrong, immediately clear the clipboard & lock bitwarden,
  46. // then run error command
  47. scheduleCleanup({
  48. lockBitwardenAfter: 0,
  49. clearClipboardAfter: 0,
  50. sessionFile
  51. })
  52. .catch(e => {
  53. // simply log a secondary error
  54. console.error(e)
  55. })
  56. .then(() => {
  57. if (onErrorCommand) {
  58. const errorCommand = exec(onErrorCommand)
  59. errorCommand.stdin.write(`'${e.toString()}'`)
  60. errorCommand.stdin.end()
  61. }
  62. })
  63. })