Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
Merge pull request #73 from ipfs/feature/swarm
Browse files Browse the repository at this point in the history
Integrate libp2p - Get swarm listener and dialer as part of the mix
  • Loading branch information
daviddias committed Mar 16, 2016
2 parents 8b4dab0 + fdd37b1 commit 725abf4
Show file tree
Hide file tree
Showing 40 changed files with 481 additions and 386 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,11 @@
"ipfs-multipart": "^0.1.0",
"ipfs-repo": "^0.5.0",
"joi": "^8.0.2",
"libp2p-ipfs": "^0.1.0",
"lodash.get": "^4.0.0",
"lodash.set": "^4.0.0",
"peer-id": "^0.6.0",
"peer-info": "^0.6.0",
"ronin": "^0.3.11",
"temp": "^0.8.3"
}
Expand Down
14 changes: 10 additions & 4 deletions src/cli/commands/bootstrap/add.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict'

const Command = require('ronin').Command
const IPFS = require('../../../ipfs-core')
const debug = require('debug')
const log = debug('cli:bootstrap')
const utils = require('../../utils')
log.error = debug('cli:bootstrap:error')

module.exports = Command.extend({
Expand All @@ -12,9 +12,15 @@ module.exports = Command.extend({
options: {},

run: (multiaddr) => {
var node = new IPFS()
node.bootstrap.add(multiaddr, (err, list) => {
if (err) { return log.error(err) }
utils.getIPFS((err, ipfs) => {
if (err) {
throw err
}
ipfs.bootstrap.add(multiaddr, (err, list) => {
if (err) {
return log.error(err)
}
})
})
}
})
20 changes: 13 additions & 7 deletions src/cli/commands/bootstrap/list.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const Command = require('ronin').Command
const IPFS = require('../../../ipfs-core')
const utils = require('../../utils')
const debug = require('debug')
const log = debug('cli:bootstrap')
log.error = debug('cli:bootstrap:error')
Expand All @@ -11,12 +11,18 @@ module.exports = Command.extend({

options: {},

run: (name) => {
var node = new IPFS()
node.bootstrap.list((err, list) => {
if (err) { return log.error(err) }
list.forEach((node) => {
console.log(node)
run: () => {
utils.getIPFS((err, ipfs) => {
if (err) {
throw err
}
ipfs.bootstrap.list((err, list) => {
if (err) {
throw err
}
list.forEach((node) => {
console.log(node)
})
})
})
}
Expand Down
14 changes: 10 additions & 4 deletions src/cli/commands/bootstrap/rm.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
'use strict'

const Command = require('ronin').Command
const IPFS = require('../../../ipfs-core')
const debug = require('debug')
const log = debug('cli:bootstrap')
log.error = debug('cli:bootstrap:error')
const utils = require('../../utils')

module.exports = Command.extend({
desc: 'Removes peers from the bootstrap list',

options: {},

run: (multiaddr) => {
var node = new IPFS()
node.bootstrap.rm(multiaddr, (err, list) => {
if (err) { return log.error(err) }
utils.getIPFS((err, ipfs) => {
if (err) {
throw err
}
ipfs.bootstrap.rm(multiaddr, (err, list) => {
if (err) {
return log.error(err)
}
})
})
}
})
4 changes: 2 additions & 2 deletions src/cli/commands/cat.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var Command = require('ronin').Command
const Command = require('ronin').Command

module.exports = Command.extend({
desc: '',

run: function (name) {}
run: function () {}
})
74 changes: 38 additions & 36 deletions src/cli/commands/config.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
'use strict'

const Command = require('ronin').Command
const IPFS = require('../../ipfs-core')
const debug = require('debug')
const get = require('lodash.get')
const set = require('lodash.set')
const log = debug('cli:config')
log.error = debug('cli:config:error')
const utils = require('../utils')

module.exports = Command.extend({
desc: 'Get and set IPFS config values',
Expand All @@ -27,48 +25,52 @@ module.exports = Command.extend({
throw new Error("argument 'key' is required")
}

var node = new IPFS()

if (!value) {
// Get the value of a given key
utils.getIPFS((err, ipfs) => {
if (err) {
throw err
}

node.config.show((err, config) => {
if (err) {
log.error(err)
throw new Error('failed to read the config')
}
if (!value) {
// Get the value of a given key

const value = get(config, key)
console.log(value)
})
} else {
// Set the new value of a given key
ipfs.config.show((err, config) => {
if (err) {
log.error(err)
throw new Error('failed to read the config')
}

if (bool) {
value = (value === 'true')
} else if (json) {
try {
value = JSON.parse(value)
} catch (err) {
log.error(err)
throw new Error('invalid JSON provided')
}
}
const value = get(config, key)
console.log(value)
})
} else {
// Set the new value of a given key

node.config.show((err, originalConfig) => {
if (err) {
log.error(err)
throw new Error('failed to read the config')
if (bool) {
value = (value === 'true')
} else if (json) {
try {
value = JSON.parse(value)
} catch (err) {
log.error(err)
throw new Error('invalid JSON provided')
}
}

const updatedConfig = set(originalConfig, key, value)
node.config.replace(updatedConfig, (err) => {
ipfs.config.show((err, originalConfig) => {
if (err) {
log.error(err)
throw new Error('failed to save the config')
throw new Error('failed to read the config')
}

const updatedConfig = set(originalConfig, key, value)
ipfs.config.replace(updatedConfig, (err) => {
if (err) {
log.error(err)
throw new Error('failed to save the config')
}
})
})
})
}
}
})
}
})
146 changes: 75 additions & 71 deletions src/cli/commands/config/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,104 +5,108 @@ const spawn = require('child_process').spawn
const fs = require('fs')
const temp = require('temp')
const async = require('async')
const IPFS = require('../../../ipfs-core')
const debug = require('debug')
const log = debug('cli:config')
log.error = debug('cli:config:error')
const utils = require('../../utils')

module.exports = Command.extend({
desc: 'Opens the config file for editing in $EDITOR',

run: (name) => {
const node = new IPFS()
const editor = process.env.EDITOR

if (!editor) {
throw new Error('ENV variable $EDITOR not set')
}
utils.getIPFS((err, ipfs) => {
if (err) {
throw err
}
const editor = process.env.EDITOR

function getConfig (next) {
node.config.show((err, config) => {
if (err) {
log.error(err)
next(new Error('failed to get the config'))
}
if (!editor) {
throw new Error('ENV variable $EDITOR not set')
}

next(null, config)
})
}
function getConfig (next) {
ipfs.config.show((err, config) => {
if (err) {
log.error(err)
next(new Error('failed to get the config'))
}

function saveTempConfig (config, next) {
temp.open('ipfs-config', (err, info) => {
if (err) {
log.error(err)
next(new Error('failed to open the config'))
}
next(null, config)
})
}

fs.write(info.fd, JSON.stringify(config, null, 2))
fs.close(info.fd, (err) => {
function saveTempConfig (config, next) {
temp.open('ipfs-config', (err, info) => {
if (err) {
log.error(err)
next(new Error('failed to open the config'))
}

fs.write(info.fd, JSON.stringify(config, null, 2))
fs.close(info.fd, (err) => {
if (err) {
log.error(err)
next(new Error('failed to open the config'))
}
})

next(null, info.path)
})
}

next(null, info.path)
})
}
function openEditor (path, next) {
const child = spawn(editor, [path], {
stdio: 'inherit'
})

function openEditor (path, next) {
const child = spawn(editor, [path], {
stdio: 'inherit'
})
child.on('exit', (err, code) => {
if (err) {
log.error(err)
throw new Error('error on the editor')
}

child.on('exit', (err, code) => {
if (err) {
log.error(err)
throw new Error('error on the editor')
}
next(null, path)
})
}

next(null, path)
})
}
function readTempConfig (path, next) {
fs.readFile(path, 'utf8', (err, data) => {
if (err) {
log.error(err)
next(new Error('failed to get the updated config'))
}

function readTempConfig (path, next) {
fs.readFile(path, 'utf8', (err, data) => {
if (err) {
log.error(err)
next(new Error('failed to get the updated config'))
}
try {
next(null, JSON.parse(data))
} catch (err) {
log.error(err)
next(new Error(`failed to parse the updated config "${err.message}"`))
}
})
}

try {
next(null, JSON.parse(data))
} catch (err) {
log.error(err)
next(new Error(`failed to parse the updated config "${err.message}"`))
}
})
}
function saveConfig (config, next) {
ipfs.config.replace(config, (err) => {
if (err) {
log.error(err)
next(new Error('failed to save the config'))
}

next()
})
}

function saveConfig (config, next) {
node.config.replace(config, (err) => {
async.waterfall([
getConfig,
saveTempConfig,
openEditor,
readTempConfig,
saveConfig
], (err) => {
if (err) {
log.error(err)
next(new Error('failed to save the config'))
throw err
}

next()
})
}

async.waterfall([
getConfig,
saveTempConfig,
openEditor,
readTempConfig,
saveConfig
], (err) => {
if (err) {
throw err
}
})
}
})
Loading

0 comments on commit 725abf4

Please sign in to comment.