mirror of
https://github.com/balena-io/balena-cli.git
synced 2024-12-18 21:27:51 +00:00
Implement logs command
This commit is contained in:
parent
1f4926bb6a
commit
721ef8413a
@ -1,7 +1,7 @@
|
||||
resin = require('../resin')
|
||||
|
||||
exports.list = (applicationId) ->
|
||||
resin.models.device.getAll applicationId, (error, devices) ->
|
||||
resin.models.device.getAllByApplication applicationId, (error, devices) ->
|
||||
resin.errors.handle(error) if error?
|
||||
|
||||
resin.log.out resin.ui.widgets.table.horizontal devices, (device) ->
|
||||
|
@ -4,3 +4,5 @@ module.exports =
|
||||
device: require('./device')
|
||||
env: require('./environment-variables')
|
||||
keys: require('./keys')
|
||||
logs: require('./logs')
|
||||
preferences: require('./preferences')
|
||||
|
33
lib/actions/logs.coffee
Normal file
33
lib/actions/logs.coffee
Normal file
@ -0,0 +1,33 @@
|
||||
_ = require('lodash')
|
||||
PubNub = require('pubnub')
|
||||
resin = require('../resin')
|
||||
|
||||
LOGS_HISTORY_COUNT = 200
|
||||
|
||||
exports.logs = (uuid) ->
|
||||
resin.models.device.getAll (error, devices) ->
|
||||
resin.errors.handle(error) if error?
|
||||
|
||||
uuidExists = _.findWhere(devices, { uuid })?
|
||||
if not uuidExists
|
||||
return resin.errors.handle(new Error('Invalid UUID'))
|
||||
|
||||
# PubNub needs to be initialised after logs
|
||||
# action was called, otherwise it prevents
|
||||
# all other actions from exiting on completion
|
||||
pubnub = PubNub.init(resin.config.pubnub)
|
||||
|
||||
channel = "device-#{uuid}-logs"
|
||||
|
||||
printLogs = (logs) ->
|
||||
logs = logs[0] if _.isArray(logs)
|
||||
resin.log.array(logs, resin.log.out)
|
||||
|
||||
pubnub.history
|
||||
count: LOGS_HISTORY_COUNT
|
||||
channel: channel
|
||||
callback: printLogs
|
||||
|
||||
pubnub.subscribe
|
||||
channel: channel
|
||||
callback: printLogs
|
@ -94,6 +94,13 @@ resin.cli.addResource
|
||||
actions: actions.env
|
||||
permission: 'user'
|
||||
|
||||
# ---------- Logs Module ----------
|
||||
resin.cli.addCommand
|
||||
command: 'logs <uuid>'
|
||||
description: 'show device logs'
|
||||
action: actions.logs.logs
|
||||
permission: 'user'
|
||||
|
||||
resin.data.prefix.set resin.config.dataPrefix, (error) ->
|
||||
resin.errors.handle(error) if error?
|
||||
|
||||
|
@ -13,6 +13,11 @@ config =
|
||||
dataPrefix: path.join(process.env.HOME, '.resin')
|
||||
sshKeyWidth: 43
|
||||
|
||||
pubnub:
|
||||
subscribe_key: 'sub-c-bbc12eba-ce4a-11e3-9782-02ee2ddab7fe'
|
||||
publish_key: 'pub-c-6cbce8db-bfd1-4fdf-a8c8-53671ae2b226'
|
||||
ssl: true
|
||||
|
||||
config.pluginsDirectory = path.join(config.dataPrefix, 'plugins')
|
||||
|
||||
config.urls =
|
||||
|
@ -1,3 +1,5 @@
|
||||
_ = require('lodash')
|
||||
|
||||
isQuiet = false
|
||||
|
||||
exports.setQuiet = (quiet) ->
|
||||
@ -20,3 +22,15 @@ exports.info = (args...) ->
|
||||
|
||||
exports.out = (args...) ->
|
||||
console.log.apply(null, args)
|
||||
|
||||
exports.array = (array, logFunction) ->
|
||||
return if not array?
|
||||
|
||||
if not _.isFunction(logFunction)
|
||||
throw new Error('Invalid log function')
|
||||
|
||||
if not _.isArray(array)
|
||||
return logFunction(array)
|
||||
|
||||
for item in array
|
||||
logFunction(item)
|
||||
|
@ -55,6 +55,54 @@ describe 'Log:', ->
|
||||
it 'should output to console.log', ->
|
||||
testConsoleFunctionBeingCalled('out', 'log', MESSAGE.empty)
|
||||
|
||||
describe '#array()', ->
|
||||
|
||||
array = [ 1, 2, 3, 4 ]
|
||||
|
||||
it 'should call log function for every line', ->
|
||||
spy = sinon.spy()
|
||||
log.array(array, spy)
|
||||
expect(spy.callCount).to.equal(array.length)
|
||||
|
||||
for item in array
|
||||
expect(spy).to.have.been.calledWith(item)
|
||||
|
||||
it 'should throw an error if log function is missing', ->
|
||||
func = _.partial(log.array, array)
|
||||
expect(func).to.throw(Error)
|
||||
|
||||
it 'should throw an error if log function is not a function', ->
|
||||
for input in [
|
||||
undefined
|
||||
null
|
||||
123
|
||||
'Hello World'
|
||||
[ 1, 2, 3 ]
|
||||
{ hello: 'world' }
|
||||
]
|
||||
func = _.partial(log.array, 'Hello', input)
|
||||
expect(func).to.throw(Error)
|
||||
|
||||
it 'should call log function once if input is not an array', ->
|
||||
for input in [
|
||||
'Hello World'
|
||||
{ hello: 'world' }
|
||||
1234
|
||||
]
|
||||
spy = sinon.spy()
|
||||
log.array(input, spy)
|
||||
expect(spy).to.have.been.calledOnce
|
||||
expect(spy).to.have.been.calledWith(input)
|
||||
|
||||
it 'should not call log function if input is undefined/null', ->
|
||||
for input in [
|
||||
undefined
|
||||
null
|
||||
]
|
||||
spy = sinon.spy()
|
||||
log.array(input, spy)
|
||||
expect(spy).to.not.have.been.called
|
||||
|
||||
describe '#setQuiet()', ->
|
||||
|
||||
it 'should set the quietness', ->
|
||||
|
@ -4,7 +4,22 @@ errors = require('../errors/errors')
|
||||
server = require('../server/server')
|
||||
config = require('../config')
|
||||
|
||||
exports.getAll = (applicationId, callback) ->
|
||||
exports.getAll = (callback) ->
|
||||
return canvas.get
|
||||
resource: 'device'
|
||||
options:
|
||||
expand: 'application'
|
||||
orderby: 'name asc'
|
||||
.then (devices) ->
|
||||
if _.isEmpty(devices)
|
||||
return callback(new errors.NotAny('devices'))
|
||||
|
||||
return callback(null, devices)
|
||||
|
||||
.catch (error) ->
|
||||
return callback(error)
|
||||
|
||||
exports.getAllByApplication = (applicationId, callback) ->
|
||||
return canvas.get
|
||||
resource: 'device'
|
||||
options:
|
||||
|
@ -50,6 +50,7 @@
|
||||
"typed-error": "~0.1.0",
|
||||
"is-online": "~3.0.0",
|
||||
"pluralize": "~1.1.0",
|
||||
"indefinite-article": "0.0.2"
|
||||
"indefinite-article": "0.0.2",
|
||||
"pubnub": "~3.7.0"
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user