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

WIP: ls #940

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions src/cli/commands/ls.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
'use strict'

const utils = require('../utils')
const Unixfs = require('ipfs-unixfs')

module.exports = {
command: 'ls <key>',

describe: 'List files for the given directory',

builder: {
v: {
alias: 'headers',
desc: 'Print table headers (Hash, Size, Name).',
type: 'boolean',
default: false
},
'resolve-type': {
desc: 'Resolve linked objects to find out their types. (not implemented yet)',
type: 'boolean',
default: false // should be true when implemented
}
},

handler (argv) {
let path = argv.key
if (path.startsWith('/ipfs/')) {
path = path.replace('/ipfs/', '')
}

argv.ipfs.object.get(path, {enc: 'base58'}, (err, node) => {
if (err) {
throw err
}
let {data, links} = node.toJSON()

const fileDesc = Unixfs.unmarshal(data)
if (fileDesc.type !== 'directory') {
throw new Error('merkeldag node was not a directory') // TODO: support shards
}

if (argv['resolve-type']) {
throw new Error('--resolve-type not implemented yet')
}

if (argv.headers) {
links = [{multihash: 'Hash', size: 'Size', name: 'Name'}].concat(links)
}

const multihashWidth = Math.max.apply(null, links.map((file) => String(file.multihash).length))
const sizeWidth = Math.max.apply(null, links.map((file) => String(file.size).length))

links.forEach((file) => {
utils.print(utils.rightpad(file.multihash, multihashWidth + 1) +
utils.rightpad(file.size, sizeWidth + 1) +
file.name)
})
})
}
}
8 changes: 8 additions & 0 deletions src/cli/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,11 @@ exports.print = (msg, newline) => {
process.stdout.write(msg)
}
}

exports.rightpad = (val, n) => {
let result = String(val)
for (let i = result.length; i < n; ++i) {
result += ' '
}
return result
}
2 changes: 1 addition & 1 deletion test/cli/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
const expect = require('chai').expect
const runOnAndOff = require('../utils/on-and-off')

const commandCount = 56
const commandCount = 57

describe('commands', () => runOnAndOff((thing) => {
let ipfs
Expand Down
40 changes: 40 additions & 0 deletions test/cli/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,46 @@ describe('files', () => runOnAndOff((thing) => {
})
})

it('ls', () => {
return ipfs('ls QmYmW4HiZhotsoSqnv2o1oUusvkRM8b9RweBoH7ao5nki2')
.then((out) => {
expect(out).to.eql(
'QmQQHYDwAQms78fPcvx1uFFsfho23YJNoewfLbi9AtdyJ9 123530 blocks\n' +
'QmPkWYfSLCEBLZu7BZt4kigGDMe3cpogMbeVf97gN2xJDN 3939 config\n' +
'Qma13ZrhKG52MWnwtZ6fMD8jGj8d4Q9sJgn5xtKgeZw5uz 5503 datastore\n' +
'QmUhUuiTKkkK8J6JZ9zmj8iNHPuNfGYcszgRumzhHBxEEU 7397 init-docs\n' +
'QmR56UJmAaZLXLdTT1ALrE9vVqV8soUEekm9BMd4FnuYqV 10 version\n')
})
})

it('ls -v', () => {
return ipfs('ls /ipfs/QmYmW4HiZhotsoSqnv2o1oUusvkRM8b9RweBoH7ao5nki2 -v')
.then((out) => {
expect(out).to.eql(
'Hash Size Name\n' +
'QmQQHYDwAQms78fPcvx1uFFsfho23YJNoewfLbi9AtdyJ9 123530 blocks\n' +
'QmPkWYfSLCEBLZu7BZt4kigGDMe3cpogMbeVf97gN2xJDN 3939 config\n' +
'Qma13ZrhKG52MWnwtZ6fMD8jGj8d4Q9sJgn5xtKgeZw5uz 5503 datastore\n' +
'QmUhUuiTKkkK8J6JZ9zmj8iNHPuNfGYcszgRumzhHBxEEU 7397 init-docs\n' +
'QmR56UJmAaZLXLdTT1ALrE9vVqV8soUEekm9BMd4FnuYqV 10 version\n')
})
})

it('ls --help', () => {
return ipfs('ls --help')
.then((out) => {
expect(out.split('\n').slice(1)).to.eql(['',
'Options:',
' -q, --quiet suppress output [boolean]',
' --help Show help [boolean]',
' -v, --headers Print table headers (Hash, Size, Name).',
' [boolean] [default: false]',
' --resolve-type Resolve linked objects to find out their types. (not',
' implemented yet) [boolean] [default: false]',
'', ''])
})
})

it('get', () => {
return ipfs('files get QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB')
.then((out) => {
Expand Down