Browse Source

Manueal sync from dmenu

master
Yigit 4 years ago
parent
commit
832850ca23
1 changed files with 16 additions and 4 deletions
  1. +16
    -4
      src/index.js

+ 16
- 4
src/index.js View File

@ -1,4 +1,4 @@
const { existsSync, writeFileSync, readFileSync } = require('fs')
const { existsSync, writeFileSync, readFileSync, unlinkSync } = require('fs')
const clipboardy = require('clipboardy') const clipboardy = require('clipboardy')
const { CommandError } = require('./util/error') const { CommandError } = require('./util/error')
const dmenuRun = require('./executable-wrappers/dmenu') const dmenuRun = require('./executable-wrappers/dmenu')
@ -81,22 +81,26 @@ const getSessionVar = async ({ dmenuPswdArgs, saveSession, sessionFile }) => {
* sync the password accounts with the remote server * sync the password accounts with the remote server
* if --sync-vault-after < time since the last sync * if --sync-vault-after < time since the last sync
*/ */
const syncIfNecessary = ({ oldestAllowedVaultSync }, session) => {
const syncIfNecessary = (args, session) => {
// const last = bwRun('sync', '--last', `--session=${session}`) // const last = bwRun('sync', '--last', `--session=${session}`)
if(!existsSync('/tmp/bwdmenu_lastsync')){ if(!existsSync('/tmp/bwdmenu_lastsync')){
console.debug('syncing vault...') console.debug('syncing vault...')
bwRun('sync', `--session=${session}`) bwRun('sync', `--session=${session}`)
writeFileSync('/tmp/bwdmenu_lastsync', new Date().toISOString(),{encoding:'utf8', flag:'w'}); writeFileSync('/tmp/bwdmenu_lastsync', new Date().toISOString(),{encoding:'utf8', flag:'w'});
unlinkSync("/tmp/bwdmenu_accounts");
getAccounts(args, session);
return; return;
} }
const last = readFileSync('/tmp/bwdmenu_lastsync', {encoding:'utf8', flag:'r'}); const last = readFileSync('/tmp/bwdmenu_lastsync', {encoding:'utf8', flag:'r'});
const timeSinceSync = (new Date().getTime() - new Date(last).getTime()) / 1000 const timeSinceSync = (new Date().getTime() - new Date(last).getTime()) / 1000
if (timeSinceSync > oldestAllowedVaultSync) {
if (timeSinceSync > args.oldestAllowedVaultSync) {
console.debug('syncing vault...') console.debug('syncing vault...')
bwRun('sync', `--session=${session}`) bwRun('sync', `--session=${session}`)
console.debug(`sync complete, last sync was ${last}`) console.debug(`sync complete, last sync was ${last}`)
writeFileSync('/tmp/bwdmenu_lastsync',new Date().toISOString(),{encoding:'utf8', flag:'w'}); writeFileSync('/tmp/bwdmenu_lastsync',new Date().toISOString(),{encoding:'utf8', flag:'w'});
unlinkSync("/tmp/bwdmenu_accounts");
getAccounts(args, session);
} }
} }
@ -136,7 +140,9 @@ const chooseAccount = async ({ dmenuArgs }, list) => {
const loginList = list.filter(a => a.type === LOGIN_TYPE) const loginList = list.filter(a => a.type === LOGIN_TYPE)
const accountNames = loginList.map(a => `${a.name}: ${a.login.username}`) const accountNames = loginList.map(a => `${a.name}: ${a.login.username}`)
const selected = await dmenuRun(...dmenuArgs)(accountNames.join('\n'))
const selected = await dmenuRun(...dmenuArgs)(accountNames.join('\n') + "\n!SYNC")
if(selected == "!SYNC")
return selected
const index = accountNames.indexOf(selected) const index = accountNames.indexOf(selected)
// accountNames indexes match loginList indexes // accountNames indexes match loginList indexes
const selectedAccount = loginList[index] const selectedAccount = loginList[index]
@ -182,6 +188,12 @@ module.exports = async args => {
// choose account in dmenu // choose account in dmenu
const selectedAccount = await chooseAccount(args, list) const selectedAccount = await chooseAccount(args, list)
if(selectedAccount == "!SYNC"){
unlinkSync("/tmp/bwdmenu_lastsync");
syncIfNecessary(args, session);
return;
}
if (args.stdout) { if (args.stdout) {
console.log(`${selectedAccount.login.username}\n${selectedAccount.login.password}`) console.log(`${selectedAccount.login.username}\n${selectedAccount.login.password}`)
} else { } else {


Loading…
Cancel
Save