"use strict"; const Promise = require("bluebird"); const execBinary = require("../../exec-binary"); const { errorResult } = require("../../text-parser"); const createRegexParser = require("../../text-parser-regex"); const unattendedFlags = require("../modifiers/unattended-flags"); const errors = require("../errors"); // TODO: Plural versions of handle modifiers? Or just have them accept an array of inputs? module.exports = function ({ name, physicalVolumes }) { return Promise.try(() => { // FIXME: Validatem if (/^[a-zA-Z0-9_][a-zA-Z0-9+_.-]*$/.test(name)) { return execBinary("vgcreate", [name, ...physicalVolumes]) .asRoot() .requireResult() .withModifier(unattendedFlags) .expectOnStderr(createRegexParser(/A volume group called ([^"]+) already exists\./, () => { return errorResult(new errors.VolumeGroupExists(`A volume group with the name '${name}' already exists`, { volumeGroupName: name })); })) .expectOnStderr(createRegexParser(/WARNING: [a-z]+ signature detected on (.+) at offset/g, (matches) => { let failedDevices = matches.map((match) => match.subMatches[0]); let list = failedDevices.join(", "); return errorResult(new errors.PartitionExists(`Refused to create a Volume Group, as partitions or partition tables already exist on the following devices: ${list}`, { paths: failedDevices })); })) .expectOnStderr(createRegexParser(/Device (.+) not found\./g, (matches) => { let failedDevices = matches.map((match) => match.subMatches[0]); let list = failedDevices.join(", "); return errorResult(new errors.InvalidPath(`The following specified devices do not exist: ${list}`, { paths: failedDevices })); })) .expectOnStderr(createRegexParser(/Physical volume '([^']+)' is already in volume group '([^']+)'/g, (matches) => { let failedItems = matches.map((match) => { let [ device, volumeGroup ] = match.subMatches; return { device, volumeGroup }; }); let list = failedItems .map((item) => `${item.device} (${item.volumeGroup})`) .join(", "); return errorResult(new errors.PhysicalVolumeInUse(`The following specified Physical Volumes are already in use in another Volume Group: ${list}`, { volumes: failedItems })); })) .execute(); } else { throw new errors.InvalidName(`The specified Volume Group name '${name}' contains invalid characters`); } }).then((_output) => { return true; }); };