From 553337c24f648e170080d7fb9b44f06094aab6cb Mon Sep 17 00:00:00 2001 From: Pedro Teixeira Date: Sun, 12 Nov 2017 11:05:08 +0000 Subject: [PATCH] files ls --- src/cli/bin.js | 3 +- src/cli/commands/files/ls.js | 91 ++++++++++++++++++++++++++++++++++++ src/cli/commands/ls.js | 60 ------------------------ src/cli/utils.js | 1 + src/core/components/files.js | 9 +++- 5 files changed, 102 insertions(+), 62 deletions(-) create mode 100644 src/cli/commands/files/ls.js delete mode 100644 src/cli/commands/ls.js diff --git a/src/cli/bin.js b/src/cli/bin.js index c9a72f2aa1..9e9c18fed9 100755 --- a/src/cli/bin.js +++ b/src/cli/bin.js @@ -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) }) diff --git a/src/cli/commands/files/ls.js b/src/cli/commands/files/ls.js new file mode 100644 index 0000000000..70810ecd3a --- /dev/null +++ b/src/cli/commands/files/ls.js @@ -0,0 +1,91 @@ +'use strict' + +const utils = require('../../utils') +const Unixfs = require('ipfs-unixfs') +const pull = require('pull-stream') + +module.exports = { + command: 'ls ', + + 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) + // }) + // }) + } +} diff --git a/src/cli/commands/ls.js b/src/cli/commands/ls.js deleted file mode 100644 index 705cd3a8a7..0000000000 --- a/src/cli/commands/ls.js +++ /dev/null @@ -1,60 +0,0 @@ -'use strict' - -const utils = require('../utils') -const Unixfs = require('ipfs-unixfs') - -module.exports = { - command: 'ls ', - - 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) - }) - }) - } -} diff --git a/src/cli/utils.js b/src/cli/utils.js index ff57085f91..d44bf4b4fd 100644 --- a/src/cli/utils.js +++ b/src/cli/utils.js @@ -99,6 +99,7 @@ exports.createProgressBar = (totalBytes) => { stream: process.stdout, total: totalBytes }) +} exports.rightpad = (val, n) => { let result = String(val) diff --git a/src/core/components/files.js b/src/core/components/files.js index e2f6443350..c1f7731ae8 100644 --- a/src/core/components/files.js +++ b/src/core/components/files.js @@ -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) => { @@ -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) })), + ) } }