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.

68 lines
1.5 KiB
JavaScript

"use strict";
const Promise = require("bluebird");
const matchValue = require("match-value");
const execBinary = require("../exec-binary");
const parseIECBytes = require("../parse-bytes-iec");
const createJSONParser = require("../text-parser-json");
function parseBoolean(value) {
if (typeof value === "boolean") {
// Newer versions of `lsblk` correctly use boolean values
return value;
} else {
return matchValue(value, {
0: false,
1: true
});
}
}
function mapType(value) {
return matchValue(value, {
part: "partition",
disk: "disk",
loop: "loopDevice",
rom: "disk",
lvm: "partition"
});
}
function mapSubType(value) {
return matchValue(value, {
part: null,
disk: null,
loop: null,
rom: "readOnlyMedia",
lvm: "lvm"
});
}
function mapDeviceList(devices) {
return devices.map((device) => {
return {
name: device.name,
path: device.path,
type: mapType(device.type),
subType: mapSubType(device.type),
mountpoint: device.mountpoint,
deviceNumber: device["maj:min"],
removable: parseBoolean(device.rm),
readOnly: parseBoolean(device.ro),
size: parseIECBytes(device.size),
children: (device.children != null) ? mapDeviceList(device.children) : []
};
});
}
module.exports = function lsblk() {
return Promise.try(() => {
return execBinary("lsblk")
.withFlags({ json: true, "output-all": true })
.requireOnStdout(createJSONParser())
.execute();
}).then((output) => {
return mapDeviceList(output.result.blockdevices);
});
};