diff --git a/build/actions/os.js b/build/actions/os.js index 8c409852..6a868c76 100644 --- a/build/actions/os.js +++ b/build/actions/os.js @@ -188,36 +188,43 @@ exports.buildConfig = { }; exports.configure = { - signature: 'os configure [deviceApiKey]', + signature: 'os configure [uuid] [deviceApiKey]', description: 'configure an os image', - help: 'Use this command to configure a previously downloaded operating system image for the specific device.\n\nNote that device api keys are only supported on ResinOS 2.0.3+\n\nExamples:\n\n $ resin os configure ../path/rpi.img 7cf02a6\n $ resin os configure ../path/rpi.img 7cf02a6 ', + help: 'Use this command to configure a previously downloaded operating system image for\nthe specific device or for an application generally.\n\nNote that device api keys are only supported on ResinOS 2.0.3+.\n\nThis comand still supports the *deprecated* format where the UUID and optionally device key\nare passed directly on the command line, but the recommended way is to pass either an --app or\n--device argument. The deprecated format will be remove in a future release.\n\nExamples:\n\n $ resin os configure ../path/rpi.img --device 7cf02a6\n $ resin os configure ../path/rpi.img --device 7cf02a6 --deviceApiKey \n $ resin os configure ../path/rpi.img --app MyApp', permission: 'user', options: [ - commandOptions.advancedConfig, { + commandOptions.advancedConfig, commandOptions.optionalApplication, commandOptions.optionalDevice, commandOptions.optionalDeviceApiKey, { signature: 'config', description: 'path to the config JSON file, see `resin os build-config`', parameter: 'config' } ], action: function(params, options, done) { - var Promise, fs, generateDeviceConfig, helpers, init, readFileAsync, resin; + var Promise, configurationResourceType, deviceApiKey, fs, generateApplicationConfig, generateDeviceConfig, helpers, init, patterns, readFileAsync, ref, resin, uuid; fs = require('fs'); Promise = require('bluebird'); readFileAsync = Promise.promisify(fs.readFile); resin = require('resin-sdk-preconfigured'); init = require('resin-device-init'); helpers = require('../utils/helpers'); - generateDeviceConfig = require('../utils/config').generateDeviceConfig; + patterns = require('../utils/patterns'); + ref = require('../utils/config'), generateDeviceConfig = ref.generateDeviceConfig, generateApplicationConfig = ref.generateApplicationConfig; + if (_.filter([options.device, options.application, params.uuid]).length !== 1) { + patterns.expectedError('To configure an image, you must provide exactly one of:\n\n* A device, with --device \n* An application, with --app \n* [Deprecated] A device, passing its uuid directly on the command line\n\nSee the help page for examples:\n\n $ resin help os configure'); + } + uuid = options.device || params.uuid; + deviceApiKey = options.deviceApiKey || params.deviceApiKey; console.info('Configuring operating system image'); - return resin.models.device.get(params.uuid).then(function(device) { + configurationResourceType = uuid ? 'device' : 'application'; + return resin.models[configurationResourceType].get(uuid || options.application).then(function(appOrDevice) { return Promise["try"](function() { if (options.config) { return readFileAsync(options.config, 'utf8').then(JSON.parse); } - return buildConfig(params.image, device.device_type, options.advanced); + return buildConfig(params.image, appOrDevice.device_type, options.advanced); }).then(function(answers) { - return generateDeviceConfig(device, params.deviceApiKey, answers).then(function(config) { - return init.configure(params.image, device.device_type, config, answers); + return (configurationResourceType === 'device' ? generateDeviceConfig(appOrDevice, deviceApiKey, answers) : generateApplicationConfig(appOrDevice, answers)).then(function(config) { + return init.configure(params.image, appOrDevice.device_type, config, answers); }); }); }).then(helpers.osProgressHandler).nodeify(done); diff --git a/doc/cli.markdown b/doc/cli.markdown index 18db89d2..a0fb4668 100644 --- a/doc/cli.markdown +++ b/doc/cli.markdown @@ -106,7 +106,7 @@ environment variable (in the same standard URL format). - [os versions <type>](#os-versions-60-type-62-) - [os download <type>](#os-download-60-type-62-) - [os build-config <image> <device-type>](#os-build-config-60-image-62-60-device-type-62-) - - [os configure <image> <uuid> [deviceApiKey]](#os-configure-60-image-62-60-uuid-62-deviceapikey-) + - [os configure <image> [uuid] [deviceApiKey]](#os-configure-60-image-62-uuid-deviceapikey-) - [os initialize <image>](#os-initialize-60-image-62-) - Config @@ -909,16 +909,22 @@ show advanced configuration options the path to the output JSON file -## os configure <image> <uuid> [deviceApiKey] +## os configure <image> [uuid] [deviceApiKey] -Use this command to configure a previously downloaded operating system image for the specific device. +Use this command to configure a previously downloaded operating system image for +the specific device or for an application generally. -Note that device api keys are only supported on ResinOS 2.0.3+ +Note that device api keys are only supported on ResinOS 2.0.3+. + +This comand still supports the *deprecated* format where the UUID and optionally device key +are passed directly on the command line, but the recommended way is to pass either an --app or +--device argument. The deprecated format will be remove in a future release. Examples: - $ resin os configure ../path/rpi.img 7cf02a6 - $ resin os configure ../path/rpi.img 7cf02a6 + $ resin os configure ../path/rpi.img --device 7cf02a6 + $ resin os configure ../path/rpi.img --device 7cf02a6 --deviceApiKey + $ resin os configure ../path/rpi.img --app MyApp ### Options @@ -926,6 +932,18 @@ Examples: show advanced configuration options +#### --application, --a,app, --a,app <application> + +application name + +#### --device, -d <device> + +device uuid + +#### --deviceApiKey, -k <device-api-key> + +custom device key - note that this is only supported on ResinOS 2.0.3+ + #### --config <config> path to the config JSON file, see `resin os build-config` diff --git a/lib/actions/os.coffee b/lib/actions/os.coffee index a5836b6b..ebb54463 100644 --- a/lib/actions/os.coffee +++ b/lib/actions/os.coffee @@ -196,21 +196,30 @@ exports.buildConfig = .nodeify(done) exports.configure = - signature: 'os configure [deviceApiKey]' + signature: 'os configure [uuid] [deviceApiKey]' description: 'configure an os image' help: ''' - Use this command to configure a previously downloaded operating system image for the specific device. + Use this command to configure a previously downloaded operating system image for + the specific device or for an application generally. - Note that device api keys are only supported on ResinOS 2.0.3+ + Note that device api keys are only supported on ResinOS 2.0.3+. + + This comand still supports the *deprecated* format where the UUID and optionally device key + are passed directly on the command line, but the recommended way is to pass either an --app or + --device argument. The deprecated format will be remove in a future release. Examples: - $ resin os configure ../path/rpi.img 7cf02a6 - $ resin os configure ../path/rpi.img 7cf02a6 + $ resin os configure ../path/rpi.img --device 7cf02a6 + $ resin os configure ../path/rpi.img --device 7cf02a6 --deviceApiKey + $ resin os configure ../path/rpi.img --app MyApp ''' permission: 'user' options: [ commandOptions.advancedConfig + commandOptions.optionalApplication + commandOptions.optionalDevice + commandOptions.optionalDeviceApiKey { signature: 'config' description: 'path to the config JSON file, see `resin os build-config`' @@ -224,20 +233,47 @@ exports.configure = resin = require('resin-sdk-preconfigured') init = require('resin-device-init') helpers = require('../utils/helpers') - { generateDeviceConfig } = require('../utils/config') + patterns = require('../utils/patterns') + { generateDeviceConfig, generateApplicationConfig } = require('../utils/config') + + if _.filter([ + options.device + options.application + params.uuid + ]).length != 1 + patterns.expectedError ''' + To configure an image, you must provide exactly one of: + + * A device, with --device + * An application, with --app + * [Deprecated] A device, passing its uuid directly on the command line + + See the help page for examples: + + $ resin help os configure + ''' + + uuid = options.device || params.uuid + deviceApiKey = options.deviceApiKey || params.deviceApiKey console.info('Configuring operating system image') - resin.models.device.get(params.uuid) - .then (device) -> + + configurationResourceType = if uuid then 'device' else 'application' + + resin.models[configurationResourceType].get(uuid || options.application) + .then (appOrDevice) -> Promise.try -> if options.config return readFileAsync(options.config, 'utf8') .then(JSON.parse) - return buildConfig(params.image, device.device_type, options.advanced) + return buildConfig(params.image, appOrDevice.device_type, options.advanced) .then (answers) -> - generateDeviceConfig(device, params.deviceApiKey, answers) - .then (config) -> - init.configure(params.image, device.device_type, config, answers) + (if configurationResourceType == 'device' + generateDeviceConfig(appOrDevice, deviceApiKey, answers) + else + generateApplicationConfig(appOrDevice, answers) + ).then (config) -> + init.configure(params.image, appOrDevice.device_type, config, answers) .then(helpers.osProgressHandler) .nodeify(done)