Skip to content

Commit b4f97eb

Browse files
committed
Add way to check for server and traefik updates, closes #62
1 parent 2a4dd02 commit b4f97eb

File tree

2 files changed

+86
-9
lines changed

2 files changed

+86
-9
lines changed

src/commands/update.js

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
const _ = require('lodash');
33
const got = require('got');
44
const chalk = require('chalk');
5+
const ora = require('ora');
56

67
// our packages
78
const {userConfig, isLoggedIn, logout} = require('../config');
@@ -10,18 +11,58 @@ const {userConfig, isLoggedIn, logout} = require('../config');
1011
const validTargets = ['traefik', 'server'];
1112

1213
exports.command = ['update [target]'];
13-
exports.describe = 'update given target';
14+
exports.describe = 'check for updates or update given target';
1415
exports.builder = {
1516
target: {
1617
alias: 't',
1718
description: `Target for update (${validTargets.join(', ')})`,
1819
},
1920
};
20-
exports.handler = async ({target} = {target: 'self'}) => {
21+
exports.handler = async ({target}) => {
2122
if (!isLoggedIn()) {
2223
return;
2324
}
2425

26+
// construct shared request params
27+
const options = {
28+
headers: {
29+
Authorization: `Bearer ${userConfig.token}`,
30+
},
31+
json: true,
32+
};
33+
34+
// if no target given - check for update
35+
if (!target || !target.length) {
36+
// show loader
37+
const spinner = ora('Checking for update...').start();
38+
39+
// services request url
40+
const remoteUrl = `${userConfig.endpoint}/version`;
41+
// send request
42+
const {body, statusCode} = await got.get(remoteUrl, options);
43+
if (statusCode !== 200 || body.error) {
44+
spinner.fail('Error checking for update');
45+
console.log(body.error || 'Oops. Something went wrong! Try again please.');
46+
return;
47+
}
48+
49+
if (body.serverUpdate || body.traefikUpdate) {
50+
spinner.warn('Updates available!');
51+
} else {
52+
spinner.succeed('You are up to date!');
53+
}
54+
55+
console.log();
56+
console.log(chalk.bold('Exoframe Server:'));
57+
console.log(` current: ${body.server}`);
58+
console.log(` latest: ${body.latestServer}`);
59+
console.log();
60+
console.log(chalk.bold('Traefik:'));
61+
console.log(` current: ${body.traefik}`);
62+
console.log(` latest: ${body.latestTraefik}`);
63+
return;
64+
}
65+
2566
if (!validTargets.includes(target)) {
2667
console.log(
2768
chalk.red('Error!'),
@@ -35,13 +76,6 @@ exports.handler = async ({target} = {target: 'self'}) => {
3576

3677
// services request url
3778
const remoteUrl = `${userConfig.endpoint}/update/${target}`;
38-
// construct shared request params
39-
const options = {
40-
headers: {
41-
Authorization: `Bearer ${userConfig.token}`,
42-
},
43-
json: true,
44-
};
4579
// try sending request
4680
try {
4781
const {body, statusCode} = await got.post(remoteUrl, options);

test/update.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,49 @@ module.exports = () => {
9292
});
9393
});
9494

95+
// test version check
96+
tap.test('Should display versions', t => {
97+
// handle correct request
98+
const response = {
99+
server: '0.18.0',
100+
latestServer: '0.19.1',
101+
serverUpdate: true,
102+
traefik: 'v1.3.0',
103+
latestTraefik: 'v1.3.2',
104+
traefikUpdate: true,
105+
};
106+
const updateServer = nock('http://localhost:8080')
107+
.get('/version')
108+
.reply(200, response);
109+
// spy on console
110+
const consoleSpy = sinon.spy(console, 'log');
111+
// execute login
112+
update({}).then(() => {
113+
// make sure log in was successful
114+
// check that server was called
115+
t.ok(updateServer.isDone());
116+
// first check console output
117+
t.deepEqual(
118+
consoleSpy.args,
119+
[
120+
[],
121+
['Exoframe Server:'],
122+
[' current: 0.18.0'],
123+
[' latest: 0.19.1'],
124+
[],
125+
['Traefik:'],
126+
[' current: v1.3.0'],
127+
[' latest: v1.3.2'],
128+
],
129+
'Correct log output'
130+
);
131+
// restore console
132+
console.log.restore();
133+
updateServer.done();
134+
t.end();
135+
});
136+
});
137+
95138
// test deauth
96139
tap.test('Should deauth on 401', t => {
97140
// copy original config for restoration

0 commit comments

Comments
 (0)