Skip to content

Commit

Permalink
feat(ui): show sudo command before it is run
Browse files Browse the repository at this point in the history
closes TryGhost#164
- add sudo ui method that shows the sudo command before it is run
- make log method synchronous again
  • Loading branch information
acburdine committed Jun 18, 2017
1 parent bd1799e commit a3d98ab
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 20 deletions.
13 changes: 5 additions & 8 deletions lib/services/nginx/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ class NginxService extends BaseService {
});
}

let ghostExec = process.argv.slice(0,2).join(' ');
let answerPromise;
let answers;

Expand All @@ -89,14 +88,12 @@ class NginxService extends BaseService {
return answerPromise.then((_answers) => {
answers = _answers;

return this.ui.noSpin(execa.shell(`sudo ${ghostExec} service nginx-conf${!answers.ssl ? ' no-ssl' : ''}`, {stdio: 'inherit'})).catch((error) => {
return this.ui.sudo(`ghost service nginx-conf${!answers.ssl ? ' no-ssl' : ''}`).catch((error) => {
return Promise.reject(new errors.ProcessError(error));
});
}).then(() => {
if (answers.ssl) {
return this.ui.noSpin(execa.shell(`sudo ${ghostExec} service nginx-ssl ${answers.email}${answers.staging ? ' staging' : ''}`, {
stdio: 'inherit'
})).catch((error) => {
return this.ui.sudo(`ghost service nginx-ssl ${answers.email}${answers.staging ? ' staging' : ''}`).catch((error) => {
return Promise.reject(new errors.ProcessError(error));
});
}
Expand Down Expand Up @@ -204,9 +201,9 @@ class NginxService extends BaseService {
let confFile = `${this.parsedUrl.hostname}.conf`;

if (fs.existsSync(`/etc/nginx/sites-available/${confFile}`)) {
return this.ui.noSpin(() => execa.shell(`sudo rm /etc/nginx/sites-available/${confFile} && sudo rm /etc/nginx/sites-enabled/${confFile}`, {
stdio: 'inherit'
}).catch(() => Promise.reject(new errors.SystemError('Nginx config file could not be removed, you will need to do this manually.'))));
return this.ui.sudo(`rm /etc/nginx/sites-available/${confFile}`).then(() => {
return this.ui.sudo(`rm /etc/nginx/sites-enabled/${confFile}`);
}).catch(() => Promise.reject(new errors.SystemError('Nginx config file could not be removed, you will need to do this manually.')));
}
}
};
Expand Down
14 changes: 7 additions & 7 deletions lib/services/process/systemd/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,28 +30,28 @@ class SystemdProcess extends BaseProcess {
ghost_exec_path: process.argv.slice(0,2).join(' ')
}), 'utf8');

return this.ui.noSpin(() => execa.shell(`sudo mv ${serviceFilename} /lib/systemd/system`, {stdio: 'inherit'}).catch(() => {
return this.ui.sudo(`mv ${serviceFilename} /lib/systemd/system`).catch(() => {
return Promise.reject(new errors.SystemError('Ghost service file could not be put in place, ensure you have proper sudo permissions and systemd is installed.'));
}));
});
}

uninstall() {
let serviceFilename = `/lib/systemd/system/${this.systemdName}.service`;

if (fs.existsSync(serviceFilename)) {
return this.ui.noSpin(() => execa.shell(`sudo rm ${serviceFilename}`, {stdio: 'inherit'}).catch(() =>
Promise.reject(new errors.SystemError('Ghost systemd service file could not be removed, you will need to do it manually.'))
));
return this.ui.sudo(`rm ${serviceFilename}`).catch(
() => Promise.reject(new errors.SystemError('Ghost systemd service file could not be removed, you will need to do it manually.'))
);
}
}

start() {
return this.ui.noSpin(() => execa.shell(`sudo systemctl start ${this.systemdName}`, {stdio: 'inherit'}))
return this.ui.sudo(`systemctl start ${this.systemdName}`)
.catch((error) => Promise.reject(new errors.ProcessError(error)));
}

stop() {
return this.ui.noSpin(() => execa.shell(`sudo systemctl stop ${this.systemdName}`, {stdio: 'inherit'}))
return this.ui.sudo(`systemctl stop ${this.systemdName}`)
.catch((error) => Promise.reject(new errors.ProcessError(error)));
}

Expand Down
18 changes: 17 additions & 1 deletion lib/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const fs = require('fs-extra');
const path = require('path');
const ora = require('ora');
const chalk = require('chalk');
const execa = require('execa');
const errors = require('./errors');
const Table = require('cli-table2');
const assign = require('lodash/assign');
Expand Down Expand Up @@ -78,6 +79,21 @@ class UI {
});
}

sudo(command, options) {
this.log(`Running sudo command: ${command}`, 'gray');

return this.noSpin(() => {
let execaOptions = assign({
stdio: 'inherit'
}, options || {});

return execa.shell(
`sudo ${command.replace(/^ghost/, process.argv.slice(0, 2).join(' '))}`,
execaOptions
);
});
}

noSpin(promiseOrFunc) {
if (this.spinner) {
this.spinner.stop();
Expand All @@ -97,7 +113,7 @@ class UI {
message = chalk[color](message);
}

this.noSpin(() => this.stdout.write(`${message}\n`));
this.stdout.write(`\r${message}\n`);
}

success(message) {
Expand Down
8 changes: 4 additions & 4 deletions test/unit/ui/index-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('Unit: UI', function () {
stdout = streamTestUtils.getWritableStream(function (output) {
expect(output, 'output exists').to.be.ok;
expect(chalk.hasColor(output), 'output has color').to.be.false;
expect(output, 'output value').to.equal('test\n');
expect(output, 'output value').to.equal('\rtest\n');

done();
});
Expand All @@ -34,7 +34,7 @@ describe('Unit: UI', function () {
stdout = streamTestUtils.getWritableStream(function (output) {
expect(output, 'output exists').to.be.ok;
expect(chalk.hasColor(output), 'output has color').to.be.true;
expect(output, 'output value').to.equal(chalk.green('test') + '\n');
expect(output, 'output value').to.equal(`\r${chalk.green('test')}\n`);

done();
});
Expand All @@ -51,7 +51,7 @@ describe('Unit: UI', function () {
stdout = streamTestUtils.getWritableStream(function (output) {
expect(output, 'output exists').to.be.ok;
expect(chalk.hasColor(output), 'output has color').to.be.true;
expect(output, 'output value').to.equal(chalk.green('test') + '\n');
expect(output, 'output value').to.equal(`\r${chalk.green('test')}\n`);

done();
});
Expand All @@ -67,7 +67,7 @@ describe('Unit: UI', function () {
stdout = streamTestUtils.getWritableStream(function (output) {
expect(output, 'output exists').to.be.ok;
expect(chalk.hasColor(output), 'output has color').to.be.true;
expect(output, 'output value').to.equal(chalk.red('test') + '\n');
expect(output, 'output value').to.equal(`\r${chalk.red('test')}\n`);

done();
});
Expand Down

0 comments on commit a3d98ab

Please sign in to comment.