Skip to content

Commit

Permalink
Auto-merge for PR #255 via VersionBot
Browse files Browse the repository at this point in the history
feat(lib): Use lsblk directly, parse json output
  • Loading branch information
resin-io-modules-versionbot[bot] authored Jul 9, 2018
2 parents dbf349a + c653464 commit 24e6485
Show file tree
Hide file tree
Showing 21 changed files with 724 additions and 1,155 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file
automatically by Versionist. DO NOT EDIT THIS FILE MANUALLY!
This project adheres to [Semantic Versioning](http://semver.org/).

## v6.3.0 - 2018-07-09

* Fix(linux): Fix pair consolidation for lsblk #255 [Jonas Hermsmeier]
* Feat(lib): Use lsblk directly, parse json output #255 [Jonas Hermsmeier]

## v6.2.5 - 2018-07-06

* Fix(linux): Add flag to lsblk to list all devices #287 [Jonas Hermsmeier]
Expand Down
100 changes: 47 additions & 53 deletions lib/diskutil.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,57 +17,7 @@
'use strict';

const plist = require('fast-plist');
const childProcess = require('child_process');

const run = (cmd, argv, callback) => {
let stdout = '';
let stderr = '';

const proc = childProcess.spawn(cmd, argv)
.on('error', callback)
.on('exit', (code, signal) => {
let error = null;
let data = null;

if (code == null || code !== 0) {
error = error || new Error(`Command "${cmd} ${argv.join(' ')}" exited unexpectedly with "${code || signal}"`);
error.stderr = stderr;
}

// NOTE: diskutil outputs error data as plist if something
// goes wrong, hence we still attempt to parse it here
try {
data = plist.parse(stdout);
} catch (e) {
error = error || e;
}

// NOTE: `diskutil list -plist` can give back 'null' data when recovering
// from a system sleep / stand-by
if (data == null) {
error = error || new Error(`Command "${cmd} ${argv.join(' ')}" returned without data`);
}

callback(error, data);
});

proc.stdout.setEncoding('utf8');
proc.stderr.setEncoding('utf8');

proc.stdout.on('readable', function() {
let data = null;
while (data = this.read()) {
stdout += data;
}
});

proc.stderr.on('readable', function() {
let data = null;
while (data = this.read()) {
stderr += data;
}
});
};
const exec = require('./exec');

const asyncMap = (items, iter, callback) => {
const results = [];
Expand Down Expand Up @@ -150,8 +100,37 @@ const setMountpoints = (devices, list) => {
});
};

const listAll = (callback) => {
const cmd = 'diskutil';
const argv = [ 'list', '-plist' ];
exec(cmd, argv, (error, stdout) => {
if (error) {
callback(error);
return;
}

let data = null;

// NOTE: diskutil outputs error data as plist if something
// goes wrong, hence we still attempt to parse it here
try {
data = plist.parse(stdout);
} catch (e) {
error = error || e;
}

// NOTE: `diskutil list -plist` can give back 'null' data when recovering
// from a system sleep / stand-by
if (data == null) {
error = error || new Error(`Command "${cmd} ${argv.join(' ')}" returned without data`);
}

callback(error, data);
});
};

const list = (callback) => {
run('diskutil', [ 'list', '-plist' ], (listError, globalList) => {
listAll((listError, globalList) => {
if (listError) {
callback(listError);
return;
Expand All @@ -162,7 +141,22 @@ const list = (callback) => {
});

asyncMap(tasks, (devicePath, next) => {
run('diskutil', [ 'info', '-plist', devicePath ], next);
exec('diskutil', [ 'info', '-plist', devicePath ], (error, stdout) => {
let data = null;
try {
data = plist.parse(stdout);
} catch (e) {
return next(e);
}

// NOTE: `diskutil` can return 'null' when recovering
// from a system sleep / stand-by
if (data == null) {
error = error || new Error(`Command "diskutil info -plist ${devicePath}}" returned without data`);
}

next(error, data);
});
}, (infoErrors, results) => {
const devices = results.filter((device, index) => {
return device && !infoErrors[index];
Expand Down
18 changes: 2 additions & 16 deletions lib/drivelist.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@

const os = require('os');
const bindings = require('bindings');
const parse = require('./parse');
const execute = require('./execute');
const scripts = require('./scripts.json');
const diskutil = require('./diskutil');
const lsblk = require('./lsblk');

/**
* @summary List available drives
Expand Down Expand Up @@ -57,19 +55,7 @@ exports.list = (callback) => {
diskutil.list(callback);
break;
case 'linux':
if (!scripts[os.platform()]) {
callback(new Error(`Your OS is not supported by this module: ${os.platform()}`));
return;
}

execute.extractAndRun(scripts[os.platform()], (error, output) => {
if (error) {
callback(error);
return;
}

callback(null, parse(output));
});
lsblk(callback);
break;
default:
return callback(new Error(`Your OS is not supported by this module: ${os.platform()}`));
Expand Down
59 changes: 59 additions & 0 deletions lib/exec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2018 Resin.io
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

const childProcess = require('child_process');

const exec = (cmd, argv, callback) => {
let stdout = '';
let stderr = '';

const proc = childProcess.spawn(cmd, argv)
.on('error', callback)
.on('exit', (code, signal) => {
let error = null;

if (code == null || code !== 0) {
error = error || new Error(`Command "${cmd} ${argv.join(' ')}" exited unexpectedly with "${code || signal}"`);
error.code = code;
error.signal = signal;
error.stdout = stdout;
error.stderr = stderr;
}

callback(error, stdout, stderr);
});

proc.stdout.setEncoding('utf8');
proc.stderr.setEncoding('utf8');

proc.stdout.on('readable', function() {
let data = null;
while (data = this.read()) {
stdout += data;
}
});

proc.stderr.on('readable', function() {
let data = null;
while (data = this.read()) {
stderr += data;
}
});
};

module.exports = exec;
Loading

0 comments on commit 24e6485

Please sign in to comment.