Implement linux.list()

This commit is contained in:
Juan Cruz Viotti 2015-01-30 10:30:28 -04:00
parent a220bc87ad
commit ac78a8bc02
3 changed files with 78 additions and 6 deletions

View File

@ -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')

22
lib/drive/linux.coffee Normal file
View File

@ -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)

View File

@ -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()