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
refs TryGhost#164
- add sudo ui method that shows the sudo command before it is run
  • Loading branch information
acburdine committed Jun 18, 2017
1 parent bd1799e commit ac4c58d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
12 changes: 5 additions & 7 deletions lib/services/nginx/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,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(`${ghostExec} 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(`${ghostExec} service nginx-ssl ${answers.email}${answers.staging && ' staging'}`).catch((error) => {
return Promise.reject(new errors.ProcessError(error));
});
}
Expand Down Expand Up @@ -204,9 +202,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
12 changes: 12 additions & 0 deletions 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,17 @@ class UI {
});
}

sudo(command, options) {
return this.noSpin(() => {
this.log(`Running sudo command: ${command}`, 'gray');
let execaOptions = assign({
stdio: 'inherit'
}, options);

return execa.shell(`sudo ${command}`, execaOptions);
});
}

noSpin(promiseOrFunc) {
if (this.spinner) {
this.spinner.stop();
Expand Down

0 comments on commit ac4c58d

Please sign in to comment.