Get rid of log module

Didn't provide that much value
This commit is contained in:
Juan Cruz Viotti 2015-01-15 11:36:43 -03:00
parent 9a3b048b9f
commit a6282d2b28
15 changed files with 39 additions and 224 deletions

View File

@ -2,7 +2,6 @@ _ = require('lodash-contrib')
async = require('async') async = require('async')
resin = require('resin-sdk') resin = require('resin-sdk')
ui = require('../ui') ui = require('../ui')
log = require('../log/log')
permissions = require('../permissions/permissions') permissions = require('../permissions/permissions')
exports.create = permissions.user (params, options, done) -> exports.create = permissions.user (params, options, done) ->
@ -25,7 +24,7 @@ exports.create = permissions.user (params, options, done) ->
exports.list = permissions.user (params, options, done) -> exports.list = permissions.user (params, options, done) ->
resin.models.application.getAll (error, applications) -> resin.models.application.getAll (error, applications) ->
return done(error) if error? return done(error) if error?
log.out ui.widgets.table.horizontal applications, [ console.log ui.widgets.table.horizontal applications, [
'ID' 'ID'
'Name' 'Name'
'Device Display Name' 'Device Display Name'
@ -37,7 +36,7 @@ exports.list = permissions.user (params, options, done) ->
exports.info = permissions.user (params, options, done) -> exports.info = permissions.user (params, options, done) ->
resin.models.application.get params.id, (error, application) -> resin.models.application.get params.id, (error, application) ->
return done(error) if error? return done(error) if error?
log.out ui.widgets.table.vertical application, [ console.log ui.widgets.table.vertical application, [
'ID' 'ID'
'Name' 'Name'
'Device Display Name' 'Device Display Name'

View File

@ -3,7 +3,6 @@ url = require('url')
async = require('async') async = require('async')
resin = require('resin-sdk') resin = require('resin-sdk')
ui = require('../ui') ui = require('../ui')
log = require('../log/log')
permissions = require('../permissions/permissions') permissions = require('../permissions/permissions')
helpers = require('../helpers/helpers') helpers = require('../helpers/helpers')
@ -45,4 +44,4 @@ exports.whoami = permissions.user (params, options, done) ->
if not username? if not username?
return done(new Error('Username not found')) return done(new Error('Username not found'))
log.out(username) console.log(username)

View File

@ -2,13 +2,12 @@ _ = require('lodash-contrib')
async = require('async') async = require('async')
resin = require('resin-sdk') resin = require('resin-sdk')
ui = require('../ui') ui = require('../ui')
log = require('../log/log')
permissions = require('../permissions/permissions') permissions = require('../permissions/permissions')
exports.list = permissions.user (params, options, done) -> exports.list = permissions.user (params, options, done) ->
resin.models.device.getAllByApplication options.application, (error, devices) -> resin.models.device.getAllByApplication options.application, (error, devices) ->
return done(error) if error? return done(error) if error?
log.out ui.widgets.table.horizontal devices, [ console.log ui.widgets.table.horizontal devices, [
'ID' 'ID'
'Name' 'Name'
'Device Display Name' 'Device Display Name'
@ -23,7 +22,7 @@ exports.list = permissions.user (params, options, done) ->
exports.info = permissions.user (params, options, done) -> exports.info = permissions.user (params, options, done) ->
resin.models.device.get params.id, (error, device) -> resin.models.device.get params.id, (error, device) ->
return done(error) if error? return done(error) if error?
log.out ui.widgets.table.vertical device, [ console.log ui.widgets.table.vertical device, [
'ID' 'ID'
'Name' 'Name'
'Device Display Name' 'Device Display Name'
@ -64,4 +63,4 @@ exports.rename = permissions.user (params, options, done) ->
exports.supported = permissions.user -> exports.supported = permissions.user ->
devices = resin.models.device.getSupportedDeviceTypes() devices = resin.models.device.getSupportedDeviceTypes()
_.each(devices, _.unary(log.out)) _.each(devices, _.unary(console.log))

View File

@ -2,7 +2,6 @@ _ = require('lodash-contrib')
resin = require('resin-sdk') resin = require('resin-sdk')
ui = require('../ui') ui = require('../ui')
permissions = require('../permissions/permissions') permissions = require('../permissions/permissions')
log = require('../log/log')
exports.list = permissions.user (params, options, done) -> exports.list = permissions.user (params, options, done) ->
resin.models.environmentVariables.getAllByApplication options.application, (error, environmentVariables) -> resin.models.environmentVariables.getAllByApplication options.application, (error, environmentVariables) ->
@ -11,7 +10,7 @@ exports.list = permissions.user (params, options, done) ->
if not options.verbose if not options.verbose
environmentVariables = _.reject(environmentVariables, resin.models.environmentVariables.isSystemVariable) environmentVariables = _.reject(environmentVariables, resin.models.environmentVariables.isSystemVariable)
log.out(ui.widgets.table.horizontal(environmentVariables)) console.log(ui.widgets.table.horizontal(environmentVariables))
return done() return done()
exports.remove = permissions.user (params, options, done) -> exports.remove = permissions.user (params, options, done) ->
@ -26,7 +25,7 @@ exports.add = permissions.user (params, options, done) ->
if not params.value? if not params.value?
return done(new Error("Environment value not found for key: #{params.key}")) return done(new Error("Environment value not found for key: #{params.key}"))
else else
log.info("Warning: using #{params.key}=#{params.value} from host environment") console.info("Warning: using #{params.key}=#{params.value} from host environment")
resin.models.environmentVariables.create(options.application, params.key, params.value, done) resin.models.environmentVariables.create(options.application, params.key, params.value, done)

View File

@ -6,7 +6,6 @@ gitCli = require('git-cli')
resin = require('resin-sdk') resin = require('resin-sdk')
permissions = require('../permissions/permissions') permissions = require('../permissions/permissions')
ui = require('../ui') ui = require('../ui')
log = require('../log/log')
examplesData = require('../data/examples.json') examplesData = require('../data/examples.json')
exports.list = permissions.user -> exports.list = permissions.user ->
@ -19,7 +18,7 @@ exports.list = permissions.user ->
example.author ?= 'Unknown' example.author ?= 'Unknown'
return example return example
log.out ui.widgets.table.horizontal examplesData, [ console.log ui.widgets.table.horizontal examplesData, [
'ID' 'ID'
'Display Name' 'Display Name'
'Repository' 'Repository'
@ -36,7 +35,7 @@ exports.info = permissions.user (params, options, done) ->
example.id = id example.id = id
example.author ?= 'Unknown' example.author ?= 'Unknown'
log.out ui.widgets.table.vertical example, [ console.log ui.widgets.table.vertical example, [
'ID' 'ID'
'Display Name' 'Display Name'
'Description' 'Description'
@ -63,7 +62,7 @@ exports.clone = permissions.user (params, options, done) ->
return callback(error) return callback(error)
(callback) -> (callback) ->
log.info("Cloning #{example.display_name} to #{example.name}") console.info("Cloning #{example.display_name} to #{example.name}")
gitCli.Repository.clone(example.repository, example.name, callback) gitCli.Repository.clone(example.repository, example.name, callback)
], done ], done

View File

@ -2,7 +2,6 @@ _ = require('lodash')
_.str = require('underscore.string') _.str = require('underscore.string')
resin = require('resin-sdk') resin = require('resin-sdk')
capitano = require('capitano') capitano = require('capitano')
log = require('../log/log')
# TODO: Refactor this terrible mess # TODO: Refactor this terrible mess
@ -62,14 +61,14 @@ getOptionHelp = (option, maxLength) ->
return result return result
exports.general = -> exports.general = ->
log.out("Usage: #{process.argv[0]} [COMMAND] [OPTIONS]\n") console.log("Usage: #{process.argv[0]} [COMMAND] [OPTIONS]\n")
log.out('Commands:\n') console.log('Commands:\n')
for command in capitano.state.commands for command in capitano.state.commands
continue if command.isWildcard() continue if command.isWildcard()
log.out(getCommandHelp(command)) console.log(getCommandHelp(command))
log.out('\nGlobal Options:\n') console.log('\nGlobal Options:\n')
options = _.map capitano.state.globalOptions, (option) -> options = _.map capitano.state.globalOptions, (option) ->
option.signature = buildOptionSignatureHelp(option) option.signature = buildOptionSignatureHelp(option)
@ -79,9 +78,9 @@ exports.general = ->
return option.signature.length return option.signature.length
for option in options for option in options
log.out(getOptionHelp(option, optionSignatureMaxLength)) console.log(getOptionHelp(option, optionSignatureMaxLength))
log.out() console.log()
exports.command = (params) -> exports.command = (params) ->
command = capitano.state.getMatchCommand(params.command) command = capitano.state.getMatchCommand(params.command)
@ -89,15 +88,15 @@ exports.command = (params) ->
if not command? or command.isWildcard() if not command? or command.isWildcard()
return capitano.defaults.actions.commandNotFound(params.command) return capitano.defaults.actions.commandNotFound(params.command)
log.out("Usage: #{command.signature}") console.log("Usage: #{command.signature}")
if command.help? if command.help?
log.out("\n#{command.help}") console.log("\n#{command.help}")
else if command.description? else if command.description?
log.out("\n#{_.str.humanize(command.description)}") console.log("\n#{_.str.humanize(command.description)}")
if not _.isEmpty(command.options) if not _.isEmpty(command.options)
log.out('\nOptions:\n') console.log('\nOptions:\n')
options = _.map command.options, (option) -> options = _.map command.options, (option) ->
option.signature = buildOptionSignatureHelp(option) option.signature = buildOptionSignatureHelp(option)
@ -107,9 +106,9 @@ exports.command = (params) ->
return option.signature.toString().length return option.signature.toString().length
for option in options for option in options
log.out(getOptionHelp(option, optionSignatureMaxLength)) console.log(getOptionHelp(option, optionSignatureMaxLength))
log.out() console.log()
exports.help = (params) -> exports.help = (params) ->
if params.command? if params.command?

View File

@ -1,5 +1,4 @@
packageJSON = require('../../package.json') packageJSON = require('../../package.json')
log = require('../log/log')
exports.version = -> exports.version = ->
log.out(packageJSON.version) console.log(packageJSON.version)

View File

@ -5,20 +5,19 @@ fs = require('fs')
resin = require('resin-sdk') resin = require('resin-sdk')
helpers = require('../helpers/helpers') helpers = require('../helpers/helpers')
ui = require('../ui') ui = require('../ui')
log = require('../log/log')
permissions = require('../permissions/permissions') permissions = require('../permissions/permissions')
exports.list = permissions.user (params, options, done) -> exports.list = permissions.user (params, options, done) ->
resin.models.key.getAll (error, keys) -> resin.models.key.getAll (error, keys) ->
return done(error) if error? return done(error) if error?
log.out ui.widgets.table.horizontal keys, [ 'ID', 'Title' ] console.log ui.widgets.table.horizontal keys, [ 'ID', 'Title' ]
return done() return done()
exports.info = permissions.user (params, options, done) -> exports.info = permissions.user (params, options, done) ->
resin.models.key.get params.id, (error, key) -> resin.models.key.get params.id, (error, key) ->
return done(error) if error? return done(error) if error?
key.public_key = '\n' + _.str.chop(key.public_key, resin.settings.get('sshKeyWidth')).join('\n') key.public_key = '\n' + _.str.chop(key.public_key, resin.settings.get('sshKeyWidth')).join('\n')
log.out(ui.widgets.table.vertical(key, [ 'ID', 'Title', 'Public Key' ])) console.log(ui.widgets.table.vertical(key, [ 'ID', 'Title', 'Public Key' ]))
return done() return done()
exports.remove = permissions.user (params, options, done) -> exports.remove = permissions.user (params, options, done) ->

View File

@ -1,6 +1,6 @@
_ = require('lodash')
resin = require('resin-sdk') resin = require('resin-sdk')
permissions = require('../permissions/permissions') permissions = require('../permissions/permissions')
log = require('../log/log')
LOGS_HISTORY_COUNT = 200 LOGS_HISTORY_COUNT = 200
@ -11,5 +11,5 @@ exports.logs = permissions.user (params, options, done) ->
tail: options.tail tail: options.tail
}, (error, message) -> }, (error, message) ->
return done(error) if error? return done(error) if error?
log.array(message, log.out) _.each(message, console.log)
return done() return done()

View File

@ -4,7 +4,6 @@ path = require('path')
mkdirp = require('mkdirp') mkdirp = require('mkdirp')
ProgressBar = require('progress') ProgressBar = require('progress')
resin = require('resin-sdk') resin = require('resin-sdk')
log = require('../log/log')
permissions = require('../permissions/permissions') permissions = require('../permissions/permissions')
exports.download = (params, options, done) -> exports.download = (params, options, done) ->
@ -28,7 +27,7 @@ exports.download = (params, options, done) ->
return callback(error) return callback(error)
(callback) -> (callback) ->
log.info("Destination file: #{outputFile}") console.info("Destination file: #{outputFile}")
bar = null bar = null
received = 0 received = 0
@ -49,5 +48,5 @@ exports.download = (params, options, done) ->
], (error) -> ], (error) ->
return done(error) if error? return done(error) if error?
log.info("\nFinished downloading #{outputFile}") console.info("\nFinished downloading #{outputFile}")
return done() return done()

View File

@ -3,7 +3,6 @@ path = require('path')
capitano = require('capitano') capitano = require('capitano')
resin = require('resin-sdk') resin = require('resin-sdk')
actions = require('./actions') actions = require('./actions')
log = require('./log/log')
errors = require('./errors/errors') errors = require('./errors/errors')
plugin = require('./plugin/plugin') plugin = require('./plugin/plugin')
@ -611,7 +610,8 @@ changeProjectDirectory = (directory) ->
resin.data.prefix.set resin.settings.get('dataPrefix'), (error) -> resin.data.prefix.set resin.settings.get('dataPrefix'), (error) ->
errors.handle(error) if error? errors.handle(error) if error?
log.setQuiet(cli.global.quiet) if cli.global.quiet
console.info = _.noop
if cli.global.project? if cli.global.project?
changeProjectDirectory(cli.global.project) changeProjectDirectory(cli.global.project)

View File

@ -1,20 +1,19 @@
_ = require('lodash') _ = require('lodash')
log = require('../log/log')
exports.handle = (error, exit = true) -> exports.handle = (error, exit = true) ->
return if not error? or error not instanceof Error return if not error? or error not instanceof Error
if process.env.DEBUG if process.env.DEBUG
log.error(error.stack) console.error(error.stack)
else else
if error.code is 'EISDIR' if error.code is 'EISDIR'
log.error("File is a directory: #{error.path}") console.error("File is a directory: #{error.path}")
else if error.code is 'ENOENT' else if error.code is 'ENOENT'
log.error("No such file or directory: #{error.path}") console.error("No such file or directory: #{error.path}")
else if error.message? else if error.message?
log.error(error.message) console.error(error.message)
if _.isNumber(error.exitCode) if _.isNumber(error.exitCode)
errorCode = error.exitCode errorCode = error.exitCode

View File

@ -1,7 +1,6 @@
_ = require('lodash') _ = require('lodash')
expect = require('chai').expect expect = require('chai').expect
sinon = require('sinon') sinon = require('sinon')
log = require('../log/log')
errors = require('./errors') errors = require('./errors')
describe 'Errors:', -> describe 'Errors:', ->
@ -10,14 +9,14 @@ describe 'Errors:', ->
it 'should log the error message to stderr', -> it 'should log the error message to stderr', ->
message = 'Hello World' message = 'Hello World'
logErrorStub = sinon.stub(log, 'error') logErrorStub = sinon.stub(console, 'error')
error = new Error(message) error = new Error(message)
errors.handle(error, false) errors.handle(error, false)
expect(logErrorStub).to.have.been.calledWith(message) expect(logErrorStub).to.have.been.calledWith(message)
logErrorStub.restore() logErrorStub.restore()
it 'should do nothing if error is not an instance of Error', -> it 'should do nothing if error is not an instance of Error', ->
logErrorStub = sinon.stub(log, 'error') logErrorStub = sinon.stub(console, 'error')
for item in [ for item in [
undefined undefined
@ -33,7 +32,7 @@ describe 'Errors:', ->
checkProcessExitOption = (error, value, expectations) -> checkProcessExitOption = (error, value, expectations) ->
processExitStub = sinon.stub(process, 'exit') processExitStub = sinon.stub(process, 'exit')
logErrorStub = sinon.stub(log, 'error') logErrorStub = sinon.stub(console, 'error')
errors.handle(error, value) errors.handle(error, value)
expectations(processExitStub, logErrorStub) expectations(processExitStub, logErrorStub)
processExitStub.restore() processExitStub.restore()

View File

@ -1,41 +0,0 @@
_ = require('lodash')
# @nodoc
isQuiet = false
exports.templates =
error: '<%= text %>'
warn: '<%= text %>'
info: '<%= text %>'
log: '<%= text %>'
exports.setQuiet = (quiet) ->
isQuiet = !!quiet
exports.isQuiet = ->
return isQuiet
exports.error = (args...) ->
console.error.apply(null, args)
exports.warning = (args...) ->
console.warn.apply(null, args)
exports.info = (args...) ->
return if exports.isQuiet()
console.info.apply(null, 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)

View File

@ -1,133 +0,0 @@
_ = require('lodash')
chai = require('chai')
chai.use(require('sinon-chai'))
expect = chai.expect
sinon = require('sinon')
log = require('./log')
describe 'Log:', ->
testConsoleFunction = (logName, consoleName, message, assertions) ->
spy = sinon.spy(console, consoleName)
log[logName](message)
assertions(spy)
console[consoleName].restore()
testConsoleFunctionBeingCalled = (logName, consoleName, message) ->
testConsoleFunction logName, consoleName, message, (spy) ->
expect(spy).to.have.been.calledOnce
expect(spy).to.have.been.calledWith(message)
testConsoleFunctionNotBeingCalled = (logName, consoleName, message) ->
testConsoleFunction logName, consoleName, message, (spy) ->
expect(spy).to.not.have.been.called
describe 'if quiet is false', ->
beforeEach ->
log.setQuiet(false)
describe '#error()', ->
it 'should output to console.error', ->
testConsoleFunctionBeingCalled('error', 'error', '')
describe '#warning()', ->
it 'should output to console.warn', ->
testConsoleFunctionBeingCalled('warning', 'warn', '')
describe '#info()', ->
it 'should output to console.info', ->
testConsoleFunctionBeingCalled('info', 'info', '')
describe '#out()', ->
it 'should output to console.log', ->
testConsoleFunctionBeingCalled('out', 'log', '')
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', ->
expect(log.isQuiet()).to.be.false
log.setQuiet(true)
expect(log.isQuiet()).to.be.true
describe '#isQuiet()', ->
it 'should return false by default', ->
expect(log.isQuiet()).to.be.false
describe 'if quiet is true', ->
beforeEach ->
log.setQuiet(true)
describe '#error()', ->
it 'should still output to console.error', ->
testConsoleFunctionBeingCalled('error', 'error', '')
describe '#warning()', ->
it 'should still output to console.warn', ->
testConsoleFunctionBeingCalled('warning', 'warn', '')
describe '#info()', ->
it 'should not call console.info', ->
testConsoleFunctionNotBeingCalled('info', 'info', '')
describe '#out()', ->
it 'should not call console.log', ->
testConsoleFunctionBeingCalled('out', 'log', '')