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

Commit

Permalink
feat: cli ls (#927)
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Rasmus Erik Voel Jensen <github-rasmuserik@solsort.dk>
  • Loading branch information
rasmuserik authored and pgte committed Nov 12, 2017
1 parent 03610b5 commit 5fc029d
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 1 deletion.
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 {print, rightpad} = 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) => {
print(rightpad(file.multihash, multihashWidth + 1) +
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 @@ -100,3 +100,11 @@ exports.createProgressBar = (totalBytes) => {
total: totalBytes
})
}

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 @@ -254,6 +254,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

0 comments on commit 5fc029d

Please sign in to comment.