mirror of
https://github.com/balena-io/balena-cli.git
synced 2025-04-13 22:22:58 +00:00
Implement and make use of plugin loader (only in preferences for now)
This commit is contained in:
parent
a3c8a2fb27
commit
594a358bef
@ -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
|
||||
|
@ -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
|
||||
|
||||
|
@ -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
|
||||
|
@ -4,4 +4,3 @@ module.exports =
|
||||
device: require('./device')
|
||||
env: require('./environment-variables')
|
||||
keys: require('./keys')
|
||||
preferences: require('./preferences')
|
||||
|
@ -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
|
||||
|
@ -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)
|
||||
|
@ -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
|
||||
|
8
lib/plugin-loader/plugin-loader.coffee
Normal file
8
lib/plugin-loader/plugin-loader.coffee
Normal file
@ -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)
|
28
lib/plugin-loader/plugin-loader.spec.coffee
Normal file
28
lib/plugin-loader/plugin-loader.spec.coffee
Normal file
@ -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)
|
Loading…
x
Reference in New Issue
Block a user