From 5509a3e9fdd53a6a46ac67f04d08beb101d49d31 Mon Sep 17 00:00:00 2001 From: Juan Cruz Viotti Date: Tue, 10 Nov 2015 12:53:34 -0400 Subject: [PATCH] Implement config read command This command is used to read a config.json from a provisioned device --- build/actions/config.js | 60 +++++++++++++++++++++++++++++++++++++++ build/actions/index.js | 3 +- build/app.js | 2 ++ capitanodoc.json | 6 ++++ doc/cli.markdown | 25 ++++++++++++++++ lib/actions/config.coffee | 56 ++++++++++++++++++++++++++++++++++++ lib/actions/index.coffee | 1 + lib/app.coffee | 3 ++ package.json | 2 ++ 9 files changed, 157 insertions(+), 1 deletion(-) create mode 100644 build/actions/config.js create mode 100644 lib/actions/config.coffee diff --git a/build/actions/config.js b/build/actions/config.js new file mode 100644 index 00000000..d9c84614 --- /dev/null +++ b/build/actions/config.js @@ -0,0 +1,60 @@ +(function() { + var Promise, imagefs, prettyjson, resin, rindle, umount, visuals; + + Promise = require('bluebird'); + + umount = Promise.promisifyAll(require('umount')); + + resin = require('resin-sdk'); + + imagefs = require('resin-image-fs'); + + visuals = require('resin-cli-visuals'); + + prettyjson = require('prettyjson'); + + rindle = require('rindle'); + + 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', + 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 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 + }); + }); + }).then(rindle.extract).then(JSON.parse).then(function(config) { + return console.log(prettyjson.render(config)); + }).nodeify(done); + } + }; + +}).call(this); diff --git a/build/actions/index.js b/build/actions/index.js index 2f51948d..abedba94 100644 --- a/build/actions/index.js +++ b/build/actions/index.js @@ -10,7 +10,8 @@ logs: require('./logs'), notes: require('./notes'), help: require('./help'), - os: require('./os') + os: require('./os'), + config: require('./config') }; }).call(this); diff --git a/build/app.js b/build/app.js index 719e291f..891617b4 100644 --- a/build/app.js +++ b/build/app.js @@ -96,6 +96,8 @@ capitano.command(actions.os.initialize); + capitano.command(actions.config.read); + capitano.command(actions.logs); update.notify(); diff --git a/capitanodoc.json b/capitanodoc.json index 660c8477..5a7c758c 100644 --- a/capitanodoc.json +++ b/capitanodoc.json @@ -62,6 +62,12 @@ "lib/actions/os.coffee" ] }, + { + "title": "Config", + "files": [ + "lib/actions/config.coffee" + ] + }, { "title": "Wizard", "files": [ diff --git a/doc/cli.markdown b/doc/cli.markdown index f92c9233..a2470fed 100644 --- a/doc/cli.markdown +++ b/doc/cli.markdown @@ -76,6 +76,10 @@ Now you have access to all the commands referenced below. - [os configure <image> <uuid>](#os-configure-60-image-62-60-uuid-62-) - [os initialize <image>](#os-initialize-60-image-62-) +- Config + + - [config read](#config-read) + - Wizard - [quickstart [name]](#quickstart-name-) @@ -594,6 +598,27 @@ device type drive +# Config + +## config read + +Use this command to read the config.json file from a provisioned device + +Example: + + $ resin config read --type raspberry-pi + $ resin config read --type raspberry-pi --drive /dev/disk2 + +### Options + +#### --type, -t <type> + +device type + +#### --drive, -d <drive> + +drive + # Wizard ## quickstart [name] diff --git a/lib/actions/config.coffee b/lib/actions/config.coffee new file mode 100644 index 00000000..7c9aea43 --- /dev/null +++ b/lib/actions/config.coffee @@ -0,0 +1,56 @@ +Promise = require('bluebird') +umount = Promise.promisifyAll(require('umount')) +resin = require('resin-sdk') +imagefs = require('resin-image-fs') +visuals = require('resin-cli-visuals') +prettyjson = require('prettyjson') +rindle = require('rindle') + +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: + + $ resin config read --type raspberry-pi + $ resin config read --type raspberry-pi --drive /dev/disk2 + ''' + 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) -> + 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)) + .nodeify(done) diff --git a/lib/actions/index.coffee b/lib/actions/index.coffee index 6832389a..ad8cbde2 100644 --- a/lib/actions/index.coffee +++ b/lib/actions/index.coffee @@ -10,3 +10,4 @@ module.exports = notes: require('./notes') help: require('./help') os: require('./os') + config: require('./config') diff --git a/lib/app.coffee b/lib/app.coffee index 35969b48..e3666619 100644 --- a/lib/app.coffee +++ b/lib/app.coffee @@ -69,6 +69,9 @@ capitano.command(actions.os.download) capitano.command(actions.os.configure) capitano.command(actions.os.initialize) +# ---------- Config Module ---------- +capitano.command(actions.config.read) + # ---------- Logs Module ---------- capitano.command(actions.logs) diff --git a/package.json b/package.json index 094ec805..6f2d362c 100644 --- a/package.json +++ b/package.json @@ -47,6 +47,7 @@ "lodash": "^3.10.0", "nplugm": "^3.0.0", "president": "^2.0.0", + "prettyjson": "^1.1.3", "resin-cli-errors": "^1.0.0", "resin-cli-events": "^1.0.2", "resin-cli-form": "^1.3.0", @@ -54,6 +55,7 @@ "resin-config-inject": "^2.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",