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

Commit ba2b561

Browse files
committed
feat: cli ls (#927)
License: MIT Signed-off-by: Rasmus Erik Voel Jensen <[email protected]>
1 parent 07228a0 commit ba2b561

File tree

4 files changed

+108
-1
lines changed

4 files changed

+108
-1
lines changed

src/cli/commands/ls.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
'use strict'
2+
3+
const {print, rightpad} = require('../utils')
4+
const Unixfs = require('ipfs-unixfs')
5+
6+
module.exports = {
7+
command: 'ls',
8+
9+
describe: '',
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+
if (argv._.length !== 2) {
27+
throw new Error('Other arguments than single hash not implemented yet.')
28+
}
29+
30+
argv.ipfs.object.get(argv._[1], {enc: 'base58'}, (err, node) => {
31+
if (err) {
32+
throw err
33+
}
34+
let {data, links} = node.toJSON()
35+
36+
const fileDesc = Unixfs.unmarshal(data)
37+
if (fileDesc.type !== 'directory') {
38+
throw new Error('merkeldag node was not a directory') // TODO: support shards
39+
}
40+
41+
if (argv['resolve-type']) {
42+
throw new Error('--resolve-type not implemented yet')
43+
}
44+
45+
if (argv.headers) {
46+
links = [{multihash: 'Hash', size: 'Size', name: 'Name'}].concat(links)
47+
}
48+
49+
const multihashWidth = Math.max.apply(null, links.map((file) => String(file.multihash).length))
50+
const sizeWidth = Math.max.apply(null, links.map((file) => String(file.size).length))
51+
52+
links.forEach((file) => {
53+
print(rightpad(file.multihash, multihashWidth + 1) +
54+
rightpad(file.size, sizeWidth + 1) +
55+
file.name)
56+
})
57+
})
58+
}
59+
}

src/cli/utils.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,11 @@ exports.print = (msg, newline) => {
8585
process.stdout.write(msg)
8686
}
8787
}
88+
89+
exports.rightpad = (val, n) => {
90+
let result = String(val)
91+
for (let i = result.length; i < n; ++i) {
92+
result += ' '
93+
}
94+
return result
95+
}

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
@@ -150,6 +150,46 @@ describe('files', () => runOnAndOff((thing) => {
150150
})
151151
})
152152

153+
it('ls', () => {
154+
return ipfs('ls QmYmW4HiZhotsoSqnv2o1oUusvkRM8b9RweBoH7ao5nki2')
155+
.then((out) => {
156+
expect(out).to.eql(
157+
'QmQQHYDwAQms78fPcvx1uFFsfho23YJNoewfLbi9AtdyJ9 123530 blocks\n' +
158+
'QmPkWYfSLCEBLZu7BZt4kigGDMe3cpogMbeVf97gN2xJDN 3939 config\n' +
159+
'Qma13ZrhKG52MWnwtZ6fMD8jGj8d4Q9sJgn5xtKgeZw5uz 5503 datastore\n' +
160+
'QmUhUuiTKkkK8J6JZ9zmj8iNHPuNfGYcszgRumzhHBxEEU 7397 init-docs\n' +
161+
'QmR56UJmAaZLXLdTT1ALrE9vVqV8soUEekm9BMd4FnuYqV 10 version\n')
162+
})
163+
})
164+
165+
it('ls -v', () => {
166+
return ipfs('ls QmYmW4HiZhotsoSqnv2o1oUusvkRM8b9RweBoH7ao5nki2 -v')
167+
.then((out) => {
168+
expect(out).to.eql(
169+
'Hash Size Name\n' +
170+
'QmQQHYDwAQms78fPcvx1uFFsfho23YJNoewfLbi9AtdyJ9 123530 blocks\n' +
171+
'QmPkWYfSLCEBLZu7BZt4kigGDMe3cpogMbeVf97gN2xJDN 3939 config\n' +
172+
'Qma13ZrhKG52MWnwtZ6fMD8jGj8d4Q9sJgn5xtKgeZw5uz 5503 datastore\n' +
173+
'QmUhUuiTKkkK8J6JZ9zmj8iNHPuNfGYcszgRumzhHBxEEU 7397 init-docs\n' +
174+
'QmR56UJmAaZLXLdTT1ALrE9vVqV8soUEekm9BMd4FnuYqV 10 version\n')
175+
})
176+
})
177+
178+
it('ls --help', () => {
179+
return ipfs('ls --help')
180+
.then((out) => {
181+
expect(out.split('\n').slice(1)).to.eql(['',
182+
'Options:',
183+
' -q, --quiet suppress output [boolean]',
184+
' --help Show help [boolean]',
185+
' -v, --headers Print table headers (Hash, Size, Name).',
186+
' [boolean] [default: false]',
187+
' --resolve-type Resolve linked objects to find out their types. (not',
188+
' implemented yet) [boolean] [default: false]',
189+
'', ''])
190+
})
191+
})
192+
153193
it('get', () => {
154194
return ipfs('files get QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB')
155195
.then((out) => {

0 commit comments

Comments
 (0)