11'use strict'
22
3- const utils = require ( '../utils' )
3+ const multibase = require ( 'multibase' )
4+ const { print, rightpad } = require ( '../utils' )
45
56module . exports = {
67 command : 'ls <key>' ,
@@ -24,16 +25,16 @@ module.exports = {
2425 desc : 'Resolve linked objects to find out their types. (not implemented yet)' ,
2526 type : 'boolean' ,
2627 default : false // should be true when implemented
28+ } ,
29+ 'cid-base' : {
30+ describe : 'Number base to display CIDs in.' ,
31+ type : 'string' ,
32+ choices : multibase . names
2733 }
2834 } ,
2935
3036 handler ( argv ) {
31- let path = argv . key
32- if ( path . startsWith ( '/ipfs/' ) ) {
33- path = path . replace ( '/ipfs/' , '' )
34- }
35-
36- argv . ipfs . ls ( path , { recursive : argv . recursive } , ( err , links ) => {
37+ argv . ipfs . ls ( argv . key , { recursive : argv . recursive , cidBase : argv . cidBase } , ( err , links ) => {
3738 if ( err ) {
3839 throw err
3940 }
@@ -45,12 +46,18 @@ module.exports = {
4546 const multihashWidth = Math . max . apply ( null , links . map ( ( file ) => file . hash . length ) )
4647 const sizeWidth = Math . max . apply ( null , links . map ( ( file ) => String ( file . size ) . length ) )
4748
49+ let pathParts = argv . key . split ( '/' )
50+
51+ if ( argv . key . startsWith ( '/ipfs/' ) ) {
52+ pathParts = pathParts . slice ( 2 )
53+ }
54+
4855 links . forEach ( link => {
4956 const fileName = link . type === 'dir' ? `${ link . name || '' } /` : link . name
50- const padding = link . depth - path . split ( '/' ) . length
51- utils . print (
52- utils . rightpad ( link . hash , multihashWidth + 1 ) +
53- utils . rightpad ( link . size || '' , sizeWidth + 1 ) +
57+ const padding = link . depth - pathParts . length
58+ print (
59+ rightpad ( link . hash , multihashWidth + 1 ) +
60+ rightpad ( link . size || '' , sizeWidth + 1 ) +
5461 ' ' . repeat ( padding ) + fileName
5562 )
5663 } )
0 commit comments