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

Commit ea53071

Browse files
author
Alan Shaw
authored
feat: pipe to add (#1833)
Allows `echo "hello" | jsipfs add` resolves #1829 License: MIT Signed-off-by: Alan Shaw <[email protected]>
1 parent 7d9b006 commit ea53071

File tree

3 files changed

+32
-17
lines changed

3 files changed

+32
-17
lines changed

src/cli/commands/add.js

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const byteman = require('byteman')
77
const reduce = require('async/reduce')
88
const mh = require('multihashes')
99
const multibase = require('multibase')
10+
const toPull = require('stream-to-pull-stream')
1011
const { print, isDaemonOn, createProgressBar } = require('../utils')
1112
const { cidToString } = require('../../utils/cid')
1213
const globSource = require('../../utils/files/glob-source')
@@ -20,15 +21,9 @@ function getTotalBytes (paths, cb) {
2021
}, cb)
2122
}
2223

23-
function addPipeline (paths, addStream, options) {
24-
const {
25-
recursive,
26-
quiet,
27-
quieter,
28-
silent
29-
} = options
24+
function addPipeline (source, addStream, options) {
3025
pull(
31-
globSource(...paths, { recursive }),
26+
source,
3227
addStream,
3328
pull.collect((err, added) => {
3429
if (err) {
@@ -39,14 +34,15 @@ function addPipeline (paths, addStream, options) {
3934
throw err
4035
}
4136

42-
if (silent) return
43-
if (quieter) return print(added.pop().hash)
37+
if (options.silent) return
38+
if (options.quieter) return print(added.pop().hash)
4439

4540
sortBy(added, 'path')
4641
.reverse()
4742
.map((file) => {
48-
const log = [ 'added', cidToString(file.hash, { base: options.cidBase }) ]
49-
if (!quiet && file.path.length > 0) log.push(file.path)
43+
const log = options.quiet ? [] : ['added']
44+
log.push(cidToString(file.hash, { base: options.cidBase }))
45+
if (!options.quiet && file.path.length > 0) log.push(file.path)
5046
return log.join(' ')
5147
})
5248
.forEach((msg) => print(msg))
@@ -55,7 +51,7 @@ function addPipeline (paths, addStream, options) {
5551
}
5652

5753
module.exports = {
58-
command: 'add <file...>',
54+
command: 'add [file...]',
5955

6056
describe: 'Add a file to IPFS using the UnixFS data format',
6157

@@ -163,8 +159,15 @@ module.exports = {
163159
throw new Error('Error: Enabling the sharding experiment should be done on the daemon')
164160
}
165161

166-
if (!argv.progress) {
167-
return addPipeline(argv.file, ipfs.addPullStream(options), argv)
162+
const source = argv.file
163+
? globSource(...argv.file, { recursive: argv.recursive })
164+
: toPull.source(process.stdin) // Pipe directly to ipfs.add
165+
166+
const adder = ipfs.addPullStream(options)
167+
168+
// No progress or piping directly to ipfs.add: no need to getTotalBytes
169+
if (!argv.progress || !argv.file) {
170+
return addPipeline(source, adder, argv)
168171
}
169172

170173
getTotalBytes(argv.file, (err, totalBytes) => {
@@ -176,7 +179,7 @@ module.exports = {
176179
bar.update(byteLength / totalBytes, { progress: byteman(byteLength, 2, 'MB') })
177180
}
178181

179-
addPipeline(argv.file, ipfs.addPullStream(options), argv)
182+
addPipeline(source, adder, argv)
180183
})
181184
}
182185
}

test/cli/files.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,13 +277,24 @@ describe('files', () => runOnAndOff((thing) => {
277277
})
278278
})
279279

280+
it('add from pipe', () => {
281+
const proc = ipfs('add')
282+
proc.stdin.write(Buffer.from('hello\n'))
283+
proc.stdin.end()
284+
return proc
285+
.then(out => {
286+
expect(out)
287+
.to.eql('added QmZULkCELmmk5XNfCgTnCyFgAVxBRBXyDHGGMVoLFLiXEN QmZULkCELmmk5XNfCgTnCyFgAVxBRBXyDHGGMVoLFLiXEN\n')
288+
})
289+
})
290+
280291
it('add --quiet', function () {
281292
this.timeout(30 * 1000)
282293

283294
return ipfs('add -q src/init-files/init-docs/readme')
284295
.then((out) => {
285296
expect(out)
286-
.to.eql('added QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB\n')
297+
.to.eql('QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB\n')
287298
})
288299
})
289300

test/utils/ipfs-exec.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ module.exports = (repoPath, opts) => {
4747
})
4848

4949
res.kill = cp.kill.bind(cp)
50+
res.stdin = cp.stdin
5051
res.stdout = cp.stdout
5152
res.stderr = cp.stderr
5253

0 commit comments

Comments
 (0)