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
/
index.js
63 lines (52 loc) · 1.49 KB
/
index.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
const {Command} = require('@oclif/command');
const DigitalOcean = require('do-wrapper').default;
const Conf = require('conf');
const Ora = require('ora');
const config = new Conf({projectName: 'shark'});
class BaseCommand extends Command {
async init() {
let API_TOKEN = '';
if (config.has('do_access_token')) {
API_TOKEN = config.get('do_access_token');
} else {
const {askToken} = require('../prompts');
const {token} = await askToken();
await this.validateToken(token);
this.setToken('do_access_token', token);
API_TOKEN = token;
}
this.api = new DigitalOcean(API_TOKEN);
this.spinner = new Ora();
}
async validateToken(token) {
try {
const api = new DigitalOcean(token);
const {
body: {account}
} = await api.account();
if (account.status === 'active') {
return true;
}
return false;
} catch (error) {
console.error(error.message);
process.exit(1);
}
}
deleteToken(key) {
config.delete(key);
}
setToken(key, value) {
config.set(key, value);
}
// This peice of code is inspired by https://github.com/heroku/heroku-cli-util/blob/master/lib/styled.js
styledJSON(data) {
if (process.stdout.isTTY) {
const cardinal = require('cardinal');
const theme = require('cardinal/themes/jq');
return cardinal.highlight(JSON.stringify(data, null, 2), {theme});
}
return JSON.stringify(data, null, 2);
}
}
module.exports = BaseCommand;