diff --git a/package.json b/package.json index c9842aba9c..fd75e69c0e 100644 --- a/package.json +++ b/package.json @@ -8,13 +8,15 @@ "main": "src/index.js", "scripts": { "lint": "standard", - "coverage": "istanbul cover --print both -- _mocha tests/test-*/index.js", + "coverage": "istanbul cover --print both -- _mocha tests/test-core/index.js", "test": "npm run test:node && npm run test:browser", - "test:node": "mocha tests/test-*/index.js", + "test:node": "npm run test:node:core && npm run test:node:http-api && npm run test:node:cli", + "test:node:cli": "mocha tests/test-cli/index.js", + "test:node:core": "mocha tests/test-core/index.js", + "test:node:http-api": "mocha tests/test-http-api/index.js", "test:browser": "karma start karma.conf.js", "test:core": "mocha tests/test-core/index.js", - "test:cli": "mocha tests/test-cli/index.js", - "test:cli-offline": "mocha tests/test-cli-offline/index.js" + "test:cli": "mocha tests/test-cli/index.js" }, "pre-commit": [ "lint", @@ -67,9 +69,11 @@ "bs58": "^3.0.0", "debug": "^2.2.0", "hapi": "^12.0.0", + "ipfs-api": "^2.13.1", "ipfs-blocks": "^0.1.0", "ipfs-merkle-dag": "^0.2.1", "ipfs-repo": "^0.5.0", + "joi": "^8.0.2", "lodash.get": "^4.0.0", "lodash.set": "^4.0.0", "peer-id": "^0.5.0", diff --git a/src/cli/commands/daemon.js b/src/cli/commands/daemon.js index f4ab32e09c..4ee301a7d7 100644 --- a/src/cli/commands/daemon.js +++ b/src/cli/commands/daemon.js @@ -4,15 +4,28 @@ const Command = require('ronin').Command const httpAPI = require('../../http-api') const debug = require('debug') const log = debug('cli:daemon') -log.error = debug('cli:damon:error') +log.error = debug('cli:daemon:error') module.exports = Command.extend({ desc: 'Start a long-running daemon process', run: name => { + console.log('Initializing daemon...') httpAPI.start((err) => { - if (err) { return log.error(err) } - log('daemon started') + if (err) { + return log.error(err) + } + console.log('Daemon is ready') + }) + + process.on('SIGINT', () => { + console.log('Received interrupt signal, shutting down..') + httpAPI.stop((err) => { + if (err) { + return log.error(err) + } + process.exit(0) + }) }) } }) diff --git a/src/cli/commands/id.js b/src/cli/commands/id.js index ba477a5ffd..7287155953 100644 --- a/src/cli/commands/id.js +++ b/src/cli/commands/id.js @@ -1,8 +1,9 @@ const Command = require('ronin').Command const IPFS = require('../../ipfs-core') const debug = require('debug') -const log = debug('cli:id') -log.error = debug('cli:id:error') +const utils = require('../utils') +const log = debug('cli') +log.error = debug('cli:error') module.exports = Command.extend({ desc: 'Shows IPFS Node ID info', @@ -14,11 +15,23 @@ module.exports = Command.extend({ } }, - run: name => { - const node = new IPFS() - node.id((err, id) => { - if (err) { return log.error(err) } - console.log(id) - }) + run: (name) => { + if (utils.isDaemonOn()) { + const ctl = utils.getAPICtl() + ctl.id((err, result) => { + if (err) { + return log.error(err) + } + console.log(result) + }) + } else { + const node = new IPFS() + node.id((err, id) => { + if (err) { + return log.error(err) + } + console.log(id) + }) + } } }) diff --git a/src/cli/utils.js b/src/cli/utils.js new file mode 100644 index 0000000000..23f3b9c4b9 --- /dev/null +++ b/src/cli/utils.js @@ -0,0 +1,32 @@ +const fs = require('fs') +const os = require('os') +const APIctl = require('ipfs-api') +const multiaddr = require('multiaddr') +const debug = require('debug') +const log = debug('cli') +log.error = debug('cli:error') + +exports = module.exports + +const repoPath = process.env.IPFS_PATH || os.homedir() + '/.ipfs' + +exports.isDaemonOn = isDaemonOn +function isDaemonOn () { + try { + fs.readFileSync(repoPath + '/api') + log('daemon is on') + return true + } catch (err) { + log('daemon is off') + return false + } +} + +exports.getAPICtl = () => { + if (!isDaemonOn) { + throw new Error('daemon is not on') + } + + const apiAddr = multiaddr(fs.readFileSync(repoPath + '/api').toString()) + return APIctl(apiAddr.toString()) +} diff --git a/src/help-menu.js b/src/help-menu.js deleted file mode 100644 index 914865bb25..0000000000 --- a/src/help-menu.js +++ /dev/null @@ -1,14 +0,0 @@ -module.exports = function help () { - var help = ['Usage: ' + this.programName + ' ' + this.name + ' [OPTIONS]'] - help.push('\n\nOPTIONS:\n\n') - var options = this.options - Object.keys(options).forEach(function (key) { - var line = ' ' - line = line + '-' + options[key].alias + ', --' + key + '\t' - line = line + options[key].type + '\t' - line = line + '- ' + options[key].desc - help.push(line + '\n') - }) - - return help -} diff --git a/src/http-api/index.js b/src/http-api/index.js index 6d66f0ed75..f7f047322d 100644 --- a/src/http-api/index.js +++ b/src/http-api/index.js @@ -3,38 +3,66 @@ const Hapi = require('hapi') const IPFS = require('../ipfs-core') const debug = require('debug') +const fs = require('fs') +const os = require('os') const log = debug('api') log.error = debug('api:error') exports = module.exports -exports.start = callback => { - // start IPFS and exports.ipfs = new IPFS() +exports.start = (callback) => { + const ipfs = exports.ipfs = new IPFS() - exports.ipfs = new IPFS() + const repoPath = process.env.IPFS_PATH || os.homedir() + '/.ipfs' + try { + fs.statSync(repoPath + '/api') + console.log('This repo is currently being used by another daemon') + process.exit(1) + } catch (err) { + fs.writeFileSync(repoPath + '/api', 'api is on by js-ipfs', {flag: 'w+'}) + } - var server = exports.server = new Hapi.Server({ - connections: { - routes: { - cors: true - } + ipfs.config.show((err, config) => { + if (err) { + return callback(err) } - }) - server.connection({ - port: 9001 - }) + // TODO: set up cors correctly, following config + var server = exports.server = new Hapi.Server({ + connections: { + routes: { + cors: true + } + } + }) + const api = config.Addresses.API.split('/') + const gateway = config.Addresses.Gateway.split('/') - // load routes - require('./routes') + // for the CLI to know the where abouts of the API + fs.writeFileSync(repoPath + '/api', config.Addresses.API) - server.start(err => { - if (err) { return callback(err) } - log('server started: ' + server.info.uri) - callback() + // select which connection with server.select(