forked from timneutkens/openprovider-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.js
62 lines (49 loc) · 1.48 KB
/
run.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
require('dotenv').config()
const chalk = require('chalk')
const log = console.log
// Require client
const Openprovider = require('./index')
const config = {
username: process.env.OP_USERNAME,
// password or hash. If you fill both hash is used.
password: process.env.OP_PASSWORD,
hash: process.env.OP_HASH,
cli: true
}
// Create new client using hash
const OpenproviderClient = new Openprovider(config)
// Example dns request using promises
const run = async (command, params) => {
log(chalk`Running {green ${command}} with params {green ${JSON.stringify(params)}}...`)
const result = await OpenproviderClient.request(command, params)
const { code, desc, data } = result.openXML.reply
if (code > 0) {
throw new Error(`${desc} (${code})`)
}
const {
id, status,
total, results
} = data
const deepResults = results && results.array && results.array.item
if (results != null && id) {
log(chalk`Result has {green ${total}} items and id {green ${id}} with status {green ${status}}`)
log(deepResults)
} else if (results != null) {
log(chalk`Result has {green ${total}} items`)
log(deepResults)
} else if (id) {
log(chalk`Result has id {green ${id}} with status {green ${status}}`)
} else {
log(data)
}
return {
id, status,
total, results: deepResults
}
}
const argv = require('minimist')(process.argv.slice(2))
const { _: [command], ...params } = argv
run(command, params)
.catch((error) => {
log(chalk.red(error))
})