Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

Commit 79981d8

Browse files
committed
feat: integrate with jsipfs cli
License: MIT Signed-off-by: achingbrain <[email protected]>
1 parent f928cd4 commit 79981d8

File tree

19 files changed

+363
-80
lines changed

19 files changed

+363
-80
lines changed

cli.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
'use strict'
2+
3+
module.exports = require('./src/cli')

core.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
'use strict'
2+
3+
module.exports = require('./src/core')

src/cli/cp.js

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

3+
const {
4+
asBoolean
5+
} = require('./utils')
6+
37
module.exports = {
48
command: 'cp <source> <dest>',
59

6-
describe: 'Copy files between locations in the mfs.',
10+
describe: 'Copy files between locations in the mfs',
711

812
builder: {
913
parents: {
1014
alias: 'p',
1115
type: 'boolean',
1216
default: false,
17+
coerce: asBoolean,
1318
describe: 'Create any non-existent intermediate directories'
1419
},
1520
recursive: {
1621
alias: 'r',
1722
type: 'boolean',
1823
default: false,
19-
describe: 'Remove directories recursively'
24+
coerce: asBoolean,
25+
describe: 'Copy directories recursively'
2026
}
2127
},
2228

src/cli/index.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'use strict'
2+
3+
const {
4+
print
5+
} = require('./utils')
6+
7+
const command = {
8+
command: 'files <command>',
9+
10+
description: 'Operations over mfs files (ls, mkdir, rm, etc)',
11+
12+
builder (yargs) {
13+
return yargs.commandDir('.')
14+
},
15+
16+
handler (argv) {
17+
print('Type `jsipfs files --help` for more instructions')
18+
}
19+
}
20+
21+
module.exports = (yargs) => {
22+
return yargs
23+
.command(command)
24+
}

src/cli/ls.js

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
11
'use strict'
22

33
const {
4-
print
4+
print,
5+
asBoolean
56
} = require('./utils')
7+
const {
8+
FILE_SEPARATOR
9+
} = require('../core/utils')
610

711
module.exports = {
8-
command: 'ls <path>',
12+
command: 'ls [path]',
913

10-
describe: 'List directories in the local mutable namespace.',
14+
describe: 'List mfs directories',
1115

1216
builder: {
1317
long: {
1418
alias: 'l',
1519
type: 'boolean',
1620
default: false,
21+
coerce: asBoolean,
1722
describe: 'Use long listing format.'
1823
}
1924
},
@@ -25,20 +30,50 @@ module.exports = {
2530
long
2631
} = argv
2732

28-
ipfs.mfs.ls(path, {
33+
ipfs.mfs.ls(path || FILE_SEPARATOR, {
2934
long
3035
}, (error, files) => {
3136
if (error) {
3237
throw error
3338
}
3439

35-
files.forEach(link => {
36-
if (long) {
37-
return print(`${link.name} ${link.hash} ${link.size}`)
38-
}
40+
if (long) {
41+
const table = []
42+
const lengths = {}
43+
44+
files.forEach(link => {
45+
const row = {
46+
name: `${link.name}`,
47+
hash: `${link.hash}`,
48+
size: `${link.size}`
49+
}
50+
51+
Object.keys(row).forEach(key => {
52+
const value = row[key]
53+
54+
lengths[key] = lengths[key] > value.length ? lengths[key] : value.length
55+
})
56+
57+
table.push(row)
58+
})
59+
60+
table.forEach(row => {
61+
let line = ''
62+
63+
Object.keys(row).forEach(key => {
64+
const value = row[key]
65+
66+
line += value.padEnd(lengths[key] - value.length)
67+
line += '\t'
68+
})
69+
70+
print(line)
71+
})
72+
73+
return
74+
}
3975

40-
print(link.name)
41-
})
76+
files.forEach(link => print(link.name))
4277
})
4378
}
4479
}

src/cli/mkdir.js

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

33
const {
4-
print
4+
print,
5+
asBoolean
56
} = require('./utils')
67

78
module.exports = {
89
command: 'mkdir <path>',
910

10-
describe: 'Make directories.',
11+
describe: 'Make mfs directories',
1112

1213
builder: {
1314
parents: {
1415
alias: 'p',
1516
type: 'boolean',
1617
default: false,
18+
coerce: asBoolean,
1719
describe: 'No error if existing, make parent directories as needed.'
1820
},
1921
cidVersion: {
@@ -28,6 +30,7 @@ module.exports = {
2830
flush: {
2931
alias: 'f',
3032
type: 'boolean',
33+
coerce: asBoolean,
3134
describe: 'Weird undocumented option'
3235
}
3336
},

src/cli/mv.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
11
'use strict'
22

3+
const {
4+
asBoolean
5+
} = require('./utils')
6+
37
module.exports = {
48
command: 'mv <source> <dest>',
59

6-
describe: 'Move files around. Just like traditional unix mv',
10+
describe: 'Move mfs files around',
711

812
builder: {
913
parents: {
1014
alias: 'p',
1115
type: 'boolean',
1216
default: false,
17+
coerce: asBoolean,
1318
describe: 'Create any non-existent intermediate directories'
1419
},
1520
recursive: {
1621
alias: 'r',
1722
type: 'boolean',
1823
default: false,
24+
coerce: asBoolean,
1925
describe: 'Remove directories recursively'
2026
}
2127
},

src/cli/read.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
'use strict'
2+
3+
const pull = require('pull-stream/pull')
4+
const through = require('pull-stream/throughs/through')
5+
const collect = require('pull-stream/sinks/collect')
6+
const waterfall = require('async/waterfall')
7+
const {
8+
print
9+
} = require('./utils')
10+
11+
module.exports = {
12+
command: 'read <path>',
13+
14+
describe: 'Read an mfs file',
15+
16+
builder: {
17+
offset: {
18+
alias: 'o',
19+
type: 'number',
20+
describe: 'Start writing at this offset'
21+
},
22+
length: {
23+
alias: 'l',
24+
type: 'number',
25+
describe: 'Write only this number of bytes'
26+
}
27+
},
28+
29+
handler (argv) {
30+
let {
31+
path,
32+
ipfs,
33+
offset,
34+
length
35+
} = argv
36+
37+
waterfall([
38+
(cb) => ipfs.mfs.readPullStream(path, {
39+
offset,
40+
length
41+
}, cb),
42+
(stream, cb) => {
43+
pull(
44+
stream,
45+
through(buffer => {
46+
print(buffer)
47+
}),
48+
collect(cb)
49+
)
50+
}
51+
], (error) => {
52+
if (error) {
53+
throw error
54+
}
55+
})
56+
}
57+
}

src/cli/rm.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
'use strict'
22

3+
const {
4+
asBoolean
5+
} = require('./utils')
6+
37
module.exports = {
48
command: 'rm <path>',
59

6-
describe: 'Remove a file or directory',
10+
describe: 'Remove an mfs file or directory',
711

812
builder: {
913
recursive: {
1014
alias: 'r',
1115
type: 'boolean',
1216
default: false,
17+
coerce: asBoolean,
1318
describe: 'Remove directories recursively'
1419
}
1520
},

src/cli/utils.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,20 @@ const print = (msg = '', newline = true) => {
1515
process.stdout.write(msg)
1616
}
1717

18+
const asBoolean = (value) => {
19+
if (value === false || value === true) {
20+
return value
21+
}
22+
23+
if (value === undefined) {
24+
return true
25+
}
26+
27+
return false
28+
}
29+
1830
module.exports = {
1931
disablePrinting,
20-
print
32+
print,
33+
asBoolean
2134
}

0 commit comments

Comments
 (0)