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

Commit

Permalink
files ls
Browse files Browse the repository at this point in the history
  • Loading branch information
pgte committed Nov 12, 2017
1 parent d624ce0 commit 553337c
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 62 deletions.
3 changes: 2 additions & 1 deletion src/cli/bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ const cli = yargs
const addCmd = require('./commands/files/add')
const catCmd = require('./commands/files/cat')
const getCmd = require('./commands/files/get')
const aliases = [addCmd, catCmd, getCmd]
const lsCmd = require('./commands/files/ls')
const aliases = [addCmd, catCmd, getCmd, lsCmd]
aliases.forEach((alias) => {
cli.command(alias.command, alias.describe, alias.builder, alias.handler)
})
Expand Down
91 changes: 91 additions & 0 deletions src/cli/commands/files/ls.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
'use strict'

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

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/', '')
}

pull(
argv.ipfs.files.ls(path, 0),
pull.collect((err, links) => {
if (err) {
throw err
}

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

links = links.filter((link) => link.path !== path)
links.forEach((link) => {
if (link.type === 'dir') {
// directory: add trailing "/"
link.name = (link.name || '') + '/'
}
})
const multihashWidth = Math.max.apply(null, links.map((file) => file.hash.length))
const sizeWidth = Math.max.apply(null, links.map((file) => String(file.size).length))

links.forEach((file) => {
utils.print(utils.rightpad(file.hash, multihashWidth + 1) +
utils.rightpad(file.size || '', sizeWidth + 1) +
file.name)
})
})
)


// 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)
// })
// })
}
}
60 changes: 0 additions & 60 deletions src/cli/commands/ls.js

This file was deleted.

1 change: 1 addition & 0 deletions src/cli/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ exports.createProgressBar = (totalBytes) => {
stream: process.stdout,
total: totalBytes
})
}

exports.rightpad = (val, n) => {
let result = String(val)
Expand Down
9 changes: 8 additions & 1 deletion src/core/components/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const waterfall = require('async/waterfall')
const isStream = require('is-stream')
const Duplex = require('stream').Duplex
const CID = require('cids')
const toB58String = require('multihashes').toB58String

module.exports = function files (self) {
const createAddPullStream = (options) => {
Expand Down Expand Up @@ -118,7 +119,13 @@ module.exports = function files (self) {

getPull: promisify((ipfsPath, callback) => {
callback(null, exporter(ipfsPath, self._ipldResolver))
})
}),

ls: (ipfsPath) => pull(
exporter(ipfsPath, self._ipldResolver, { maxDepth: 1 }),
pull.filter((node) => node.depth === 1),
pull.map((node) => Object.assign({}, node, { hash: toB58String(node.hash) })),
)
}
}

Expand Down

0 comments on commit 553337c

Please sign in to comment.