You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
2.5 KiB
JavaScript

"use strict";
const Promise = require("bluebird");
const DataLoader = require("dataloader");
const mapObj = require("map-obj");
const lvm = require("../../packages/exec-lvm");
const All = require("../../packages/graphql-interface/symbols/all");
const nvmeCLI = require("../../packages/exec-nvme-cli");
const smartctl = require("../../packages/exec-smartctl");
const dlayerWrap = require("../../packages/dlayer-wrap");
const evaluateAndPick = require("../../packages/evaluate-and-pick");
function makeSingleCommand({ command, selectResult }) {
return function (ids) {
return Promise.map(ids, (id) => {
if (id === All) {
// FIXME: Have some sort of mechanism for making this possible?
throw new Error(`This data source does not support fetching all entries`);
} else {
return command(id);
}
}).map((result) => {
if (selectResult != null) {
return selectResult(result);
} else {
return result;
}
});
};
}
module.exports = function createSources() {
let sources = {
// lvmLogicalVolumes: evaluateAndPick({
// command: lvm.getLogicalVolumes,
// selectResult: (result) => result.volumes,
// selectID: (volume) => volume.path
// }),
// lvmPhysicalVolumes: evaluateAndPick({
// command: lvm.getPhysicalVolumes,
// selectResult: (result) => result.volumes,
// selectID: (device) => device.path
// }),
// lvmVolumeGroups: evaluateAndPick({
// command: lvm.getVolumeGroups,
// selectResult: (result) => result.groups,
// selectID: (group) => group.name
// }),
nvmeIdentifyController: makeSingleCommand({
command: (path) => nvmeCLI.identifyController({ devicePath: path })
}),
nvmeListNamespaces: makeSingleCommand({
command: (path) => nvmeCLI.listNamespaces({ devicePath: path })
}),
smartctlScan: evaluateAndPick({
command: smartctl.scan,
selectID: (device) => device.path
}),
smartctlInfo: makeSingleCommand({
command: (path) => dlayerWrap(() => smartctl.info({ devicePath: path }), {
allowedErrors: [ smartctl.InfoError ]
})
}),
smartctlAttributes: makeSingleCommand({
command: (path) => dlayerWrap(() => smartctl.attributes({ devicePath: path }), {
allowedErrors: [ smartctl.AttributesError ]
})
}),
};
// TODO: Consider moving these to be inline as well, somehow
let factoryModules = {
lsblk: require("./lsblk")(),
findmnt: require("./findmnt")()
};
return mapObj({ ... factoryModules, ... sources }, (name, factory) => {
return [
name,
new DataLoader(factory)
];
});
};