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

Commit 888122a

Browse files
committed
feat: cli ls (#927)
License: MIT Signed-off-by: Rasmus Erik Voel Jensen <github-rasmuserik@solsort.dk>
1 parent a85cf70 commit 888122a

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
@@ -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 /ipfs/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)