-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcommand-line-control.js
79 lines (74 loc) · 2.13 KB
/
command-line-control.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
const SVGatorBackend = require("./svgator-backend/index.js");
let auth_code = '';
let access_token = '';
let customer_id = '';
let project_id = '';
let domain = 'http://localhost:8080';
let action;
let app_id = '';
let secret_key = '';
for(let i = 0; i < process.argv.length; i++) {
let arg = process.argv[i];
if (arg.toString().match(/^ac_/)) {
auth_code = arg.toString();
}
if (arg.toString().match(/^at_/)) {
access_token = arg.toString();
}
if (arg.toString().match(/^ai_/)) {
app_id = arg.toString();
}
if (arg.toString().match(/^sk_/)) {
secret_key = arg.toString();
}
if (arg.toString().match(/^ci_/)) {
customer_id = arg.toString();
}
if (arg.toString().match(/^pi_/)) {
project_id = arg.toString();
}
if (arg.toString().match(/app\.svgator\.(?:com)/)) {
domain = 'https://' + arg.toString();
}
if (arg.toString().match(/--action\=/)) {
action = arg.toString().replace(/--action=/, '');
}
}
let svgator = new SVGatorBackend({
app_id: app_id,
secret_key: secret_key,
endpoint: domain + '/api/app-auth',
});
/**
* @param {Promise.<object>} promise
* @param {boolean} returnRaw
*/
function handlePromise(promise, returnRaw) {
promise
.then(function(response) {
console.log(returnRaw ? response : JSON.stringify(response));
})
.catch(function(err) {
let error = err.message ? err.message : err.toString();
console.log(JSON.stringify({error}));
})
.finally(function() {
process.exit();
});
};
switch(action) {
case 'get-token':
handlePromise(svgator.token.get(auth_code));
break;
case 'get-projects':
handlePromise(svgator.projects.getAll(access_token, customer_id, 1000, 0));
break;
case 'get-project':
handlePromise(svgator.projects.get(access_token, project_id));
break;
case 'export':
handlePromise(svgator.projects.export(access_token, project_id), true);
break;
default:
console.log(JSON.stringify({error: "Wrong action"}));
}