This repository has been archived by the owner on Nov 3, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
util.js
66 lines (57 loc) · 2.02 KB
/
util.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
'use strict';
const DigitalOcean = require('do-wrapper').default;
const chalk = require('chalk');
const CacheConf = require('cache-conf');
const Ora = require('ora');
const config = new CacheConf();
const spinner = new Ora();
const ACCESS_TOKEN = config.get('do_api_access_token');
const DoAPI = new DigitalOcean(ACCESS_TOKEN, 50);
module.exports.config = config;
module.exports.spinner = spinner;
module.exports.DoAPI = DoAPI;
module.exports.initAccount = async () => {
const verifyAccount = async accesstoken => {
try {
const DoAPI = new DigitalOcean(accesstoken, 5);
spinner.start('Verifying your account...');
const {
body: { account }
} = await DoAPI.account();
if (account) {
spinner.succeed('Account verified!');
config.set('do_api_access_token', accesstoken);
console.log(`
${chalk.green('Hi! Your access token is valid.')}
Email: ${account.email}
Droplet Limit: ${account.droplet_limit}
Floating IP Limit: ${account.floating_ip_limit}
`);
}
} catch (error) {
spinner.fail('Verification failed!');
console.error(`
${chalk.red('Please make sure you are using a valid access token')}
`);
}
};
const Create = require('./prompts/create');
const answers = await Create.accessToken();
// If token is valid verifAccount sets the token
await verifyAccount(answers.do_api_access_token);
};
module.exports.callMatchingMethod = (object, method) => {
if (Object.prototype.hasOwnProperty.call(object, method)) {
object[method]();
} else if (Object.prototype.hasOwnProperty.call(object, 'init')) {
object.init();
} else {
console.error(`Couldn't find the method/property ${method} in ${object} `);
}
};
module.exports.calculateCostAndHours = (createdAt, hourlyPrice) => {
const createdDate = new Date(createdAt);
const totalHours = Math.ceil(Math.abs(Date.now() - createdDate) / 36e5);
const totalCost = totalHours * hourlyPrice;
return { totalCost, totalHours };
};