diff --git a/src/cli/commands/id.js b/src/cli/commands/id.js index deedf71f28..52bc904f9d 100644 --- a/src/cli/commands/id.js +++ b/src/cli/commands/id.js @@ -1,7 +1,23 @@ var Command = require('ronin').Command +var IPFS = require('../../ipfs-core') module.exports = Command.extend({ - desc: '', + desc: 'Shows IPFS Node ID info', - run: function (name) {} + options: { + format: { + alias: 'f', + type: 'string' + } + }, + + run: function (name) { + var node = new IPFS() + node.id(function (err, id) { + if (err) { + return console.error(err) + } + console.log(id) + }) + } }) diff --git a/src/ipfs-core/index.js b/src/ipfs-core/index.js index 2329d6bd25..4fa57991bf 100644 --- a/src/ipfs-core/index.js +++ b/src/ipfs-core/index.js @@ -41,10 +41,7 @@ function IPFS () { callback = opts opts = {} } - - if (!repo.exists()) { - callback(new Error('Repo does not exist, you must init repo first')) - } else { repo.load() } + repoExists(callback) repo.config.read(function (err, config) { if (err) { @@ -54,7 +51,27 @@ function IPFS () { }) } - self.id = function (format, callback) {} + self.id = function (opts, callback) { + if (typeof opts === 'function') { + callback = opts + opts = {} + } + repoExists(callback) + + repo.config.read(function (err, config) { + if (err) { + return callback(err) + } + callback(null, { + ID: config.Identity.PeerID, + // TODO needs https://github.com/diasdavid/js-peer-id/blob/master/src/index.js#L76 + PublicKey: '', + Addresses: config.Addresses, + AgentVersion: 'js-ipfs', + ProtocolVersion: '9000' + }) + }) + } self.repo = { init: function (bits, force, empty, callback) { @@ -66,13 +83,17 @@ function IPFS () { callback = opts opts = {} } - if (!repo.exists()) { - callback(new Error('Repo does not exist, you must init repo first')) - } else { repo.load() } + repoExists(callback) repo.version.read(callback) }, gc: function () {} } + + function repoExists (callback) { + if (!repo.exists()) { + callback(new Error('Repo does not exist, you must init repo first')) + } else { repo.load() } + } } diff --git a/tests/jsipfs-test.js b/tests/jsipfs-test.js index 872993ccbd..49ac1f0bd1 100644 --- a/tests/jsipfs-test.js +++ b/tests/jsipfs-test.js @@ -46,4 +46,12 @@ describe('IPFS Repo Tests', function () { done() }) }) + + it('check id info', function (done) { + node.id(function (err, id) { + expect(err).to.equal(null) + expect(id).to.be.a('object') + done() + }) + }) })