Move logs logic to a nested logs module

This commit is contained in:
Juan Cruz Viotti 2015-01-09 09:42:46 -03:00
parent 5ec4abc710
commit bf7430d7fd
3 changed files with 13 additions and 43 deletions

View File

@ -1,52 +1,17 @@
_ = require('lodash')
PubNub = require('pubnub')
resin = require('resin-sdk')
helpers = require('../helpers/helpers')
permissions = require('../permissions/permissions')
log = require('../log/log')
errors = require('../errors/errors')
logs = require('../logs/logs')
LOGS_HISTORY_COUNT = 200
getLogData = (logs) ->
return logs[0] if _.isArray(logs)
return logs
printLogs = (logs, number) ->
logs = getLogData(logs)
logs = _.last(logs, number) if _.isNumber(number)
log.array(logs, log.out)
exports.logs = permissions.user (params, options) ->
numberOfLines = options.num
tailOutput = options.tail or false
if numberOfLines? and not _.isNumber(numberOfLines)
errors.handle(new Error('n/num should be a number'))
helpers.isDeviceUUIDValid params.uuid, (error, isValidUUID) ->
logs.subscribe params.uuid, {
history: options.num or LOGS_HISTORY_COUNT
tail: options.tail
}, (error, message) ->
errors.handle(error) if error?
if not isValidUUID
return 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.settings.get('pubnub'))
channel = _.template(resin.settings.get('events.deviceLogs'), uuid: params.uuid)
pubnub.history
count: LOGS_HISTORY_COUNT
channel: channel
callback: (logs) ->
printLogs(logs, numberOfLines)
if not tailOutput or numberOfLines?
process.exit(0)
if tailOutput
pubnub.subscribe
channel: channel
callback: printLogs
log.array(message, log.out)

View File

@ -478,6 +478,11 @@ capitano.command
To continuously stream output, and see new logs in real time, use the `--tail` option.
Note that for now you need to provide the whole UUID for this command to work correctly,
and the tool won't notice if you're using an invalid UUID.
This is due to some technical limitations that we plan to address soon.
Examples:
$ resin logs 23c73a12e3527df55c60b9ce647640c1b7da1b32d71e6a39849ac0f00db828
$ resin logs 23c73a12e3527df55c60b9ce647640c1b7da1b32d71e6a39849ac0f00db828 --num 20

View File

@ -47,7 +47,7 @@
},
"dependencies": {
"async": "~0.9.0",
"capitano": "~1.1.0",
"capitano": "~1.1.1",
"cliff": "~0.1.9",
"coffee-script": "~1.8.0",
"conf.js": "^0.1.1",