Browse Source

add confirmation for removal

master
Guillermo Rauch 9 years ago
parent
commit
77d10deddb
  1. 4
      bin/now-remove
  2. 52
      lib/alias.js

4
bin/now-remove

@ -55,13 +55,13 @@ function readConfirmation (app) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const time = chalk.gray(ms(new Date() - app.created) + ' ago'); const time = chalk.gray(ms(new Date() - app.created) + ' ago');
const tbl = table( const tbl = table(
[[app.uid, time, `https://${app.url}`]], [[app.uid, `https://${app.url}`, time]],
{ align: ['l', 'r', 'l'], hsep: ' '.repeat(6) } { align: ['l', 'r', 'l'], hsep: ' '.repeat(6) }
); );
process.stdout.write('> The following deployment will be removed permanently\n'); process.stdout.write('> The following deployment will be removed permanently\n');
process.stdout.write(' ' + tbl + '\n'); process.stdout.write(' ' + tbl + '\n');
process.stdout.write(` ${chalk.bold.red('> Are you sure?')} ${chalk.gray('[yN] ')}`); process.stdout.write(`${chalk.bold.red('> Are you sure?')} ${chalk.gray('[yN] ')}`);
process.stdin.on('data', (d) => { process.stdin.on('data', (d) => {
process.stdin.pause(); process.stdin.pause();

52
lib/alias.js

@ -3,6 +3,8 @@ import dns from 'dns';
import Now from '../lib'; import Now from '../lib';
import toHost from './to-host'; import toHost from './to-host';
import chalk from 'chalk'; import chalk from 'chalk';
import table from 'text-table';
import ms from 'ms';
export default class Alias extends Now { export default class Alias extends Now {
@ -37,22 +39,26 @@ export default class Alias extends Now {
throw err; throw err;
} }
return this.retry(async (bail, attempt) => { if (await readConfirmation(_alias)) {
const res = await this._fetch(`/now/aliases/${_alias.uid}`, { return this.retry(async (bail, attempt) => {
method: 'DELETE' const res = await this._fetch(`/now/aliases/${_alias.uid}`, {
}); method: 'DELETE'
});
if (403 === res.status) { if (403 === res.status) {
return bail(new Error('Unauthorized')); return bail(new Error('Unauthorized'));
} }
if (res.status !== 200) { if (res.status !== 200) {
const err = new Error('Deletion failed. Try again later.'); const err = new Error('Deletion failed. Try again later.');
throw err; throw err;
} }
console.log(`${chalk.cyan('> Success!')}`); console.log(`${chalk.cyan('> Success!')} The alias has been removed.`);
}); });
} else {
console.log('> Aborted');
}
} }
async findAlias (alias) { async findAlias (alias) {
@ -271,3 +277,23 @@ function resolve4 (host) {
}); });
}); });
} }
function readConfirmation (alias) {
return new Promise((resolve, reject) => {
const time = chalk.gray(ms(new Date() - new Date(alias.created)) + ' ago');
const tbl = table(
[[alias.uid, `https://${alias.alias}`, time]],
{ align: ['l', 'r', 'l'], hsep: ' '.repeat(3) }
);
process.stdout.write('> The following alias will be removed permanently\n');
process.stdout.write(' ' + tbl + '\n');
process.stdout.write(`${chalk.bold.red('> Are you sure?')} ${chalk.gray('[yN] ')}`);
process.stdin.on('data', (d) => {
process.stdin.pause();
process.stdin.write('\n');
resolve('y' === d.toString().trim());
}).resume();
});
}

Loading…
Cancel
Save