balena-cli/lib/actions/os.coffee

45 lines
1.2 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')
url = require('url')
resin = require('../resin')
connection = require('../connection/connection')
2014-12-05 15:51:52 +00:00
cli = require('../cli/cli')
2014-12-05 16:00:15 +00:00
ui = require('../ui')
2014-12-02 15:08:22 +00:00
exports.download = (id) ->
params =
2014-12-05 15:51:52 +00:00
network: cli.getArgument('network')
wifiSsid: cli.getArgument('wifiSsid')
wifiKey: cli.getArgument('wifiKey')
2014-12-02 15:08:22 +00:00
fileName = resin.os.generateCacheName(id, params)
2014-12-05 15:51:52 +00:00
outputFile = cli.getArgument('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) ->
connection.parseConnectionParameters(params, callback)
(parameters, callback) ->
parameters.appId = id
query = url.format(query: parameters)
2014-12-05 14:53:59 +00:00
downloadUrl = url.resolve(resin.settings.get('urls.download'), query)
2014-12-02 15:08:22 +00:00
return callback(null, downloadUrl)
(downloadUrl, callback) ->
2014-12-05 16:00:15 +00:00
ui.patterns.downloadFile(downloadUrl, outputFile, callback)
2014-12-02 15:08:22 +00:00
], (error) ->
resin.errors.handle(error) if error?
resin.log.info("\nFinished downloading #{outputFile}")