Skip to content

Commit

Permalink
refactor: add missing log cmds and modularize log api(ipfs-inactive#544)
Browse files Browse the repository at this point in the history
  • Loading branch information
nunofmn committed Jun 18, 2017
1 parent 6ff0ea7 commit 5ca09af
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 22 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,12 @@ This means:
- [`ipfs.id([callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/API/generic#id)
- [`ipfs.version([callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/API/generic#version)
- [`ipfs.ping()`](https://github.com/ipfs/interface-ipfs-core/tree/master/API/generic#ping)
- [`ipfs.log()`](https://github.com/ipfs/interface-ipfs-core/tree/master/API/generic#log)

#### [log](https://github.com/ipfs/interface-ipfs-core/tree/master/API/generic)

- [`ipfs.log.ls([callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/API/generic)
- [`ipfs.log.tail([callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/API/generic)
- [`ipfs.log.level(subsystem, level, [options, callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/API/generic)

#### [key](https://github.com/ipfs/interface-ipfs-core/tree/master/API/key)

Expand Down
21 changes: 0 additions & 21 deletions src/api/log.js

This file was deleted.

9 changes: 9 additions & 0 deletions src/api/log/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict'

module.exports = (arg) => {
return {
tail: require('./tail')(arg),
ls: require('./ls')(arg),
level: require('./level')(arg)
}
}
30 changes: 30 additions & 0 deletions src/api/log/level.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict'

const promisify = require('promisify-es6')
const moduleConfig = require('../../module-config')

module.exports = (arg) => {
const send = moduleConfig(arg)

return promisify((subsystem, level, opts, callback) => {
if (typeof opts === 'function') {
callback = opts
opts = {}
}
if (typeof subsystem !== 'string') {
return callback(new Error('Invalid subsystem type'))
}

if (typeof level !== 'string') {
return callback(new Error('Invalid level type'))
}

send({
path: 'log/level',
args: [subsystem, level],
qs: opts,
files: undefined,
buffer: true
}, callback)
})
}
20 changes: 20 additions & 0 deletions src/api/log/ls.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict'

const promisify = require('promisify-es6')
const moduleConfig = require('../../module-config')

module.exports = (arg) => {
const send = moduleConfig(arg)

return promisify((callback) => {
send({
path: 'log/ls'
}, (err, result) => {
if (err) {
return callback(err)
}

callback(null, result.Strings)
})
})
}
22 changes: 22 additions & 0 deletions src/api/log/tail.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict'

const promisify = require('promisify-es6')
const pump = require('pump')
const ndjson = require('ndjson')
const moduleConfig = require('../../module-config')

module.exports = (arg) => {
const send = moduleConfig(arg)

return promisify((callback) => {
return send({
path: 'log/tail'
}, (err, response) => {
if (err) {
return callback(err)
}
const outputStream = pump(response, ndjson.parse())
callback(null, outputStream)
})
})
}
44 changes: 44 additions & 0 deletions test/log.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,30 @@ describe('.log', () => {
})
})
})

it('.log.ls', (done) => {
ipfs.log.ls((err, res) => {
expect(err).to.not.exist()
expect(res).to.exist()

expect(res).to.be.an('array')

done()
})
})

it('.log.level', (done) => {
ipfs.log.level('all', 'error', (err, res) => {
expect(err).to.not.exist()
expect(res).to.exist()

expect(res).to.be.an('object')
expect(res).to.not.have.property('Error')
expect(res).to.have.property('Message')

done()
})
})
})

describe('Promise API', () => {
Expand All @@ -47,5 +71,25 @@ describe('.log', () => {
})
})
})

it('.log.ls', () => {
return ipfs.log.ls()
.then((res) => {
expect(res).to.exist()

expect(res).to.be.an('array')
})
})

it('.log.level', () => {
return ipfs.log.level('all', 'error')
.then((res) => {
expect(res).to.exist()

expect(res).to.be.an('object')
expect(res).to.not.have.property('Error')
expect(res).to.have.property('Message')
})
})
})
})
17 changes: 17 additions & 0 deletions test/sub-modules.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,21 @@ describe('submodules', () => {
expect(ping).to.be.a('function')
})
})

describe('log', () => {
it('.ls', () => {
const ls = require('../src/api/log/ls')
expect(ls).to.be.a('function')
})

it('.tail', () => {
const tail = require('../src/api/log/tail')
expect(tail).to.be.a('function')
})

it('.level', () => {
const level = require('../src/api/log/level')
expect(level).to.be.a('function')
})
})
})

0 comments on commit 5ca09af

Please sign in to comment.