/
cli.js
231 lines (219 loc) · 7.1 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#!/usr/bin/env node
/* Copyright 2017 Tierion
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const utils = require('./lib/utils')
const _ = require('lodash')
const getStdin = require('get-stdin')
const yargs = require('yargs')
async function parseBaseUriAsync(baseUri) {
// if the value supplied in --server or in chainpoint-cli.config is invalid, exit
if (!utils.isValidUrl(baseUri)) {
console.error(`Invalid server - ${baseUri}`)
process.exit(1)
}
// if no value was specified in --server or in chainpoint-cli.config, let the chainpint-client select node(s)
// http://0.0.0.0 is the env default, and represents a null setting
if (baseUri === 'http://0.0.0.0') return null
// otherwise, return the valid value supplied with --server or in chainpoint-cli.config as a one element array
return baseUri
}
async function startAsync() {
// load environment variables and commands
const env = require('./lib/parse-env.js')
const submitCmd = require('./lib/submit.js')
const updateCmd = require('./lib/update.js')
const verifyCmd = require('./lib/verify.js')
const evaluateCmd = require('./lib/evaluate.js')
const exportCmd = require('./lib/export.js')
const listCmd = require('./lib/list.js')
const showCmd = require('./lib/show.js')
const deleteCmd = require('./lib/delete.js')
const versionCmd = require('./lib/version.js')
// remove old cli.config file if it exists
utils.deleteOldConfig()
async function processArgsAsync() {
let input
try {
let inputItems = []
input = await getStdin()
if (input) {
inputItems = input.trim().split(' ')
yargs.parse(inputItems)
}
} catch (error) {
yargs.showHelp()
console.error(`Error reading from stdin: ${input}`)
}
let argv = yargs
.usage(
'Usage: ' +
require.main.filename
.split('/')
.pop()
.slice(0, -3) +
' <command> [options] <argument>'
)
.option('s', {
alias: 'server',
requiresArg: true,
default: env.CHAINPOINT_NODE_API_BASE_URI,
description: 'specify server to use',
type: 'string'
})
.option('q', {
alias: 'quiet',
demandOption: false,
requiresArg: false,
description: 'suppress all non-error output',
type: 'boolean'
})
.option('j', {
alias: 'json',
demandOption: false,
requiresArg: false,
description: 'format all output as json',
type: 'boolean'
})
.command(
'submit',
'submit a hash to be anchored (3x Nodes default)',
async yargs => {
let argv = yargs
.usage(
'Usage: submit [options] (<hash> <hash>... | <hash>,<hash>,... )'
)
.string('_').argv
argv.server = await parseBaseUriAsync(argv.server)
submitCmd.executeAsync(yargs, argv)
}
)
.command(
'update',
'retrieve an updated proof for your hash(es), if available',
async yargs => {
let argv = yargs
.usage('Usage: update [options] <hash_id_node>')
.option('a', {
alias: 'all',
demandOption: false,
requiresArg: false,
description: 'process all items in local database',
type: 'boolean'
}).argv
updateCmd.executeAsync(yargs, argv)
}
)
.command('verify', "verify a proof's anchor claims", async yargs => {
let argv = yargs
.usage('Usage: verify [options] <hash_id_node>')
.option('a', {
alias: 'all',
demandOption: false,
requiresArg: false,
description: 'process all items in local database',
type: 'boolean'
}).argv
verifyCmd.executeAsync(yargs, argv)
})
.command(
'evaluate',
"evaluate and display a proof's anchor claims",
async yargs => {
let argv = yargs
.usage('Usage: evaluate [options] <hash_id_node>')
.option('a', {
alias: 'all',
demandOption: false,
requiresArg: false,
description: 'process all items in local database',
type: 'boolean'
})
.option('b', {
alias: 'btc',
demandOption: false,
requiresArg: false,
description: 'get relevant bitcoin anchor tx information',
type: 'boolean'
}).argv
evaluateCmd.executeAsync(yargs, argv)
}
)
.command('export', 'export a proof', async yargs => {
let argv = yargs
.usage('Usage: export [options] <hash_id_node>')
.option('b', {
alias: 'binary',
demandOption: false,
requiresArg: false,
description: 'use binary format',
type: 'boolean'
}).argv
exportCmd.executeAsync(yargs, argv)
})
.command(
'list',
'display the status of every hash in the local database',
yargs => {
let argv = yargs.usage('Usage: list').argv
listCmd.executeAsync(yargs, argv)
}
)
.command('show', 'show the proof for a hash_id_node', yargs => {
let argv = yargs.usage('Usage: show [hash_id_node]').argv
showCmd.executeAsync(yargs, argv)
})
.command('delete', 'delete a hash from the local database', yargs => {
let argv = yargs.usage('Usage: delete <hash_id_node>').argv
deleteCmd.executeAsync(yargs, argv)
})
.command('version', 'show the CLI version', yargs => {
let argv = yargs.usage('Usage: version').argv
versionCmd.executeAsync(yargs, argv)
})
.demandCommand(1, 'You must specify a command.')
.help('help', 'show help').argv
// parse cli command and display error message on bad or missing command
parseCommand(yargs, argv)
}
function parseCommand(yargs, argv) {
if (argv._.length < 1) {
yargs.showHelp()
} else {
// check for unknown command
let command = _.lowerCase(argv._[0])
if (
_.indexOf(
[
'submit',
'update',
'verify',
'evaluate',
'export',
'list',
'show',
'delete',
'version'
],
command
) < 0
) {
yargs.showHelp()
console.error(`Unknown command: ${command}`)
}
}
}
// parse and process the command
processArgsAsync()
}
module.exports = {
startAsync: startAsync
}