Skip to content

Commit 2ed6274

Browse files
committed
Merge branch 'develop', prepare 0.23.0
2 parents e59a606 + 61616fd commit 2ed6274

File tree

2 files changed

+170
-51
lines changed

2 files changed

+170
-51
lines changed

src/commands/update.js

Lines changed: 100 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,65 @@ const _ = require('lodash');
33
const got = require('got');
44
const chalk = require('chalk');
55
const ora = require('ora');
6+
const inquirer = require('inquirer');
67

78
// our packages
89
const {userConfig, isLoggedIn, logout} = require('../config');
910

1011
// valid targets list
11-
const validTargets = ['traefik', 'server'];
12+
const validTargets = ['traefik', 'server', 'all'];
13+
// construct shared request params
14+
const options = {
15+
headers: {
16+
Authorization: `Bearer ${userConfig.token}`,
17+
},
18+
json: true,
19+
};
20+
21+
const runUpdate = async target => {
22+
console.log(chalk.bold(`Updating ${target} on:`), userConfig.endpoint);
23+
24+
// services request url
25+
const remoteUrl = `${userConfig.endpoint}/update/${target}`;
26+
// try sending request
27+
try {
28+
const {body, statusCode} = await got.post(remoteUrl, options);
29+
if (statusCode !== 200 || body.error) {
30+
throw new Error(body.error || 'Oops. Something went wrong! Try again please.');
31+
}
32+
33+
if (body.updated) {
34+
console.log(chalk.green(`Successfully updated ${target}!`));
35+
return;
36+
}
37+
38+
console.log(chalk.green(`${_.capitalize(target)} is already up to date!`));
39+
} catch (e) {
40+
// if authorization is expired/broken/etc
41+
if (e.statusCode === 401) {
42+
logout(userConfig);
43+
console.log(chalk.red('Error: authorization expired!'), 'Please, relogin and try again.');
44+
return;
45+
}
46+
47+
const reason = e.response.body && e.response.body.error ? e.response.body.error : e.toString();
48+
console.log(chalk.red(`Error updating ${target}:`), reason);
49+
console.log('Update log:\n');
50+
(e.response.body.log || 'No log available')
51+
.split('\n')
52+
.map(l => {
53+
try {
54+
return JSON.parse(l);
55+
} catch (e) {
56+
return l;
57+
}
58+
})
59+
.filter(l => l !== undefined)
60+
.map(l => l.trim())
61+
.filter(l => l && l.length > 0)
62+
.forEach(line => console.log(line));
63+
}
64+
};
1265

1366
exports.command = ['update [target]'];
1467
exports.describe = 'check for updates or update given target';
@@ -23,14 +76,6 @@ exports.handler = async ({target}) => {
2376
return;
2477
}
2578

26-
// construct shared request params
27-
const options = {
28-
headers: {
29-
Authorization: `Bearer ${userConfig.token}`,
30-
},
31-
json: true,
32-
};
33-
3479
// if no target given - check for update
3580
if (!target || !target.length) {
3681
// show loader
@@ -60,7 +105,44 @@ exports.handler = async ({target}) => {
60105
console.log(chalk.bold('Traefik:'));
61106
console.log(` current: ${body.traefik}`);
62107
console.log(` latest: ${body.latestTraefik}`);
63-
return;
108+
console.log();
109+
110+
// if updates are available - ask user if he want them immediately
111+
if (!body.serverUpdate && !body.traefikUpdate) {
112+
return;
113+
}
114+
115+
const prompts = [];
116+
if (body.serverUpdate) {
117+
prompts.push({
118+
type: 'confirm',
119+
name: 'upServer',
120+
message: 'Update server now?',
121+
default: true,
122+
});
123+
}
124+
125+
if (body.traefikUpdate) {
126+
prompts.push({
127+
type: 'confirm',
128+
name: 'upTraefik',
129+
message: 'Update Traefik now?',
130+
default: true,
131+
});
132+
}
133+
const {upServer, upTraefik} = await inquirer.prompt(prompts);
134+
// if user doesn't want update - just exit
135+
if (!upServer && !upTraefik) {
136+
return;
137+
}
138+
// define target based on user input
139+
if (upServer && upTraefik) {
140+
target = 'all';
141+
} else if (upServer) {
142+
target = 'server';
143+
} else if (upTraefik) {
144+
target = 'traefik';
145+
}
64146
}
65147

66148
if (!validTargets.includes(target)) {
@@ -72,46 +154,13 @@ exports.handler = async ({target}) => {
72154
return;
73155
}
74156

75-
console.log(chalk.bold(`Updating ${target} on:`), userConfig.endpoint);
76-
77-
// services request url
78-
const remoteUrl = `${userConfig.endpoint}/update/${target}`;
79-
// try sending request
80-
try {
81-
const {body, statusCode} = await got.post(remoteUrl, options);
82-
if (statusCode !== 200 || body.error) {
83-
throw new Error(body.error || 'Oops. Something went wrong! Try again please.');
84-
}
85-
86-
if (body.updated) {
87-
console.log(chalk.green(`Successfully updated ${target}!`));
88-
return;
89-
}
90-
91-
console.log(chalk.green(`${_.capitalize(target)} is already up to date!`));
92-
} catch (e) {
93-
// if authorization is expired/broken/etc
94-
if (e.statusCode === 401) {
95-
logout(userConfig);
96-
console.log(chalk.red('Error: authorization expired!'), 'Please, relogin and try again.');
97-
return;
98-
}
99-
100-
const reason = e.response.body && e.response.body.error ? e.response.body.error : e.toString();
101-
console.log(chalk.red(`Error updating ${target}:`), reason);
102-
console.log('Update log:\n');
103-
(e.response.body.log || 'No log available')
104-
.split('\n')
105-
.map(l => {
106-
try {
107-
return JSON.parse(l);
108-
} catch (e) {
109-
return l;
110-
}
111-
})
112-
.filter(l => l !== undefined)
113-
.map(l => l.trim())
114-
.filter(l => l && l.length > 0)
115-
.forEach(line => console.log(line));
157+
// if target is all - run updates sequentially
158+
if (target === 'all') {
159+
await runUpdate('traefik');
160+
await runUpdate('server');
161+
return;
116162
}
163+
164+
// otherwise - just run given target
165+
await runUpdate(target);
117166
};

test/update.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
const tap = require('tap');
33
const nock = require('nock');
44
const sinon = require('sinon');
5+
const inquirer = require('inquirer');
56

67
// our packages
78
const {handler: update} = require('../src/commands/update');
@@ -108,6 +109,8 @@ module.exports = () => {
108109
.reply(200, response);
109110
// spy on console
110111
const consoleSpy = sinon.spy(console, 'log');
112+
// stup inquirer answers
113+
sinon.stub(inquirer, 'prompt').callsFake(() => Promise.resolve({upServer: false, upTraefik: false}));
111114
// execute login
112115
update({}).then(() => {
113116
// make sure log in was successful
@@ -125,16 +128,83 @@ module.exports = () => {
125128
['Traefik:'],
126129
[' current: v1.3.0'],
127130
[' latest: v1.3.2'],
131+
[],
128132
],
129133
'Correct log output'
130134
);
131135
// restore console
132136
console.log.restore();
137+
// restore inquirer
138+
inquirer.prompt.restore();
139+
// cleanup server
133140
updateServer.done();
134141
t.end();
135142
});
136143
});
137144

145+
// test version check
146+
tap.test('Should update all on user prompt', t => {
147+
// handle correct request
148+
const response = {
149+
server: '0.18.0',
150+
latestServer: '0.19.1',
151+
serverUpdate: true,
152+
traefik: 'v1.3.0',
153+
latestTraefik: 'v1.3.2',
154+
traefikUpdate: true,
155+
};
156+
const updateInfoServer = nock('http://localhost:8080')
157+
.get('/version')
158+
.reply(200, response);
159+
const updateServerRun = nock('http://localhost:8080')
160+
.post('/update/server')
161+
.reply(200, {updated: true});
162+
const updateTraefikRun = nock('http://localhost:8080')
163+
.post('/update/traefik')
164+
.reply(200, {updated: true});
165+
// spy on console
166+
const consoleSpy = sinon.spy(console, 'log');
167+
// stup inquirer answers
168+
sinon.stub(inquirer, 'prompt').callsFake(() => Promise.resolve({upServer: true, upTraefik: true}));
169+
// execute login
170+
update({}).then(() => {
171+
// make sure log in was successful
172+
// check that servers were called
173+
t.ok(updateInfoServer.isDone());
174+
t.ok(updateServerRun.isDone());
175+
t.ok(updateTraefikRun.isDone());
176+
// first check console output
177+
t.deepEqual(
178+
consoleSpy.args,
179+
[
180+
[],
181+
['Exoframe Server:'],
182+
[' current: 0.18.0'],
183+
[' latest: 0.19.1'],
184+
[],
185+
['Traefik:'],
186+
[' current: v1.3.0'],
187+
[' latest: v1.3.2'],
188+
[],
189+
['Updating traefik on:', 'http://localhost:8080'],
190+
['Successfully updated traefik!'],
191+
['Updating server on:', 'http://localhost:8080'],
192+
['Successfully updated server!'],
193+
],
194+
'Correct log output'
195+
);
196+
// restore console
197+
console.log.restore();
198+
// restore inquirer
199+
inquirer.prompt.restore();
200+
// cleanup server
201+
updateInfoServer.done();
202+
updateServerRun.done();
203+
updateTraefikRun.done();
204+
t.end();
205+
});
206+
});
207+
138208
// test deauth
139209
tap.test('Should deauth on 401', t => {
140210
// copy original config for restoration

0 commit comments

Comments
 (0)