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

Commit a285ee7

Browse files
committed
Make cli connect to the daemon if it's running
1 parent d2f6d10 commit a285ee7

File tree

7 files changed

+74
-23
lines changed

7 files changed

+74
-23
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
"ipfs-api": "^2.13.1",
7777
"ipfs-blocks": "^0.1.0",
7878
"ipfs-merkle-dag": "^0.2.1",
79-
"ipfs-multipart": "0.0.1",
79+
"ipfs-multipart": "^0.1.0",
8080
"ipfs-repo": "^0.5.0",
8181
"joi": "^8.0.2",
8282
"lodash.get": "^4.0.0",

src/cli/commands/object/data.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict'
22

33
const Command = require('ronin').Command
4-
const IPFS = require('../../../ipfs-core')
4+
const utils = require('../../utils')
55
const bs58 = require('bs58')
66
const debug = require('debug')
77
const log = debug('cli:object')
@@ -13,20 +13,29 @@ module.exports = Command.extend({
1313
options: {},
1414

1515
run: (key) => {
16-
var node = new IPFS()
16+
var ipfs = utils.getIPFS()
1717

1818
if (!key) {
1919
throw new Error("Argument 'key' is required")
2020
}
2121

22-
const mh = new Buffer(bs58.decode(key))
23-
node.object.data(mh, (err, data) => {
22+
const mh = utils.isDaemonOn()
23+
? key
24+
: new Buffer(bs58.decode(key))
25+
26+
ipfs.object.data(mh, (err, data) => {
2427
if (err) {
2528
log.error(err)
2629
throw err
2730
}
2831

29-
console.log(data.toString())
32+
if (data instanceof Buffer) {
33+
console.log(data.toString())
34+
return
35+
}
36+
37+
// js-ipfs-api output (http stream)
38+
data.pipe(process.stdout)
3039
})
3140
}
3241
})

src/cli/commands/object/get.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict'
22

33
const Command = require('ronin').Command
4-
const IPFS = require('../../../ipfs-core')
4+
const utils = require('../../utils')
55
const bs58 = require('bs58')
66
const debug = require('debug')
77
const log = debug('cli:object')
@@ -13,14 +13,25 @@ module.exports = Command.extend({
1313
options: {},
1414

1515
run: (key) => {
16-
var node = new IPFS()
16+
var ipfs = utils.getIPFS()
1717

1818
if (!key) {
1919
throw new Error("Argument 'key' is required")
2020
}
2121

22+
if (utils.isDaemonOn()) {
23+
return ipfs.object.get(key, (err, obj) => {
24+
if (err) {
25+
log.error(err)
26+
throw err
27+
}
28+
29+
console.log(JSON.stringify(obj))
30+
})
31+
}
32+
2233
const mh = new Buffer(bs58.decode(key))
23-
node.object.get(mh, (err, obj) => {
34+
ipfs.object.get(mh, (err, obj) => {
2435
if (err) {
2536
log.error(err)
2637
throw err

src/cli/commands/object/links.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict'
22

33
const Command = require('ronin').Command
4-
const IPFS = require('../../../ipfs-core')
4+
const utils = require('../../utils')
55
const bs58 = require('bs58')
66
const debug = require('debug')
77
const log = debug('cli:object')
@@ -13,19 +13,29 @@ module.exports = Command.extend({
1313
options: {},
1414

1515
run: (key) => {
16-
var node = new IPFS()
16+
var ipfs = utils.getIPFS()
1717

1818
if (!key) {
1919
throw new Error("Argument 'key' is required")
2020
}
2121

22-
const mh = new Buffer(bs58.decode(key))
23-
node.object.links(mh, (err, links) => {
22+
const mh = utils.isDaemonOn()
23+
? key
24+
: new Buffer(bs58.decode(key))
25+
26+
ipfs.object.links(mh, (err, links) => {
2427
if (err) {
2528
log.error(err)
2629
throw err
2730
}
2831

32+
if (links.Links) { // js-ipfs-api output
33+
links.Links.forEach((link) => {
34+
console.log(link.Hash, link.Size, link.Name)
35+
})
36+
return
37+
}
38+
2939
links.forEach((link) => {
3040
console.log(bs58.encode(link.hash).toString(), link.size, link.name)
3141
})

src/cli/commands/object/new.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict'
22

33
const Command = require('ronin').Command
4-
const IPFS = require('../../../ipfs-core')
4+
const utils = require('../../utils')
55
const bs58 = require('bs58')
66
const debug = require('debug')
77
const log = debug('cli:object')
@@ -13,14 +13,19 @@ module.exports = Command.extend({
1313
options: {},
1414

1515
run: (template) => {
16-
var node = new IPFS()
16+
var ipfs = utils.getIPFS()
1717

18-
node.object.new(template, (err, obj) => {
18+
ipfs.object.new(template, (err, obj) => {
1919
if (err) {
2020
log.error(err)
2121
throw err
2222
}
2323

24+
if (typeof obj.Hash === 'string') { // js-ipfs-api output
25+
console.log(obj.Hash)
26+
return
27+
}
28+
2429
console.log(bs58.encode(obj.Hash).toString())
2530
})
2631
}

src/cli/commands/object/put.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict'
22

33
const Command = require('ronin').Command
4-
const IPFS = require('../../../ipfs-core')
4+
const utils = require('../../utils')
55
const bs58 = require('bs58')
66
const bl = require('bl')
77
const fs = require('fs')
@@ -17,9 +17,20 @@ module.exports = Command.extend({
1717
options: {},
1818

1919
run: (filePath) => {
20-
var node = new IPFS()
20+
var ipfs = utils.getIPFS()
2121

2222
function parseAndAddNode (buf) {
23+
if (utils.isDaemonOn()) {
24+
return ipfs.object.put(buf, 'json', (err, obj) => {
25+
if (err) {
26+
log.error(err)
27+
throw err
28+
}
29+
30+
console.log('added', obj.Hash)
31+
})
32+
}
33+
2334
let parsed
2435
try {
2536
parsed = JSON.parse(buf.toString())
@@ -38,7 +49,7 @@ module.exports = Command.extend({
3849

3950
const dagNode = new DAGNode(data, links)
4051

41-
node.object.put(dagNode, (err, obj) => {
52+
ipfs.object.put(dagNode, (err, obj) => {
4253
if (err) {
4354
log.error(err)
4455
throw err

src/cli/commands/object/stat.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict'
22

33
const Command = require('ronin').Command
4-
const IPFS = require('../../../ipfs-core')
4+
const utils = require('../../utils')
55
const bs58 = require('bs58')
66
const debug = require('debug')
77
const log = debug('cli:object')
@@ -13,19 +13,24 @@ module.exports = Command.extend({
1313
options: {},
1414

1515
run: (key) => {
16-
var node = new IPFS()
16+
var ipfs = utils.getIPFS()
1717

1818
if (!key) {
1919
throw new Error("Argument 'key' is required")
2020
}
2121

22-
const mh = new Buffer(bs58.decode(key))
23-
node.object.stat(mh, (err, stats) => {
22+
const mh = utils.isDaemonOn()
23+
? key
24+
: new Buffer(bs58.decode(key))
25+
26+
ipfs.object.stat(mh, (err, stats) => {
2427
if (err) {
2528
log.error(err)
2629
throw err
2730
}
2831

32+
delete stats.Hash // only for js-ipfs-api output
33+
2934
Object.keys(stats).forEach((key) => {
3035
console.log(`${key}: ${stats[key]}`)
3136
})

0 commit comments

Comments
 (0)