From 594a358bef183b60301a7f2af9d407cf0837536e Mon Sep 17 00:00:00 2001 From: Juan Cruz Viotti Date: Thu, 27 Nov 2014 11:22:48 -0400 Subject: [PATCH] Implement and make use of plugin loader (only in preferences for now) --- lib/actions/app.coffee | 9 ++++--- lib/actions/device.coffee | 5 ++-- lib/actions/environment-variables.coffee | 11 ++++---- lib/actions/index.coffee | 1 - lib/actions/keys.coffee | 5 ++-- lib/actions/preferences.coffee | 11 +++++--- lib/app.coffee | 7 ++---- lib/plugin-loader/plugin-loader.coffee | 8 ++++++ lib/plugin-loader/plugin-loader.spec.coffee | 28 +++++++++++++++++++++ 9 files changed, 63 insertions(+), 22 deletions(-) create mode 100644 lib/plugin-loader/plugin-loader.coffee create mode 100644 lib/plugin-loader/plugin-loader.spec.coffee diff --git a/lib/actions/app.coffee b/lib/actions/app.coffee index 12f860c0..ce23e242 100644 --- a/lib/actions/app.coffee +++ b/lib/actions/app.coffee @@ -2,11 +2,11 @@ _ = require('lodash') async = require('async') resin = require('../resin') -exports.create = (name, program) -> +exports.create = (name) -> async.waterfall [ (callback) -> - deviceType = program.parent.type + deviceType = resin.cli.getArgument('type') if deviceType? return callback(null, deviceType) @@ -52,7 +52,8 @@ exports.restart = (id) -> resin.models.application.restart id, (error) -> resin.errors.handle(error) if error? -exports.remove = (id, program) -> - resin.ui.patterns.remove 'application', program.parent.yes, (callback) -> +exports.remove = (id) -> + confirmArgument = resin.cli.getArgument('yes') + resin.ui.patterns.remove 'application', confirmArgument, (callback) -> resin.models.application.remove(id, callback) , resin.errors.handle diff --git a/lib/actions/device.coffee b/lib/actions/device.coffee index 8e3bb7a8..e2cd47ca 100644 --- a/lib/actions/device.coffee +++ b/lib/actions/device.coffee @@ -14,8 +14,9 @@ exports.list = (applicationId) -> return device , [ 'ID', 'Name', 'Device Type', 'Is Online', 'IP Address', 'Application', 'Status', 'Last Seen' ] -exports.remove = (id, program) -> - resin.ui.patterns.remove 'device', program.parent.yes, (callback) -> +exports.remove = (id) -> + confirmArgument = resin.cli.getArgument('yes') + resin.ui.patterns.remove 'device', confirmArgument, (callback) -> resin.models.device.remove(id, callback) , resin.errors.handle diff --git a/lib/actions/environment-variables.coffee b/lib/actions/environment-variables.coffee index f83a4c92..0fdb260e 100644 --- a/lib/actions/environment-variables.coffee +++ b/lib/actions/environment-variables.coffee @@ -6,8 +6,8 @@ SYSTEM_VAR_REGEX = /^RESIN_/ isSystemVariable = (environmentVariable) -> SYSTEM_VAR_REGEX.test(environmentVariable.name) -exports.list = (program) -> - applicationId = program.parent?.application +exports.list = -> + applicationId = resin.cli.getArgument('application') if not applicationId? resin.errors.handle(new Error('You have to specify an application')) @@ -15,12 +15,13 @@ exports.list = (program) -> resin.models.environmentVariables.getAll applicationId, (error, environmentVariables) -> resin.errors.handle(error) if error? - if not program.parent.verbose? + if not resin.cli.getArgument('verbose')? environmentVariables = _.reject(environmentVariables, isSystemVariable) resin.log.out(resin.ui.widgets.table.horizontal(environmentVariables)) -exports.remove = (id, program) -> - resin.ui.patterns.remove 'environment variable', program.parent.yes, (callback) -> +exports.remove = (id) -> + confirmArgument = resin.cli.getArgument('yes') + resin.ui.patterns.remove 'environment variable', confirmArgument, (callback) -> resin.models.environmentVariables.remove(id, callback) , resin.errors.handle diff --git a/lib/actions/index.coffee b/lib/actions/index.coffee index a55de2f0..bde1da26 100644 --- a/lib/actions/index.coffee +++ b/lib/actions/index.coffee @@ -4,4 +4,3 @@ module.exports = device: require('./device') env: require('./environment-variables') keys: require('./keys') - preferences: require('./preferences') diff --git a/lib/actions/keys.coffee b/lib/actions/keys.coffee index 729772c8..8b5726e6 100644 --- a/lib/actions/keys.coffee +++ b/lib/actions/keys.coffee @@ -25,7 +25,8 @@ exports.info = (id) -> key.public_key = '\n' + helpers.formatLongString(key.public_key, resin.config.sshKeyWidth) resin.log.out(resin.ui.widgets.table.vertical(key, _.identity, [ 'ID', 'Title', 'Public Key' ])) -exports.remove = (id, program) -> - resin.ui.patterns.remove 'key', program.parent.yes, (callback) -> +exports.remove = (id) -> + confirmArgument = resin.cli.getArgument('yes') + resin.ui.patterns.remove 'key', confirmArgument, (callback) -> resin.server.delete("/user/keys/#{id}", callback) , resin.errors.handle diff --git a/lib/actions/preferences.coffee b/lib/actions/preferences.coffee index c1a700ae..ba31e2ec 100644 --- a/lib/actions/preferences.coffee +++ b/lib/actions/preferences.coffee @@ -1,5 +1,10 @@ open = require('open') -resin = require('../resin') -exports.preferences = -> - open(resin.config.urls.preferences) +module.exports = (resin) -> + + resin.cli.addCommand + command: 'preferences' + description: 'open preferences form' + permission: 'user' + action: -> + open(resin.config.urls.preferences) diff --git a/lib/app.coffee b/lib/app.coffee index f71d26da..8e7ce04e 100644 --- a/lib/app.coffee +++ b/lib/app.coffee @@ -2,6 +2,7 @@ _ = require('lodash') resin = require('./resin') packageJSON = require('../package.json') actions = require('./actions') +pluginLoader = require('./plugin-loader/plugin-loader') resin.cli.setVersion(packageJSON.version) @@ -73,11 +74,7 @@ resin.cli.addCommand permission: 'user' # ---------- Preferences Module ---------- -resin.cli.addCommand - command: 'preferences' - description: 'open preferences form' - action: actions.preferences.preferences - permission: 'user' +pluginLoader.use(require('./actions/preferences')) # ---------- Keys Module ---------- resin.cli.addResource diff --git a/lib/plugin-loader/plugin-loader.coffee b/lib/plugin-loader/plugin-loader.coffee new file mode 100644 index 00000000..74ba6825 --- /dev/null +++ b/lib/plugin-loader/plugin-loader.coffee @@ -0,0 +1,8 @@ +_ = require('lodash') +resin = require('../resin') + +exports.use = (plugin) -> + if not _.isFunction(plugin) + throw new Error('Plugin should be a function') + + plugin.call(null, resin) diff --git a/lib/plugin-loader/plugin-loader.spec.coffee b/lib/plugin-loader/plugin-loader.spec.coffee new file mode 100644 index 00000000..2da1a64b --- /dev/null +++ b/lib/plugin-loader/plugin-loader.spec.coffee @@ -0,0 +1,28 @@ +_ = require('lodash') +chai = require('chai') +chai.use(require('sinon-chai')) +expect = chai.expect +sinon = require('sinon') +resin = require('../resin') +pluginLoader = require('../plugin-loader/plugin-loader') + +describe 'Plugin Loader:', -> + + describe '#use()', -> + + it 'should pass the resin object to the function', -> + spy = sinon.spy() + pluginLoader.use(spy) + expect(spy).to.have.been.calledWith(resin) + + it 'should throw an error if plugin is not a function', -> + for nonFunction in [ + undefined + null + [ 1, 2, 3 ] + 123 + 'Hello World' + { hello: 'world' } + ] + func = _.partial(pluginLoader.use, nonFunction) + expect(func).to.throw(Error)