-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
145 lines (129 loc) · 3.7 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
#!/usr/bin/env node
const co = require("co");
const cla = require("command-line-args");
const clc = require("command-line-commands");
require("console.table");
const auth = require("./lib/auth");
const usage = require("./lib/usage");
const filter = require("./lib/filter");
const {command, argv} = clc([null, "help", "auth", "list", "add", "remove", "update", "nginx"]);
const opts = cla([
// auth
{name: "tenant", alias: "T", type: String},
{name: "user", alias: "U", type: String},
{name: "password", alias: "P", type: String},
// domain, record
{name: "name", alias: "n", type: String, defaultOption: true},
{name: "ttl", defaultValue: 3600, type: Number},
{name: "description", type: String},
// domain
{name: "email", alias: "e", defaultValue: "[email protected]", type: String},
{name: "gslb", type: Number},
// record
{name: "type", alias: "t", type: String},
{name: "data", alias: "d", type: String},
{name: "priority", alias: "p", type: Number},
{name: "gslb_region", type: String},
{name: "gslb_weight", type: Number},
{name: "gslb_check", type: Number},
// batch
{name: "file", alias: "f", type: String},
{name: "directory", alias: "D", type: String},
{name: "A", type: String},
{name: "AAAA", type: String},
// misc
{name: "domain", type: String},
{name: "record", type: String},
{name: "verbose", alias: "v", type: Boolean},
], argv);
const show = data => {
const keys = Object.keys(data);
if(keys.length == 1){
data = data[keys[0]];
}else{
data = [data];
}
if("verbose" in opts){
console.log(data);
}else if(data.length){
console.table(data.map(filter["domain_id" in data[0] ? "recRes" : "domRes"]));
}else{
console.log("No data");
}
};
const necessary = keys => {
for(const key of keys){
if(!Object.keys(opts).some(item => item == key)){
throw new Error(`No ${key} specified.`);
}
}
};
const fixdata = _ => {
if(opts.type == "MX" || opts.type == "CNAME"){
opts.data = opts.data.replace(/[^.]$/, "$&.");
}
if(opts.type == "MX" && !opts.priority){
opts.priority = 10;
}
};
co(function*(){
if(!command || command == "help"){
return console.log(usage);
}
if(command == "auth"){
yield auth.authorize(opts);
return console.log("OK");
}
if(command == "nginx"){
necessary(["file"]);
const client = yield auth.getBatchClient(opts);
return yield client.nginx(opts);
}
const client = yield auth.getClient(opts);
if(opts.name){
if(!opts.domain){
opts.domain = yield client.exportDomain(opts.name);
}
if(!opts.record){
opts.record = opts.domain == opts.name ? null : opts.name;
}
opts.name = opts.name.replace(/[^.]$/, "$&.");
}
if(opts.domain && /\./.test(opts.domain)){
opts.domain = yield client.findDomainID(opts.domain.replace(/[^.]$/, "$&."));
}
if(opts.domain && opts.record && /\./.test(opts.record)){
opts.record = yield client.findRecordID(opts.domain, opts.record.replace(/[^.]$/, "$&."));
}
if(opts.domain && opts.record){
if(command == "update"){
necessary(["type", "data"]);
fixdata();
show(yield client.updateRecord(opts));
}else if(command == "remove"){
yield client.removeRecord(opts);
console.log("OK");
}
}else if(opts.domain){
if(command == "list"){
show(yield client.listRecord(opts));
}else if(command == "add"){
necessary(["type", "data"]);
fixdata();
show(yield client.addRecord(opts));
}else if(command == "update"){
necessary(["name"]);
show(yield client.updateDomain(opts));
}else if(command == "remove"){
yield client.removeDomain(opts);
console.log("OK");
}
}else{
if(command == "list"){
show(yield client.listDomain(opts));
}else if(command == "add"){
necessary(["name"]);
show(yield client.addDomain(opts));
}
}
}).catch(e => console.error(e));