From ac78a8bc0221a467be8922f5a8b35f5d9f219370 Mon Sep 17 00:00:00 2001 From: Juan Cruz Viotti Date: Fri, 30 Jan 2015 10:30:28 -0400 Subject: [PATCH] Implement linux.list() --- lib/drive/drive.coffee | 16 ++++++++----- lib/drive/linux.coffee | 22 ++++++++++++++++++ lib/drive/linux.spec.coffee | 46 +++++++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+), 6 deletions(-) create mode 100644 lib/drive/linux.coffee create mode 100644 lib/drive/linux.spec.coffee diff --git a/lib/drive/drive.coffee b/lib/drive/drive.coffee index 876f98cb..a24a894f 100644 --- a/lib/drive/drive.coffee +++ b/lib/drive/drive.coffee @@ -7,6 +7,7 @@ IS_WINDOWS = os.platform() is 'win32' win32 = require('./win32') osx = require('./osx') +linux = require('./linux') agnostic = require('./agnostic') exports.writeImage = (devicePath, imagePath, options = {}, callback = _.noop) -> @@ -58,9 +59,12 @@ exports.writeImage = (devicePath, imagePath, options = {}, callback = _.noop) -> return callback(error) exports.listDrives = (callback) -> - if os.platform() is 'darwin' - osx.list(callback) - else if os.platform() is 'win32' - win32.list(callback) - else - throw new Error('Your OS is not supported by this module') + switch os.platform() + when 'darwin' then + osx.list(callback) + when 'win32' then + win32.list(callback) + when 'linux' then + linux.list(callback) + else + throw new Error('Your OS is not supported by this module') diff --git a/lib/drive/linux.coffee b/lib/drive/linux.coffee new file mode 100644 index 00000000..e62c9c43 --- /dev/null +++ b/lib/drive/linux.coffee @@ -0,0 +1,22 @@ +_ = require('lodash') +os = require('os') +childProcess = require('child_process') +tableParser = require('table-parser') + +exports.list = (callback) -> + childProcess.exec 'lsblk -d --output NAME,MODEL,SIZE', {}, (error, stdout, stderr) -> + return callback(error) if error? + + if not _.isEmpty(stderr) + return callback(new Error(stderr)) + + result = tableParser.parse(stdout) + + result = _.map result, (row) -> + return { + device: "/dev/#{_.first(row.NAME)}" + description: row.MODEL.join(' ') + size: _.first(row.SIZE).replace(/,/g, '.') + } + + return callback(null, result) diff --git a/lib/drive/linux.spec.coffee b/lib/drive/linux.spec.coffee new file mode 100644 index 00000000..d403e505 --- /dev/null +++ b/lib/drive/linux.spec.coffee @@ -0,0 +1,46 @@ +chai = require('chai') +expect = chai.expect +sinon = require('sinon') +chai.use(require('sinon-chai')) +childProcess = require('child_process') +linux = require('./linux') + +describe 'Drive LINUX:', -> + + describe 'given correct output from lsblk', -> + + beforeEach -> + @childProcessStub = sinon.stub(childProcess, 'exec') + @childProcessStub.yields null, ''' + NAME MODEL SIZE + sda WDC WD10JPVX-75J 931,5G + sdb STORAGE DEVICE 14,7G + sr0 DVD+-RW GU90N 1024M + ''', undefined + + afterEach -> + @childProcessStub.restore() + + it 'should extract the necessary information', (done) -> + linux.list (error, drives) -> + expect(error).to.not.exist + + expect(drives).to.deep.equal [ + { + device: '/dev/sda' + description: 'WDC WD10JPVX-75J' + size: '931.5G' + } + { + device: '/dev/sdb' + description: 'STORAGE DEVICE' + size: '14.7G' + } + { + device: '/dev/sr0' + description: 'DVD+-RW GU90N' + size: '1024M' + } + ] + + return done()