-
Notifications
You must be signed in to change notification settings - Fork 8
/
utils.js
80 lines (66 loc) · 2.35 KB
/
utils.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
var chalk = require('chalk')
var rainbow = require('ansi-rainbow')
var stores = {
mem : require('./stores/mem')
}
module.exports = {
displayVersionMaybe : function (argv) {
if (!argv.v && !argv.version) return
var pkg = require('./package.json')
console.log(rainbow.r(pkg.name)+chalk.red(' ❤ ')+chalk.green('v'+pkg.version))
process.exit(0)
},
displayHelpMaybe : function (argv) {
if (!argv.h && !argv.help) return
var pkg = require('./package.json')
console.log(chalk.cyan('help coming soon!')+chalk.red(' ❤ '))
process.exit(0)
},
selectStore : function (argv) {
if (stores[argv.store] == undefined) { console.log(chalk.red('ERROR ')+'no such datastore '+argv.store); process.exit(1) }
return stores[argv.store]()
},
displayStartMessage : function (argv) {
// console.log(rainbow.r('nameserver ')+chalk.bgBlue(argv.nameserver))
this.displayServiceStatus('domain', argv.domain)
// this.displayServiceStatus('store', argv.store)
// console.log(rainbow.r('ttl ')+chalk.bgBlue(argv.ttl))
},
displayServiceStatus : function (service, meta, check) {
console.log(rainbow.r(this.fillSpaces(service,6))+' '+this.fillSpaces(meta,21)+' '+(check ? chalk.green('✔') : ''))
},
displayErrorMessage : function (msg, err, props) {
console.log(chalk.red('ERROR: ')+msg, err)
if (props.hint) console.log(chalk.cyan('HINT: ')+props.hint)
if (props.exit) process.exit(1)
},
fillSpaces : function (word, len) {
while(word.length < len) {
word = word+' '
}
return word
},
wrapDefaults : function (name, payload, argv) {
name = name+'.'+(payload.domain || argv.domain)
if (!payload.domain) payload.domain = argv.domain
if (!payload.ttl) payload.ttl = argv.ttl
payload.time = this.getUnixTime()
return {
name : name,
payload : payload
}
},
getUnixTime : function () {
return Math.floor(new Date().getTime() / 1000)
},
reverse_map : function(src) {
var dst = {},
k;
for (k in src) {
if (src.hasOwnProperty(k)) {
dst[src[k]] = k;
}
}
return dst;
}
}