-
Notifications
You must be signed in to change notification settings - Fork 0
/
act-library
executable file
·77 lines (69 loc) · 1.86 KB
/
act-library
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
#!/usr/bin/env node
/* eslint no-shadow: [ "error", { "allow": ["_", "program", "argv"] }] */
const debug = require('debug')('ara-contracts:cli:library')
const { createProgram } = require('../lib/program')
const { info, error } = require('ara-console')
const library = require('../library')
// yargs program command line interface instance
const program = createProgram({
usage: `
usage: act library: [-h] [--help]
<did> [options]
`
})
const { argv } = program
.command('$0 <did>', 'Gets a list of content DIDs purchased by <did>', (_) => {
_
.positional('did', {
type: 'string',
describe: 'The library owner\'s DID'
})
}, onall)
.command('get <did> <index>', 'Gets the content DID at <index> in <did>\'s library', (_) => {
_
.positional('did', {
type: 'string',
describe: 'The library owner\'s DID'
})
.positional('index', {
type: 'number',
describe: 'The position in the library'
})
}, onget)
.help('h')
.recommendCommands()
.strict()
// Main program entry
module.exports = (async function main() {
if (argv.debug) {
require('debug').enable('ara-contracts*')
}
}())
async function onall(argv) {
const { did } = argv
try {
info(`Getting ${did}'s library...`)
const size = await library.getLibrarySize(did)
const lib = await library.getLibrary(did)
info(`${did} library (${size})\n${lib}.`)
} catch (err) {
onfatal(err)
}
}
async function onget(argv) {
const { did, index } = argv
try {
info(`Getting item ${index} in ${did}'s library...`)
const contentDid = await library.getLibraryItem({ requesterDid: did, index })
info(`item ${index} has did: ${contentDid}.`)
} catch (err) {
onfatal(err)
}
}
function onfatal(err) {
if (err) {
debug(err)
error('fatal:', err.message)
}
process.exit(1)
}