balena-cli/lib/actions/os.coffee

59 lines
1.7 KiB
CoffeeScript
Raw Normal View History

2014-12-02 15:08:22 +00:00
_ = require('lodash')
async = require('async')
path = require('path')
mkdirp = require('mkdirp')
ProgressBar = require('progress')
2015-01-08 12:04:37 +00:00
resin = require('resin-sdk')
2014-12-22 16:41:14 +00:00
log = require('../log/log')
permissions = require('../permissions/permissions')
2014-12-22 16:47:12 +00:00
errors = require('../errors/errors')
cache = require('../cache/cache')
2014-12-02 15:08:22 +00:00
exports.download = (params, options) ->
# TODO: Evaluate if ConnectionParams is a good name for this object
# as it includes an application id, which is not connection related
# Maybe we should move appId outside this class?
connectionParams = new resin.connection.ConnectionParams
network: options.network
wifiSsid: options.ssid
wifiKey: options.key
appId: params.id
# TODO: Change cache.generateCacheName to accept a ConnectionParams instance
# to avoid the complication of having to omit it from the object and pass
# as another parameter
fileName = cache.generateCacheName(params.id, _.omit(connectionParams, 'appId'))
2014-12-02 15:08:22 +00:00
outputFile = options.output or path.join(resin.settings.get('directories.os'), fileName)
2014-12-02 15:08:22 +00:00
async.waterfall [
(callback) ->
# We need to ensure this directory exists
2014-12-02 15:53:34 +00:00
mkdirp path.dirname(outputFile), (error) ->
2014-12-02 15:08:22 +00:00
return callback(error)
(callback) ->
bar = null
received = 0
resin.models.os.download connectionParams, outputFile, callback, (state) ->
# TODO: Allow quieting this progress bar
bar ?= new ProgressBar 'Downloading device OS [:bar] :percent :etas',
complete: '='
incomplete: ' '
width: 40
total: state.total
2014-12-02 15:08:22 +00:00
return if bar.complete or not state?
2014-12-02 15:08:22 +00:00
bar.tick(state.received - received)
received = state.received
2014-12-02 15:08:22 +00:00
], errors.handleCallback ->
2014-12-22 16:41:14 +00:00
log.info("\nFinished downloading #{outputFile}")