"use strict"; const Promise = require("bluebird"); const path = require("path"); const execBinary = require("../exec-binary"); const createPegParser = require("../text-parser-pegjs"); const itemsToObject = require("../../packages/items-to-object"); /* FIXME: Error handling, eg. device not found errors */ function outputParser(rootRule) { return createPegParser({ grammarFile: path.join(__dirname, "./parser.pegjs"), options: { allowedStartRules: [ rootRule ] } }); } module.exports = { attributes: function ({ devicePath }) { return Promise.try(() => { return execBinary("smartctl", [devicePath]) .asRoot() .withFlags({ attributes: true }) .requireOnStdout(outputParser("RootAttributes")) .execute(); }).then((output) => { // NOTE: Ignore the header, for now return output.result.attributes; }); }, info: function ({ devicePath }) { return Promise.try(() => { return execBinary("smartctl", [devicePath]) .asRoot() .withFlags({ info: true }) .requireOnStdout(outputParser("RootInfo")) .execute(); }).then((output) => { // NOTE: Ignore the header, for now return itemsToObject(output.result.fields); }); }, scan: function () { return Promise.try(() => { return execBinary("smartctl") .asRoot() .withFlags({ scan: true }) .requireOnStdout(outputParser("RootScan")) .execute(); }).then((output) => { // NOTE: Ignore the header, for now return output.result.devices; }); } };