Skip to content

Instantly share code, notes, and snippets.

@simontraill
Created October 12, 2017 14:33
Show Gist options
  • Save simontraill/fb1d2e4d7d6bfd1375a3fd67bc7cb56c to your computer and use it in GitHub Desktop.
Save simontraill/fb1d2e4d7d6bfd1375a3fd67bc7cb56c to your computer and use it in GitHub Desktop.
const readline = require('readline');
const fs = require('fs');
function readLines(devs,callback) {
var values = {};
var rs = fs.createReadStream('./partitions');
rs.on('error', function(e) {
callback(e);
});
var rl = readline.createInterface({
input: rs
});
rl.on('line', function(buffer) {
var tokens = buffer.toString().trim().split(/\s+/);
var lastToken = tokens.slice(-1)[0];
if (devs.indexOf(lastToken) != -1) {
values[lastToken] = tokens.slice(0,2).map(function(x) { return parseInt(x); });
}
});
rl.on('close', function() {
callback(false,values);
});
}
readLines(['sda','sda1','sda2','sda5'],function(error,result) {
if ( error ) {
console.log("Something went wrong: " + error.toString());
} else {
console.log(JSON.stringify(result));
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment