From fc6dda5d22772b6fef2e841b326c60f8f69e69b8 Mon Sep 17 00:00:00 2001
From: Juan Cruz Viotti <juanchiviotti@gmail.com>
Date: Fri, 30 Jan 2015 11:38:46 -0400
Subject: [PATCH] Make use of published drivelist

---
 lib/actions/drive.coffee    |  4 +--
 lib/drive/drive.coffee      | 12 +-------
 lib/drive/linux.coffee      | 22 --------------
 lib/drive/linux.spec.coffee | 46 ------------------------------
 lib/drive/osx.coffee        | 36 -----------------------
 lib/drive/osx.spec.coffee   | 57 -------------------------------------
 lib/drive/win32.coffee      | 22 --------------
 lib/drive/win32.spec.coffee | 40 --------------------------
 package.json                |  2 +-
 9 files changed, 4 insertions(+), 237 deletions(-)
 delete mode 100644 lib/drive/linux.coffee
 delete mode 100644 lib/drive/linux.spec.coffee
 delete mode 100644 lib/drive/osx.coffee
 delete mode 100644 lib/drive/osx.spec.coffee
 delete mode 100644 lib/drive/win32.spec.coffee

diff --git a/lib/actions/drive.coffee b/lib/actions/drive.coffee
index e8a8af3d..0450abd4 100644
--- a/lib/actions/drive.coffee
+++ b/lib/actions/drive.coffee
@@ -1,5 +1,5 @@
 visuals = require('resin-cli-visuals')
-drive = require('../drive/drive')
+drivelist = require('drivelist')
 
 exports.list =
 	signature: 'drives'
@@ -12,7 +12,7 @@ exports.list =
 	'''
 	permission: 'user'
 	action: (params, options, done) ->
-		drive.listDrives (error, drives) ->
+		drivelist.list (error, drives) ->
 			return done(error) if error?
 
 			console.log visuals.widgets.table.horizontal drives, [
diff --git a/lib/drive/drive.coffee b/lib/drive/drive.coffee
index 01630061..57145e18 100644
--- a/lib/drive/drive.coffee
+++ b/lib/drive/drive.coffee
@@ -1,13 +1,11 @@
 os = require('os')
 fs = require('fs')
-_ = require('lodash-contrib')
+_ = require('lodash')
 async = require('async')
 
 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) ->
@@ -57,11 +55,3 @@ exports.writeImage = (devicePath, imagePath, options = {}, callback = _.noop) ->
 			delete error.code
 
 		return callback(error)
-
-exports.listDrives = (callback) ->
-	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
deleted file mode 100644
index e62c9c43..00000000
--- a/lib/drive/linux.coffee
+++ /dev/null
@@ -1,22 +0,0 @@
-_ = 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
deleted file mode 100644
index d403e505..00000000
--- a/lib/drive/linux.spec.coffee
+++ /dev/null
@@ -1,46 +0,0 @@
-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()
diff --git a/lib/drive/osx.coffee b/lib/drive/osx.coffee
deleted file mode 100644
index 71ceea31..00000000
--- a/lib/drive/osx.coffee
+++ /dev/null
@@ -1,36 +0,0 @@
-_ = require('lodash')
-os = require('os')
-childProcess = require('child_process')
-tableParser = require('table-parser')
-
-exports.list = (callback) ->
-	childProcess.exec 'diskutil list', {}, (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 _.compact _.flatten _.values(row)
-
-		result = _.filter result, (row) ->
-			return row[0] is '0:'
-
-		result = _.map result, (row) ->
-			return _.rest(row)
-
-		result = _.map result, (row) ->
-
-			device = row.pop()
-			sizeMeasure = row.pop()
-			size = row.pop()
-
-			return {
-				device: "/dev/#{device}"
-				size: "#{size} #{sizeMeasure}"
-				description: row.join(' ')
-			}
-
-		return callback(null, result)
diff --git a/lib/drive/osx.spec.coffee b/lib/drive/osx.spec.coffee
deleted file mode 100644
index b0f89020..00000000
--- a/lib/drive/osx.spec.coffee
+++ /dev/null
@@ -1,57 +0,0 @@
-chai = require('chai')
-expect = chai.expect
-sinon = require('sinon')
-chai.use(require('sinon-chai'))
-childProcess = require('child_process')
-osx = require('./osx')
-
-describe 'Drive OSX:', ->
-
-	describe 'given correct output from diskdrive', ->
-
-		beforeEach ->
-			@childProcessStub = sinon.stub(childProcess, 'exec')
-			@childProcessStub.yields null, '''
-				/dev/disk0
-					 #:                       TYPE NAME                    SIZE       IDENTIFIER
-					 0:      GUID_partition_scheme                        *750.2 GB   disk0
-					 1:                        EFI EFI                     209.7 MB   disk0s1
-					 2:          Apple_CoreStorage                         749.3 GB   disk0s2
-					 3:                 Apple_Boot Recovery HD             650.0 MB   disk0s3
-				/dev/disk1
-					 #:                       TYPE NAME                    SIZE       IDENTIFIER
-					 0:                  Apple_HFS Macintosh HD           *748.9 GB   disk1
-																				 Logical Volume on disk0s2
-																				 3D74D961-80FB-4DB1-808F-8B5800C53E3A
-																				 Unlocked Encrypted
-				/dev/disk2
-					 #:                       TYPE NAME                    SIZE       IDENTIFIER
-					 0:                            elementary OS          *15.7 GB    disk2
-			''', undefined
-
-		afterEach ->
-			@childProcessStub.restore()
-
-		it 'should extract the necessary information', (done) ->
-			osx.list (error, drives) ->
-				expect(error).to.not.exist
-
-				expect(drives).to.deep.equal [
-					{
-						device: '/dev/disk0'
-						description: 'GUID_partition_scheme'
-						size: '*750.2 GB'
-					}
-					{
-						device: '/dev/disk1'
-						description: 'Apple_HFS Macintosh HD'
-						size: '*748.9 GB'
-					}
-					{
-						device: '/dev/disk2'
-						description: 'elementary OS'
-						size: '*15.7 GB'
-					}
-				]
-
-				return done()
diff --git a/lib/drive/win32.coffee b/lib/drive/win32.coffee
index 6a71576a..69fcf44f 100644
--- a/lib/drive/win32.coffee
+++ b/lib/drive/win32.coffee
@@ -1,10 +1,8 @@
-os = require('os')
 childProcess = require('child_process')
 path = require('path')
 _ = require('lodash-contrib')
 async = require('async')
 fs = require('fs')
-tableParser = require('table-parser')
 
 scriptsPath = path.join(__dirname, 'scripts')
 diskpartRescanScriptPath = path.join(scriptsPath, 'diskpart_rescan')
@@ -36,23 +34,3 @@ exports.eraseMBR = (devicePath, callback) ->
 			fs.close(fd, callback)
 
 	], callback)
-
-exports.list = (callback) ->
-	childProcess.exec 'wmic diskdrive get DeviceID, Caption, 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) ->
-			size = _.parseInt(row.Size[0]) / 1e+9
-
-			return {
-				device: _.first(row.DeviceID)
-				size: "#{Math.floor(size)} GB"
-				description: row.Caption.join(' ')
-			}
-
-		return callback(null, result)
diff --git a/lib/drive/win32.spec.coffee b/lib/drive/win32.spec.coffee
deleted file mode 100644
index 51ab1688..00000000
--- a/lib/drive/win32.spec.coffee
+++ /dev/null
@@ -1,40 +0,0 @@
-chai = require('chai')
-expect = chai.expect
-sinon = require('sinon')
-chai.use(require('sinon-chai'))
-childProcess = require('child_process')
-win32 = require('./win32')
-
-describe 'Drive WIN32:', ->
-
-	describe 'given correct output from wmic', ->
-
-		beforeEach ->
-			@childProcessStub = sinon.stub(childProcess, 'exec')
-			@childProcessStub.yields null, '''
-				Caption                            DeviceID               Size
-				WDC WD10JPVX-75JC3T0               \\\\.\\PHYSICALDRIVE0  1000202273280
-				Generic STORAGE DEVICE USB Device  \\\\.\\PHYSICALDRIVE1  15718510080
-			''', undefined
-
-		afterEach ->
-			@childProcessStub.restore()
-
-		it 'should extract the necessary information', (done) ->
-			win32.list (error, drives) ->
-				expect(error).to.not.exist
-
-				expect(drives).to.deep.equal [
-					{
-						device: '\\\\.\\PHYSICALDRIVE0'
-						description: 'WDC WD10JPVX-75JC3T0'
-						size: '1000 GB'
-					}
-					{
-						device: '\\\\.\\PHYSICALDRIVE1'
-						description: 'Generic STORAGE DEVICE USB Device'
-						size: '15 GB'
-					}
-				]
-
-				return done()
diff --git a/package.json b/package.json
index 1be24133..7375958f 100644
--- a/package.json
+++ b/package.json
@@ -49,6 +49,7 @@
     "capitano": "~1.3.0",
     "coffee-script": "~1.8.0",
     "conf.js": "^0.1.1",
+    "drivelist": "^1.0.0",
     "git-cli": "~0.8.2",
     "lodash": "~2.4.1",
     "lodash-contrib": "~241.4.14",
@@ -58,7 +59,6 @@
     "progress-stream": "^0.5.0",
     "resin-cli-visuals": "0.0.3",
     "resin-sdk": "git+https://git@github.com/resin-io/resin-sdk.git",
-    "table-parser": "0.0.3",
     "underscore.string": "~2.4.0"
   }
 }