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

Commit f0c5aa0

Browse files
rasmuserikpgte
authored andcommitted
feat: cli ls (#927)
License: MIT Signed-off-by: Rasmus Erik Voel Jensen <[email protected]>
1 parent bb715f9 commit f0c5aa0

File tree

4 files changed

+109
-1
lines changed

4 files changed

+109
-1
lines changed

src/cli/commands/ls.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
'use strict'
2+
3+
const {print, rightpad} = require('../utils')
4+
const Unixfs = require('ipfs-unixfs')
5+
6+
module.exports = {
7+
command: 'ls <key>',
8+
9+
describe: 'List files for the given directory',
10+
11+
builder: {
12+
v: {
13+
alias: 'headers',
14+
desc: 'Print table headers (Hash, Size, Name).',
15+
type: 'boolean',
16+
default: false
17+
},
18+
'resolve-type': {
19+
desc: 'Resolve linked objects to find out their types. (not implemented yet)',
20+
type: 'boolean',
21+
default: false // should be true when implemented
22+
}
23+
},
24+
25+
handler (argv) {
26+
let path = argv.key
27+
if (path.startsWith('/ipfs/')) {
28+
path = path.replace('/ipfs/', '')
29+
}
30+
31+
argv.ipfs.object.get(path, {enc: 'base58'}, (err, node) => {
32+
if (err) {
33+
throw err
34+
}
35+
let {data, links} = node.toJSON()
36+
37+
const fileDesc = Unixfs.unmarshal(data)
38+
if (fileDesc.type !== 'directory') {
39+
throw new Error('merkeldag node was not a directory') // TODO: support shards
40+
}
41+
42+
if (argv['resolve-type']) {
43+
throw new Error('--resolve-type not implemented yet')
44+
}
45+
46+
if (argv.headers) {
47+
links = [{multihash: 'Hash', size: 'Size', name: 'Name'}].concat(links)
48+
}
49+
50+
const multihashWidth = Math.max.apply(null, links.map((file) => String(file.multihash).length))
51+
const sizeWidth = Math.max.apply(null, links.map((file) => String(file.size).length))
52+
53+
links.forEach((file) => {
54+
print(rightpad(file.multihash, multihashWidth + 1) +
55+
rightpad(file.size, sizeWidth + 1) +
56+
file.name)
57+
})
58+
})
59+
}
60+
}

src/cli/utils.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,11 @@ exports.createProgressBar = (totalBytes) => {
100100
total: totalBytes
101101
})
102102
}
103+
104+
exports.rightpad = (val, n) => {
105+
let result = String(val)
106+
for (let i = result.length; i < n; ++i) {
107+
result += ' '
108+
}
109+
return result
110+
}

test/cli/commands.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
const expect = require('chai').expect
55
const runOnAndOff = require('../utils/on-and-off')
66

7-
const commandCount = 56
7+
const commandCount = 57
88

99
describe('commands', () => runOnAndOff((thing) => {
1010
let ipfs

test/cli/files.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,46 @@ describe('files', () => runOnAndOff((thing) => {
254254
})
255255
})
256256

257+
it('ls', () => {
258+
return ipfs('ls QmYmW4HiZhotsoSqnv2o1oUusvkRM8b9RweBoH7ao5nki2')
259+
.then((out) => {
260+
expect(out).to.eql(
261+
'QmQQHYDwAQms78fPcvx1uFFsfho23YJNoewfLbi9AtdyJ9 123530 blocks\n' +
262+
'QmPkWYfSLCEBLZu7BZt4kigGDMe3cpogMbeVf97gN2xJDN 3939 config\n' +
263+
'Qma13ZrhKG52MWnwtZ6fMD8jGj8d4Q9sJgn5xtKgeZw5uz 5503 datastore\n' +
264+
'QmUhUuiTKkkK8J6JZ9zmj8iNHPuNfGYcszgRumzhHBxEEU 7397 init-docs\n' +
265+
'QmR56UJmAaZLXLdTT1ALrE9vVqV8soUEekm9BMd4FnuYqV 10 version\n')
266+
})
267+
})
268+
269+
it('ls -v', () => {
270+
return ipfs('ls /ipfs/QmYmW4HiZhotsoSqnv2o1oUusvkRM8b9RweBoH7ao5nki2 -v')
271+
.then((out) => {
272+
expect(out).to.eql(
273+
'Hash Size Name\n' +
274+
'QmQQHYDwAQms78fPcvx1uFFsfho23YJNoewfLbi9AtdyJ9 123530 blocks\n' +
275+
'QmPkWYfSLCEBLZu7BZt4kigGDMe3cpogMbeVf97gN2xJDN 3939 config\n' +
276+
'Qma13ZrhKG52MWnwtZ6fMD8jGj8d4Q9sJgn5xtKgeZw5uz 5503 datastore\n' +
277+
'QmUhUuiTKkkK8J6JZ9zmj8iNHPuNfGYcszgRumzhHBxEEU 7397 init-docs\n' +
278+
'QmR56UJmAaZLXLdTT1ALrE9vVqV8soUEekm9BMd4FnuYqV 10 version\n')
279+
})
280+
})
281+
282+
it('ls --help', () => {
283+
return ipfs('ls --help')
284+
.then((out) => {
285+
expect(out.split('\n').slice(1)).to.eql(['',
286+
'Options:',
287+
' -q, --quiet suppress output [boolean]',
288+
' --help Show help [boolean]',
289+
' -v, --headers Print table headers (Hash, Size, Name).',
290+
' [boolean] [default: false]',
291+
' --resolve-type Resolve linked objects to find out their types. (not',
292+
' implemented yet) [boolean] [default: false]',
293+
'', ''])
294+
})
295+
})
296+
257297
it('get', () => {
258298
return ipfs('files get QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB')
259299
.then((out) => {

0 commit comments

Comments
 (0)