diff --git a/build/actions/config.js b/build/actions/config.js index a6b00dc2..c1523f2e 100644 --- a/build/actions/config.js +++ b/build/actions/config.js @@ -16,6 +16,10 @@ limitations under the License. */ (function() { + var commandOptions; + + commandOptions = require('./command-options'); + exports.read = { signature: 'config read', description: 'read a device configuration', @@ -189,11 +193,11 @@ limitations under the License. }; exports.generate = { - signature: 'config generate ', + signature: 'config generate', description: 'generate a config.json file', - help: 'Use this command to generate a config.json for a device\n\nExamples:\n\n $ resin config generate 7cf02a6\n $ resin config generate 7cf02a6 --output config.json', + help: 'Use this command to generate a config.json for a device or application\n\nExamples:\n\n $ resin config generate --device 7cf02a6\n $ resin config generate --device 7cf02a6 --output config.json\n $ resin config generate --app MyApp\n $ resin config generate --app MyApp --output config.json', options: [ - { + commandOptions.optionalApplication, commandOptions.optionalDevice, { signature: 'output', description: 'output', parameter: 'output', @@ -210,8 +214,21 @@ limitations under the License. form = require('resin-cli-form'); deviceConfig = require('resin-device-config'); prettyjson = require('prettyjson'); - return resin.models.device.get(params.uuid).then(function(device) { - return resin.models.device.getManifestBySlug(device.device_type).get('options').then(form.run).then(_.partial(deviceConfig.getByDevice, device.uuid)); + if ((options.device == null) && (options.application == null)) { + throw new Error('You have to pass either a device or an application.\n\nSee the help page for examples:\n\n $ resin help config generate'); + } + return Promise["try"](function() { + if (options.device != null) { + return resin.models.device.get(options.device); + } + return resin.models.application.get(options.application); + }).then(function(resource) { + return resin.models.device.getManifestBySlug(resource.device_type).get('options').then(form.run).then(function(answers) { + if (resource.uuid != null) { + return deviceConfig.getByDevice(resource.uuid, answers); + } + return deviceConfig.getByApplication(resource.app_name, answers); + }); }).then(function(config) { if (options.output != null) { return fs.writeFileAsync(options.output, JSON.stringify(config)); diff --git a/lib/actions/config.coffee b/lib/actions/config.coffee index 91e697b0..4070c772 100644 --- a/lib/actions/config.coffee +++ b/lib/actions/config.coffee @@ -14,6 +14,8 @@ See the License for the specific language governing permissions and limitations under the License. ### +commandOptions = require('./command-options') + exports.read = signature: 'config read' description: 'read a device configuration' @@ -214,17 +216,21 @@ exports.reconfigure = .nodeify(done) exports.generate = - signature: 'config generate ' + signature: 'config generate' description: 'generate a config.json file' help: ''' - Use this command to generate a config.json for a device + Use this command to generate a config.json for a device or application Examples: - $ resin config generate 7cf02a6 - $ resin config generate 7cf02a6 --output config.json + $ resin config generate --device 7cf02a6 + $ resin config generate --device 7cf02a6 --output config.json + $ resin config generate --app MyApp + $ resin config generate --app MyApp --output config.json ''' options: [ + commandOptions.optionalApplication + commandOptions.optionalDevice { signature: 'output' description: 'output' @@ -242,11 +248,27 @@ exports.generate = deviceConfig = require('resin-device-config') prettyjson = require('prettyjson') - resin.models.device.get(params.uuid).then (device) -> - resin.models.device.getManifestBySlug(device.device_type) + if not options.device? and not options.application? + throw new Error ''' + You have to pass either a device or an application. + + See the help page for examples: + + $ resin help config generate + ''' + + Promise.try -> + if options.device? + return resin.models.device.get(options.device) + return resin.models.application.get(options.application) + .then (resource) -> + resin.models.device.getManifestBySlug(resource.device_type) .get('options') .then(form.run) - .then(_.partial(deviceConfig.getByDevice, device.uuid)) + .then (answers) -> + if resource.uuid? + return deviceConfig.getByDevice(resource.uuid, answers) + return deviceConfig.getByApplication(resource.app_name, answers) .then (config) -> if options.output? return fs.writeFileAsync(options.output, JSON.stringify(config))