Merge pull request #257 from resin-io/jviotti/feature/config-json-write

Implement config write command
This commit is contained in:
Juan Cruz Viotti 2015-11-11 08:29:10 -04:00
commit a71fb8ca4d
6 changed files with 183 additions and 31 deletions

View File

@ -1,10 +1,14 @@
(function() { (function() {
var Promise, imagefs, prettyjson, resin, rindle, umount, visuals; var Promise, _, getConfigPartitionInformationByType, imagefs, prettyjson, readConfigJSON, resin, rindle, stringToStream, umount, visuals, writeConfigJSON;
_ = require('lodash');
Promise = require('bluebird'); Promise = require('bluebird');
umount = Promise.promisifyAll(require('umount')); umount = Promise.promisifyAll(require('umount'));
stringToStream = require('string-to-stream');
resin = require('resin-sdk'); resin = require('resin-sdk');
imagefs = require('resin-image-fs'); imagefs = require('resin-image-fs');
@ -15,10 +19,41 @@
rindle = require('rindle'); rindle = require('rindle');
getConfigPartitionInformationByType = function(type) {
return resin.models.device.getManifestBySlug(type).then(function(manifest) {
var config, ref;
config = (ref = manifest.configuration) != null ? ref.config : void 0;
if (config == null) {
throw new Error("Unsupported device type: " + type);
}
return config;
});
};
readConfigJSON = function(drive, type) {
return getConfigPartitionInformationByType(type).then(function(configuration) {
return imagefs.read({
image: drive,
partition: configuration.partition,
path: configuration.path
});
}).then(rindle.extract).then(JSON.parse);
};
writeConfigJSON = function(drive, type, config) {
return getConfigPartitionInformationByType(type).then(function(configuration) {
return imagefs.write({
image: drive,
partition: configuration.partition,
path: configuration.path
}, stringToStream(config));
}).then(rindle.wait);
};
exports.read = { exports.read = {
signature: 'config read', signature: 'config read',
description: 'read a device configuration', description: 'read a device configuration',
help: 'Use this command to read the config.json file from a provisioned device\n\nExample:\n\n $ resin config read --type raspberry-pi\n $ resin config read --type raspberry-pi --drive /dev/disk2', help: 'Use this command to read the config.json file from a provisioned device\n\nExamples:\n\n $ resin config read --type raspberry-pi\n $ resin config read --type raspberry-pi --drive /dev/disk2',
options: [ options: [
{ {
signature: 'type', signature: 'type',
@ -39,20 +74,48 @@
return Promise["try"](function() { return Promise["try"](function() {
return options.drive || visuals.drive('Select the device drive'); return options.drive || visuals.drive('Select the device drive');
}).tap(umount.umountAsync).then(function(drive) { }).tap(umount.umountAsync).then(function(drive) {
return resin.models.device.getManifestBySlug(options.type).then(function(manifest) { return readConfigJSON(drive, options.type);
var config, ref; }).tap(function(config) {
config = (ref = manifest.configuration) != null ? ref.config : void 0; return console.info(prettyjson.render(config));
if (config == null) { }).nodeify(done);
throw new Error("Unsupported device type: " + options.type); }
} };
return imagefs.read({
image: drive, exports.write = {
partition: config.partition, signature: 'config write <key> <value>',
path: config.path description: 'write a device configuration',
}); help: 'Use this command to write the config.json file of a provisioned device\n\nExamples:\n\n $ resin config write --type raspberry-pi username johndoe\n $ resin config write --type raspberry-pi --drive /dev/disk2 username johndoe\n $ resin config write --type raspberry-pi files.network/settings "..."',
options: [
{
signature: 'type',
description: 'device type',
parameter: 'type',
alias: 't',
required: 'You have to specify a device type'
}, {
signature: 'drive',
description: 'drive',
parameter: 'drive',
alias: 'd'
}
],
permission: 'user',
root: true,
action: function(params, options, done) {
return Promise["try"](function() {
return options.drive || visuals.drive('Select the device drive');
}).tap(umount.umountAsync).then(function(drive) {
return readConfigJSON(drive, options.type).then(function(config) {
console.info("Setting " + params.key + " to " + params.value);
_.set(config, params.key, params.value);
return JSON.stringify(config);
}).tap(function() {
return umount.umountAsync(drive);
}).then(function(config) {
return writeConfigJSON(drive, options.type, config);
}); });
}).then(rindle.extract).then(JSON.parse).then(function(config) { }).tap(function() {
return console.log(prettyjson.render(config)); return console.info('Done');
}).nodeify(done); }).nodeify(done);
} }
}; };

View File

@ -98,6 +98,8 @@
capitano.command(actions.config.read); capitano.command(actions.config.read);
capitano.command(actions.config.write);
capitano.command(actions.logs); capitano.command(actions.logs);
update.notify(); update.notify();

View File

@ -79,6 +79,7 @@ Now you have access to all the commands referenced below.
- Config - Config
- [config read](#config-read) - [config read](#config-read)
- [config write &#60;key&#62; &#60;value&#62;](#config-write-60-key-62-60-value-62-)
- Wizard - Wizard
@ -604,7 +605,7 @@ drive
Use this command to read the config.json file from a provisioned device Use this command to read the config.json file from a provisioned device
Example: Examples:
$ resin config read --type raspberry-pi $ resin config read --type raspberry-pi
$ resin config read --type raspberry-pi --drive /dev/disk2 $ resin config read --type raspberry-pi --drive /dev/disk2
@ -619,6 +620,26 @@ device type
drive drive
## config write &#60;key&#62; &#60;value&#62;
Use this command to write the config.json file of a provisioned device
Examples:
$ resin config write --type raspberry-pi username johndoe
$ resin config write --type raspberry-pi --drive /dev/disk2 username johndoe
$ resin config write --type raspberry-pi files.network/settings "..."
### Options
#### --type, -t &#60;type&#62;
device type
#### --drive, -d &#60;drive&#62;
drive
# Wizard # Wizard
## quickstart [name] ## quickstart [name]

View File

@ -1,18 +1,47 @@
_ = require('lodash')
Promise = require('bluebird') Promise = require('bluebird')
umount = Promise.promisifyAll(require('umount')) umount = Promise.promisifyAll(require('umount'))
stringToStream = require('string-to-stream')
resin = require('resin-sdk') resin = require('resin-sdk')
imagefs = require('resin-image-fs') imagefs = require('resin-image-fs')
visuals = require('resin-cli-visuals') visuals = require('resin-cli-visuals')
prettyjson = require('prettyjson') prettyjson = require('prettyjson')
rindle = require('rindle') rindle = require('rindle')
getConfigPartitionInformationByType = (type) ->
return resin.models.device.getManifestBySlug(type).then (manifest) ->
config = manifest.configuration?.config
if not config?
throw new Error("Unsupported device type: #{type}")
return config
readConfigJSON = (drive, type) ->
return getConfigPartitionInformationByType(type).then (configuration) ->
return imagefs.read
image: drive
partition: configuration.partition
path: configuration.path
.then(rindle.extract)
.then(JSON.parse)
writeConfigJSON = (drive, type, config) ->
return getConfigPartitionInformationByType(type).then (configuration) ->
return imagefs.write
image: drive
partition: configuration.partition
path: configuration.path
, stringToStream(config)
.then(rindle.wait)
exports.read = exports.read =
signature: 'config read' signature: 'config read'
description: 'read a device configuration' description: 'read a device configuration'
help: ''' help: '''
Use this command to read the config.json file from a provisioned device Use this command to read the config.json file from a provisioned device
Example: Examples:
$ resin config read --type raspberry-pi $ resin config read --type raspberry-pi
$ resin config read --type raspberry-pi --drive /dev/disk2 $ resin config read --type raspberry-pi --drive /dev/disk2
@ -39,18 +68,53 @@ exports.read =
return options.drive or visuals.drive('Select the device drive') return options.drive or visuals.drive('Select the device drive')
.tap(umount.umountAsync) .tap(umount.umountAsync)
.then (drive) -> .then (drive) ->
resin.models.device.getManifestBySlug(options.type).then (manifest) -> return readConfigJSON(drive, options.type)
config = manifest.configuration?.config .tap (config) ->
console.info(prettyjson.render(config))
if not config? .nodeify(done)
throw new Error("Unsupported device type: #{options.type}")
exports.write =
imagefs.read signature: 'config write <key> <value>'
image: drive description: 'write a device configuration'
partition: config.partition help: '''
path: config.path Use this command to write the config.json file of a provisioned device
.then(rindle.extract)
.then(JSON.parse) Examples:
.then (config) ->
console.log(prettyjson.render(config)) $ resin config write --type raspberry-pi username johndoe
$ resin config write --type raspberry-pi --drive /dev/disk2 username johndoe
$ resin config write --type raspberry-pi files.network/settings "..."
'''
options: [
{
signature: 'type'
description: 'device type'
parameter: 'type'
alias: 't'
required: 'You have to specify a device type'
}
{
signature: 'drive'
description: 'drive'
parameter: 'drive'
alias: 'd'
}
]
permission: 'user'
root: true
action: (params, options, done) ->
Promise.try ->
return options.drive or visuals.drive('Select the device drive')
.tap(umount.umountAsync)
.then (drive) ->
readConfigJSON(drive, options.type).then (config) ->
console.info("Setting #{params.key} to #{params.value}")
_.set(config, params.key, params.value)
return JSON.stringify(config)
.tap ->
return umount.umountAsync(drive)
.then (config) ->
return writeConfigJSON(drive, options.type, config)
.tap ->
console.info('Done')
.nodeify(done) .nodeify(done)

View File

@ -71,6 +71,7 @@ capitano.command(actions.os.initialize)
# ---------- Config Module ---------- # ---------- Config Module ----------
capitano.command(actions.config.read) capitano.command(actions.config.read)
capitano.command(actions.config.write)
# ---------- Logs Module ---------- # ---------- Logs Module ----------
capitano.command(actions.logs) capitano.command(actions.logs)

View File

@ -63,6 +63,7 @@
"resin-vcs": "^2.0.0", "resin-vcs": "^2.0.0",
"rimraf": "^2.4.3", "rimraf": "^2.4.3",
"rindle": "^1.0.0", "rindle": "^1.0.0",
"string-to-stream": "^1.0.1",
"tmp": "0.0.28", "tmp": "0.0.28",
"umount": "^1.1.1", "umount": "^1.1.1",
"underscore.string": "^3.1.1", "underscore.string": "^3.1.1",