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() {
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');
umount = Promise.promisifyAll(require('umount'));
stringToStream = require('string-to-stream');
resin = require('resin-sdk');
imagefs = require('resin-image-fs');
@ -15,10 +19,41 @@
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 = {
signature: 'config read',
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: [
{
signature: 'type',
@ -39,20 +74,48 @@
return Promise["try"](function() {
return options.drive || visuals.drive('Select the device drive');
}).tap(umount.umountAsync).then(function(drive) {
return resin.models.device.getManifestBySlug(options.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: " + options.type);
}
return imagefs.read({
image: drive,
partition: config.partition,
path: config.path
});
return readConfigJSON(drive, options.type);
}).tap(function(config) {
return console.info(prettyjson.render(config));
}).nodeify(done);
}
};
exports.write = {
signature: 'config write <key> <value>',
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) {
return console.log(prettyjson.render(config));
}).tap(function() {
return console.info('Done');
}).nodeify(done);
}
};

View File

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

View File

@ -79,6 +79,7 @@ Now you have access to all the commands referenced below.
- Config
- [config read](#config-read)
- [config write &#60;key&#62; &#60;value&#62;](#config-write-60-key-62-60-value-62-)
- Wizard
@ -604,7 +605,7 @@ drive
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 --drive /dev/disk2
@ -619,6 +620,26 @@ device type
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
## quickstart [name]

View File

@ -1,18 +1,47 @@
_ = require('lodash')
Promise = require('bluebird')
umount = Promise.promisifyAll(require('umount'))
stringToStream = require('string-to-stream')
resin = require('resin-sdk')
imagefs = require('resin-image-fs')
visuals = require('resin-cli-visuals')
prettyjson = require('prettyjson')
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 =
signature: 'config read'
description: 'read a device configuration'
help: '''
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 --drive /dev/disk2
@ -39,18 +68,53 @@ exports.read =
return options.drive or visuals.drive('Select the device drive')
.tap(umount.umountAsync)
.then (drive) ->
resin.models.device.getManifestBySlug(options.type).then (manifest) ->
config = manifest.configuration?.config
if not config?
throw new Error("Unsupported device type: #{options.type}")
imagefs.read
image: drive
partition: config.partition
path: config.path
.then(rindle.extract)
.then(JSON.parse)
.then (config) ->
console.log(prettyjson.render(config))
return readConfigJSON(drive, options.type)
.tap (config) ->
console.info(prettyjson.render(config))
.nodeify(done)
exports.write =
signature: 'config write <key> <value>'
description: 'write a device configuration'
help: '''
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: [
{
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)

View File

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

View File

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