This repository has been archived by the owner on Jun 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cli.js
executable file
·88 lines (80 loc) · 2.78 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/env node
'use strict'
const parseSchema = require('./src/parse.js')
const fs = require('fs')
const printify = require('@ipld/printify')
const { inspect } = require('util')
const api = require('./src/fs')
const reader = require('./src/reader')
const createStorage = require('./src/local-storage')
/* eslint-disable no-console */
const parse = async argv => {
const s = await parseSchema(argv.input, argv.output)
if (!argv.output) console.log(inspect(s, { depth: Infinity }))
else fs.writeFileSync(argv.output, JSON.stringify(s))
}
const runImport = async argv => {
const iter = await api.fromFileSystem(argv.input)
let store
if (argv.storage) store = createStorage(argv.storage)
for await (let { block, root } of iter) {
if (root) block = root.block()
if (store) {
await store.put(block)
if (root) console.log('Root:', (await root.block().cid()).toString())
} else {
if (block.codec === 'raw') {
console.log('Block<raw>', (await block.cid()).toString())
} else {
console.log('Block<' + block.codec + '>', printify(block.decode()))
}
}
}
}
const createReader = argv => {
const store = createStorage(argv.storage)
const _reader = reader(argv.rootCID, store.get)
return _reader
}
const runRead = async argv => {
const reader = createReader(argv)
for await (const buffer of reader.read(argv.path, argv.start, argv.end)) {
process.stdout.write(buffer)
}
}
const runLs = async argv => {
const reader = createReader(argv)
const files = await reader.ls(argv.path)
files.forEach(f => console.log(f))
}
const storageOptions = yargs => {
yargs.option('storage', { desc: 'Directory to store blocks' })
}
const importOptions = yargs => {
yargs.positional('input', { desc: 'File or directory to import' })
storageOptions(yargs)
}
const readerOptions = yargs => {
yargs.positional('storage', { desc: 'Directory of stored blocks' })
yargs.positional('rootCID', { desc: 'CID of root node for file or directory' })
}
const readOptions = yargs => {
readerOptions(yargs)
yargs.positional('path', { desc: 'Path to filename' })
yargs.option('start', { desc: 'starting position, defaults to 0' })
yargs.option('end', { desc: 'ending position, defaults to end of file' })
}
const lsOptions = yargs => {
readerOptions(yargs)
yargs.positional('path', { desc: 'Path to directory' })
}
const yargs = require('yargs')
const args = yargs
.command('parse <input> [output]', 'Parse schema file', importOptions, parse)
.command('import <input>', 'Import file or directory', storageOptions, runImport)
.command('ls <storage> <rootCID> <path>', 'List directory contents', lsOptions, runLs)
.command('read <storage> <rootCID> <path>', 'Read file contents', readOptions, runRead)
.argv
if (!args._.length) {
yargs.showHelp()
}