From 9f4ae4ac3fff88e6ed5ad626a62b46fb5ac42f84 Mon Sep 17 00:00:00 2001 From: Juan Cruz Viotti Date: Tue, 3 Feb 2015 13:31:16 -0400 Subject: [PATCH] Rename current device init to os download --- lib/actions/device.coffee | 94 -------------------------------------- lib/actions/os.coffee | 95 +++++++++++++++++++++++++++++++++++++++ lib/app.coffee | 2 +- 3 files changed, 96 insertions(+), 95 deletions(-) diff --git a/lib/actions/device.coffee b/lib/actions/device.coffee index c516bb06..d35917ab 100644 --- a/lib/actions/device.coffee +++ b/lib/actions/device.coffee @@ -2,12 +2,7 @@ _ = require('lodash-contrib') path = require('path') async = require('async') resin = require('resin-sdk') -os = require('os') visuals = require('resin-cli-visuals') -fs = require('fs') -progressStream = require('progress-stream') -drivelist = require('drivelist') -diskio = require('diskio') commandOptions = require('./command-options') exports.list = @@ -141,92 +136,3 @@ exports.supported = action: -> devices = resin.models.device.getSupportedDeviceTypes() _.each(devices, _.unary(console.log)) - -exports.init = - signature: 'device init [device]' - description: 'write an operating system image to a device' - help: ''' - Use this command to write an operating system image to a device. - - Note that this command requires admin privileges. - - If `device` is omitted, you will be prompted to select a device interactively. - - Notice this command asks for confirmation interactively. - You can avoid this by passing the `--yes` boolean option. - - You can quiet the progress bar by passing the `--quiet` boolean option. - - You may have to unmount the device before attempting this operation. - - See the `drives` command to get a list of all connected devices to your machine and their respective ids. - - In Mac OS X: - $ sudo diskutil unmountDisk /dev/xxx - - In GNU/Linux: - $ sudo umount /dev/xxx - - Examples: - $ resin device init rpi.iso /dev/disk2 - ''' - options: [ commandOptions.yes ] - permission: 'user' - action: (params, options, done) -> - async.waterfall [ - - (callback) -> - return callback(null, params.device) if params.device? - - drivelist.list (error, drives) -> - return callback(error) if error? - - drives = _.map drives, (item) -> - return { - name: "#{item.device} (#{item.size}) - #{item.description}" - value: item.device - } - - visuals.widgets.select('Select a drive', drives, callback) - - (device, callback) -> - params.device = device - - if options.yes - return callback(null, true) - else - confirmMessage = "This will completely erase #{params.device}. Are you sure you want to continue?" - visuals.widgets.confirm(confirmMessage, callback) - - (confirmed, callback) -> - return done() if not confirmed - - imageFileSize = fs.statSync(params.image).size - - if imageFileSize is 0 - error = new Error("Invalid OS image: #{params.image}. The image is 0 bytes.") - return callback(error) - - progress = progressStream - length: imageFileSize - time: 500 - - if not options.quiet - bar = new visuals.widgets.Progress('Writing Device OS') - - progress.on 'progress', (status) -> - console.log(bar.tick(status.percentage, status.eta)) - - imageFileStream = fs.createReadStream(params.image).pipe(progress) - - diskio.writeStream(params.device, imageFileStream, callback) - - ], (error) -> - if os.platform() is 'win32' and error? and (error.code is 'EPERM' or error.code is 'EACCES') - windosu = require('windosu') - - # Need to escape everypath to avoid errors - resinWritePath = "\"#{path.join(__dirname, '..', '..', 'bin', 'resin-write')}\"" - windosu.exec("node #{resinWritePath} \"#{params.image}\" \"#{params.device}\"") - else - return done(error) diff --git a/lib/actions/os.coffee b/lib/actions/os.coffee index 14336901..df4e3141 100644 --- a/lib/actions/os.coffee +++ b/lib/actions/os.coffee @@ -1,9 +1,15 @@ _ = require('lodash-contrib') +fs = require('fs') +os = require('os') async = require('async') path = require('path') mkdirp = require('mkdirp') resin = require('resin-sdk') visuals = require('resin-cli-visuals') +progressStream = require('progress-stream') +drivelist = require('drivelist') +diskio = require('diskio') +commandOptions = require('./command-options') exports.download = signature: 'os download ' @@ -86,3 +92,92 @@ exports.download = return done(error) if error? console.info("\nFinished downloading #{outputFile}") return done() + +exports.install = + signature: 'os install [device]' + description: 'write an operating system image to a device' + help: ''' + Use this command to write an operating system image to a device. + + Note that this command requires admin privileges. + + If `device` is omitted, you will be prompted to select a device interactively. + + Notice this command asks for confirmation interactively. + You can avoid this by passing the `--yes` boolean option. + + You can quiet the progress bar by passing the `--quiet` boolean option. + + You may have to unmount the device before attempting this operation. + + See the `drives` command to get a list of all connected devices to your machine and their respective ids. + + In Mac OS X: + $ sudo diskutil unmountDisk /dev/xxx + + In GNU/Linux: + $ sudo umount /dev/xxx + + Examples: + $ resin os install rpi.iso /dev/disk2 + ''' + options: [ commandOptions.yes ] + permission: 'user' + action: (params, options, done) -> + async.waterfall [ + + (callback) -> + return callback(null, params.device) if params.device? + + drivelist.list (error, drives) -> + return callback(error) if error? + + drives = _.map drives, (item) -> + return { + name: "#{item.device} (#{item.size}) - #{item.description}" + value: item.device + } + + visuals.widgets.select('Select a drive', drives, callback) + + (device, callback) -> + params.device = device + + if options.yes + return callback(null, true) + else + confirmMessage = "This will completely erase #{params.device}. Are you sure you want to continue?" + visuals.widgets.confirm(confirmMessage, callback) + + (confirmed, callback) -> + return done() if not confirmed + + imageFileSize = fs.statSync(params.image).size + + if imageFileSize is 0 + error = new Error("Invalid OS image: #{params.image}. The image is 0 bytes.") + return callback(error) + + progress = progressStream + length: imageFileSize + time: 500 + + if not options.quiet + bar = new visuals.widgets.Progress('Writing Device OS') + + progress.on 'progress', (status) -> + console.log(bar.tick(status.percentage, status.eta)) + + imageFileStream = fs.createReadStream(params.image).pipe(progress) + + diskio.writeStream(params.device, imageFileStream, callback) + + ], (error) -> + if os.platform() is 'win32' and error? and (error.code is 'EPERM' or error.code is 'EACCES') + windosu = require('windosu') + + # Need to escape everypath to avoid errors + resinWritePath = "\"#{path.join(__dirname, '..', '..', 'bin', 'resin-write')}\"" + windosu.exec("node #{resinWritePath} \"#{params.image}\" \"#{params.device}\"") + else + return done(error) diff --git a/lib/app.coffee b/lib/app.coffee index a26c632d..ae37cc14 100644 --- a/lib/app.coffee +++ b/lib/app.coffee @@ -62,7 +62,6 @@ capitano.command(actions.app.init) capitano.command(actions.device.list) capitano.command(actions.device.supported) capitano.command(actions.device.rename) -capitano.command(actions.device.init) capitano.command(actions.device.info) capitano.command(actions.device.remove) capitano.command(actions.device.identify) @@ -93,6 +92,7 @@ capitano.command(actions.logs.logs) # ---------- OS Module ---------- capitano.command(actions.os.download) +capitano.command(actions.os.install) # ---------- Examples Module ---------- capitano.command(actions.examples.list)