Make os download command accept an application name instead of id

This commit is contained in:
Juan Cruz Viotti 2015-04-20 09:06:40 -04:00
parent ac115c7ea3
commit 85444a5a6a
2 changed files with 95 additions and 87 deletions

View File

@ -26,9 +26,9 @@
elevate = require('../elevate'); elevate = require('../elevate');
exports.download = { exports.download = {
signature: 'os download <id>', signature: 'os download <name>',
description: 'download device OS', description: 'download device OS',
help: 'Use this command to download the device OS configured to a specific network.\n\nEthernet:\n You can setup the device OS to use ethernet by setting the `--network` option to "ethernet".\n\nWifi:\n You can setup the device OS to use wifi by setting the `--network` option to "wifi".\n If you set "network" to "wifi", you will need to specify the `--ssid` and `--key` option as well.\n\nAlternatively, you can omit all kind of network configuration options to configure interactively.\n\nYou have to specify an output location with the `--output` option.\n\nExamples:\n\n $ resin os download 91 --output ~/MyResinOS.zip\n $ resin os download 91 --network ethernet --output ~/MyResinOS.zip\n $ resin os download 91 --network wifi --ssid MyNetwork --key secreykey123 --output ~/MyResinOS.zip\n $ resin os download 91 --network ethernet --output ~/MyResinOS.zip', help: 'Use this command to download the device OS configured to a specific network.\n\nEthernet:\n You can setup the device OS to use ethernet by setting the `--network` option to "ethernet".\n\nWifi:\n You can setup the device OS to use wifi by setting the `--network` option to "wifi".\n If you set "network" to "wifi", you will need to specify the `--ssid` and `--key` option as well.\n\nAlternatively, you can omit all kind of network configuration options to configure interactively.\n\nYou have to specify an output location with the `--output` option.\n\nExamples:\n\n $ resin os download MyApp --output ~/MyResinOS.zip\n $ resin os download MyApp --network ethernet --output ~/MyResinOS.zip\n $ resin os download MyApp --network wifi --ssid MyNetwork --key secreykey123 --output ~/MyResinOS.zip\n $ resin os download MyApp --network ethernet --output ~/MyResinOS.zip',
options: [ options: [
commandOptions.network, commandOptions.wifiSsid, commandOptions.wifiKey, { commandOptions.network, commandOptions.wifiSsid, commandOptions.wifiKey, {
signature: 'output', signature: 'output',
@ -40,51 +40,56 @@
], ],
permission: 'user', permission: 'user',
action: function(params, options, done) { action: function(params, options, done) {
var osParams; return resin.models.application.get(params.name, function(error, application) {
osParams = { var osParams;
network: options.network,
wifiSsid: options.ssid,
wifiKey: options.key,
appId: params.id
};
return async.waterfall([
function(callback) {
if (osParams.network != null) {
return callback();
}
return visuals.patterns.selectNetworkParameters(function(error, parameters) {
if (error != null) {
return callback(error);
}
_.extend(osParams, parameters);
return callback();
});
}, function(callback) {
return mkdirp(path.dirname(options.output), _.unary(callback));
}, function(callback) {
var bar, spinner;
console.info("Destination file: " + options.output + "\n");
bar = new visuals.widgets.Progress('Downloading Device OS');
spinner = new visuals.widgets.Spinner('Downloading Device OS (size unknown)');
return resin.models.os.download(osParams, options.output, function(error) {
spinner.stop();
if (error != null) {
return callback(error);
}
}, function(state) {
if (state != null) {
return bar.update(state);
} else {
return spinner.start();
}
});
}
], function(error) {
if (error != null) { if (error != null) {
return done(error); return done(error);
} }
console.info("\nFinished downloading " + options.output); osParams = {
return done(null, options.output); network: options.network,
wifiSsid: options.ssid,
wifiKey: options.key,
appId: application.id
};
return async.waterfall([
function(callback) {
if (osParams.network != null) {
return callback();
}
return visuals.patterns.selectNetworkParameters(function(error, parameters) {
if (error != null) {
return callback(error);
}
_.extend(osParams, parameters);
return callback();
});
}, function(callback) {
return mkdirp(path.dirname(options.output), _.unary(callback));
}, function(callback) {
var bar, spinner;
console.info("Destination file: " + options.output + "\n");
bar = new visuals.widgets.Progress('Downloading Device OS');
spinner = new visuals.widgets.Spinner('Downloading Device OS (size unknown)');
return resin.models.os.download(osParams, options.output, function(error) {
spinner.stop();
if (error != null) {
return callback(error);
}
}, function(state) {
if (state != null) {
return bar.update(state);
} else {
return spinner.start();
}
});
}
], function(error) {
if (error != null) {
return done(error);
}
console.info("\nFinished downloading " + options.output);
return done(null, options.output);
});
}); });
} }
}; };

View File

@ -12,7 +12,7 @@ updateActions = require('./update')
elevate = require('../elevate') elevate = require('../elevate')
exports.download = exports.download =
signature: 'os download <id>' signature: 'os download <name>'
description: 'download device OS' description: 'download device OS'
help: ''' help: '''
Use this command to download the device OS configured to a specific network. Use this command to download the device OS configured to a specific network.
@ -30,10 +30,10 @@ exports.download =
Examples: Examples:
$ resin os download 91 --output ~/MyResinOS.zip $ resin os download MyApp --output ~/MyResinOS.zip
$ resin os download 91 --network ethernet --output ~/MyResinOS.zip $ resin os download MyApp --network ethernet --output ~/MyResinOS.zip
$ resin os download 91 --network wifi --ssid MyNetwork --key secreykey123 --output ~/MyResinOS.zip $ resin os download MyApp --network wifi --ssid MyNetwork --key secreykey123 --output ~/MyResinOS.zip
$ resin os download 91 --network ethernet --output ~/MyResinOS.zip $ resin os download MyApp --network ethernet --output ~/MyResinOS.zip
''' '''
options: [ options: [
commandOptions.network commandOptions.network
@ -50,45 +50,48 @@ exports.download =
] ]
permission: 'user' permission: 'user'
action: (params, options, done) -> action: (params, options, done) ->
osParams = resin.models.application.get params.name, (error, application) ->
network: options.network
wifiSsid: options.ssid
wifiKey: options.key
appId: params.id
async.waterfall [
(callback) ->
return callback() if osParams.network?
visuals.patterns.selectNetworkParameters (error, parameters) ->
return callback(error) if error?
_.extend(osParams, parameters)
return callback()
(callback) ->
# We need to ensure this directory exists
mkdirp(path.dirname(options.output), _.unary(callback))
(callback) ->
console.info("Destination file: #{options.output}\n")
bar = new visuals.widgets.Progress('Downloading Device OS')
spinner = new visuals.widgets.Spinner('Downloading Device OS (size unknown)')
resin.models.os.download osParams, options.output, (error) ->
spinner.stop()
return callback(error) if error?
, (state) ->
if state?
bar.update(state)
else
spinner.start()
], (error) ->
return done(error) if error? return done(error) if error?
console.info("\nFinished downloading #{options.output}")
return done(null, options.output) osParams =
network: options.network
wifiSsid: options.ssid
wifiKey: options.key
appId: application.id
async.waterfall [
(callback) ->
return callback() if osParams.network?
visuals.patterns.selectNetworkParameters (error, parameters) ->
return callback(error) if error?
_.extend(osParams, parameters)
return callback()
(callback) ->
# We need to ensure this directory exists
mkdirp(path.dirname(options.output), _.unary(callback))
(callback) ->
console.info("Destination file: #{options.output}\n")
bar = new visuals.widgets.Progress('Downloading Device OS')
spinner = new visuals.widgets.Spinner('Downloading Device OS (size unknown)')
resin.models.os.download osParams, options.output, (error) ->
spinner.stop()
return callback(error) if error?
, (state) ->
if state?
bar.update(state)
else
spinner.start()
], (error) ->
return done(error) if error?
console.info("\nFinished downloading #{options.output}")
return done(null, options.output)
exports.install = exports.install =
signature: 'os install <image> [device]' signature: 'os install <image> [device]'