Make use of resin-config-json for config commands

This module encapsulates the low level details of `config.json` I/O and
tests them extensively.

See: https://github.com/resin-io/resin-config-json
This commit is contained in:
Juan Cruz Viotti 2015-11-11 10:04:46 -04:00
parent 79f2b4f0d5
commit 4fc7a4e436
3 changed files with 21 additions and 89 deletions

View File

@ -1,5 +1,5 @@
(function() {
var Promise, _, getConfigPartitionInformationByType, imagefs, prettyjson, readConfigJSON, resin, rindle, stringToStream, umount, visuals, writeConfigJSON;
var Promise, _, config, prettyjson, umount, visuals;
_ = require('lodash');
@ -7,49 +7,12 @@
umount = Promise.promisifyAll(require('umount'));
stringToStream = require('string-to-stream');
resin = require('resin-sdk');
imagefs = require('resin-image-fs');
visuals = require('resin-cli-visuals');
config = require('resin-config-json');
prettyjson = require('prettyjson');
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',
@ -74,9 +37,9 @@
return Promise["try"](function() {
return options.drive || visuals.drive('Select the device drive');
}).tap(umount.umountAsync).then(function(drive) {
return readConfigJSON(drive, options.type);
}).tap(function(config) {
return console.info(prettyjson.render(config));
return config.read(drive, options.type);
}).tap(function(configJSON) {
return console.info(prettyjson.render(configJSON));
}).nodeify(done);
}
};
@ -105,14 +68,14 @@
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) {
return config.read(drive, options.type).then(function(configJSON) {
console.info("Setting " + params.key + " to " + params.value);
_.set(config, params.key, params.value);
return JSON.stringify(config);
_.set(configJSON, params.key, params.value);
return configJSON;
}).tap(function() {
return umount.umountAsync(drive);
}).then(function(config) {
return writeConfigJSON(drive, options.type, config);
}).then(function(configJSON) {
return config.write(drive, options.type, configJSON);
});
}).tap(function() {
return console.info('Done');

View File

@ -1,39 +1,9 @@
_ = 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')
config = require('resin-config-json')
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'
@ -68,9 +38,9 @@ exports.read =
return options.drive or visuals.drive('Select the device drive')
.tap(umount.umountAsync)
.then (drive) ->
return readConfigJSON(drive, options.type)
.tap (config) ->
console.info(prettyjson.render(config))
return config.read(drive, options.type)
.tap (configJSON) ->
console.info(prettyjson.render(configJSON))
.nodeify(done)
exports.write =
@ -107,14 +77,14 @@ exports.write =
return options.drive or visuals.drive('Select the device drive')
.tap(umount.umountAsync)
.then (drive) ->
readConfigJSON(drive, options.type).then (config) ->
config.read(drive, options.type).then (configJSON) ->
console.info("Setting #{params.key} to #{params.value}")
_.set(config, params.key, params.value)
return JSON.stringify(config)
_.set(configJSON, params.key, params.value)
return configJSON
.tap ->
return umount.umountAsync(drive)
.then (config) ->
return writeConfigJSON(drive, options.type, config)
.then (configJSON) ->
return config.write(drive, options.type, configJSON)
.tap ->
console.info('Done')
.nodeify(done)

View File

@ -53,9 +53,9 @@
"resin-cli-form": "^1.3.0",
"resin-cli-visuals": "^1.2.2",
"resin-config-inject": "^2.0.0",
"resin-config-json": "^1.0.0",
"resin-device-init": "^2.0.0",
"resin-image": "^1.1.4",
"resin-image-fs": "^2.1.1",
"resin-image-manager": "^3.2.2",
"resin-pine": "^1.3.0",
"resin-sdk": "^4.0.0",
@ -63,7 +63,6 @@
"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",