Skip to content

Commit

Permalink
feat: add list command (#10)
Browse files Browse the repository at this point in the history
List out the root CIDs in the current space

```sh
w3 ls
bafybeib5bn5onczh4n2qb25wqjvyemj6mwk6oshwiaibcef2testdboqra
bafybeif6uamfrkdcailubrfqs6vadz66pv25yxizvkw5rjpb3o6hhi5lzi
bafybeifb5ojeetrigasdrvsj7c7oys5dnvhf3rcgoagydcrswfdmihaulu
bafybeifbk33tijfe7hzduqqoistzwj4lunbc2mbczlgath67xgvh2lsxmm
```

List output as ndjson

```sh
w3 ls --json
{"root":"bafybeib5bn5onczh4n2qb25wqjvyemj6mwk6oshwiaibcef2testdboqra","shards":["bagbaierarihlji6ep47f42fv46atov4vh7kyhledn5shif5zwbpszsk3kltq","bagbaierawqhk2jn2qwzdm5ajdevwp2t5kari6d6qok7iu2jyfm6kndndemla","bagbaieraxwxthv7ntlucs2lpghxv7wsrvoyca4gtna2ny4tg7mlkquqef7aq","bagbaieraz2bfnvj6rooqpjny2j3xjeqp2otndee5oylxl6jmdll7ydxa5lwq"]}
{"root":"bafybeif6uamfrkdcailubrfqs6vadz66pv25yxizvkw5rjpb3o6hhi5lzi","shards":["bagbaierazhga7wit3k2etn22hqmhqg5lj3ddsjji4lvk7fafvqljgycjz5ia"]}
{"root":"bafybeifb5ojeetrigasdrvsj7c7oys5dnvhf3rcgoagydcrswfdmihaulu","shards":["bagbaieraytfm7hxlzzr4vmckazjiiwraozzrcj2bwqsipi5mtzgyxfc523lq"]}
{"root":"bafybeifbk33tijfe7hzduqqoistzwj4lunbc2mbczlgath67xgvh2lsxmm","shards":["bagbaierajc6su75q6sdau5fy3nywxlstd6efsoxr4vsmrajck5wcvvgeoqmq"]}
```

Pretty list output with shards

```sh
w3 ls --shards
bafybeib5bn5onczh4n2qb25wqjvyemj6mwk6oshwiaibcef2testdboqra
└─┬ shards
  ├── bagbaierarihlji6ep47f42fv46atov4vh7kyhledn5shif5zwbpszsk3kltq
  ├── bagbaierawqhk2jn2qwzdm5ajdevwp2t5kari6d6qok7iu2jyfm6kndndemla
  ├── bagbaieraxwxthv7ntlucs2lpghxv7wsrvoyca4gtna2ny4tg7mlkquqef7aq
  └── bagbaieraz2bfnvj6rooqpjny2j3xjeqp2otndee5oylxl6jmdll7ydxa5lwq

bafybeif6uamfrkdcailubrfqs6vadz66pv25yxizvkw5rjpb3o6hhi5lzi
└─┬ shards
  └── bagbaierazhga7wit3k2etn22hqmhqg5lj3ddsjji4lvk7fafvqljgycjz5ia

bafybeifb5ojeetrigasdrvsj7c7oys5dnvhf3rcgoagydcrswfdmihaulu
└─┬ shards
  └── bagbaieraytfm7hxlzzr4vmckazjiiwraozzrcj2bwqsipi5mtzgyxfc523lq

bafybeifbk33tijfe7hzduqqoistzwj4lunbc2mbczlgath67xgvh2lsxmm
└─┬ shards
  └── bagbaierajc6su75q6sdau5fy3nywxlstd6efsoxr4vsmrajck5wcvvgeoqmq
```

oooh nice.

License: MIT
Signed-off-by: Oli Evans <oli@protocol.ai>
  • Loading branch information
olizilla authored Dec 10, 2022
1 parent 8617b49 commit b6cb1f0
Show file tree
Hide file tree
Showing 5 changed files with 237 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ Upload file(s) to web3.storage. The IPFS Content ID (CID) for your files is calc
List all the uploads registered in the current space.

- `--json` Format as newline delimted JSON
- `--cid` Only print the root CID per upload
- `--shards` Pretty print with shards in output

### `w3 rm <root-cid>`

Expand Down
9 changes: 8 additions & 1 deletion bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import sade from 'sade'
import open from 'open'
import { getPkg, unwarnify } from './lib.js'
import { createSpace, registerSpace, createDelegation, upload } from './index.js'
import { createSpace, registerSpace, createDelegation, upload, list } from './index.js'

unwarnify()

Expand All @@ -24,6 +24,13 @@ cli.command('open <cid>')
.describe('Open CID on https://w3s.link')
.action(cid => open(`https://w3s.link/ipfs/${cid}`))

cli.command('ls')
.alias('list')
.describe('List uploads in the current space')
.option('--json', 'Format as newline delimted JSON')
.option('--shards', 'Pretty print with shards in output')
.action(list)

cli.command('space')
.describe('Create and mangage w3 spaces')

Expand Down
37 changes: 37 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fs from 'fs'
import ora from 'ora'
import tree from 'pretty-tree'
import { Readable } from 'stream'
import { create } from '@web3-storage/w3up-client'
import * as DID from '@ipld/dag-ucan/did'
Expand Down Expand Up @@ -35,6 +36,42 @@ export async function upload (firstPath, opts) {
console.log(`⁂ https://w3s.link/ipfs/${root}`)
}

/**
* Print out all the uploads in the current space
*/
export async function list (opts) {
const client = await create()
let count = 0
let res
do {
res = await client.capability.upload.list()
count += res.results.length
if (res.results.length) {
if (opts.json) {
console.log(res.results.map(({ root, shards }) => JSON.stringify({
root: root.toString(),
shards: shards.map(s => s.toString())
})).join('\n'))
} else if (opts.shards) {
console.log(res.results.map(({ root, shards }) => tree({
label: root.toString(),
nodes: [{
label: 'shards',
leaf: shards.map(s => s.toString())
}]
})).join('\n'))
} else {
console.log(res.results.map(({ root }) => root.toString()).join('\n'))
}
}
} while (res.cursor && res.results.length)

if (count === 0 && !opts.json) {
console.log('⁂ No uploads in space')
console.log('⁂ Try out `w3 up <path to files>` to upload some')
}
}

export async function createSpace (name) {
const client = await create()
const space = await client.createSpace(name)
Expand Down
190 changes: 190 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"files-from-path": "^0.2.6",
"open": "^8.4.0",
"ora": "^6.1.2",
"pretty-tree": "^1.0.0",
"sade": "^1.8.1"
}
}

0 comments on commit b6cb1f0

Please sign in to comment.