Skip to content

Instantly share code, notes, and snippets.

@andreicek
Last active July 30, 2017 07:11
Show Gist options
  • Save andreicek/11fd9c5cfc30526184e0fc934e9c93bb to your computer and use it in GitHub Desktop.
Save andreicek/11fd9c5cfc30526184e0fc934e9c93bb to your computer and use it in GitHub Desktop.
Enpass CSV to pass
const fs = require('fs');
const {spawn} = require('child_process');
function read() {
return new Promise((resolve) => {
fs.readFile('./pass.csv', 'UTF-8', (_, data) => resolve(data));
});
}
function objectify(item) {
const entry = {
name: item[0]
};
item.shift();
item.forEach((e, i) => {
if (i % 2 === 0) {
entry[e] = item[i + 1];
}
});
return entry;
}
function toString(entry) {
delete entry.name;
return Object.keys(entry).map((key) => {
return `${key}: ${entry[key]}`;
}).join('\n');
}
function save(entry) {
return new Promise((resolve) => {
const pass = spawn('pass', ['insert', '-m', entry.name]);
pass.stdout.on('data', (d) => {
pass.stdin.setEncoding('ascii');
pass.stdin.write(toString(entry));
pass.stdin.write('\x03');
pass.kill();
resolve();
});
});
}
(async function() {
const file = (await read())
.split('\n')
.map((row) =>
row.split(',').map((i) => i.replace(/\"/g, '')).filter(Boolean)
)
.map(objectify);
for (let entry of file) {
await save(entry);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment