Browse Source

Accept alias name in `now alias rm`, add retries

master
Tony Kovanen 9 years ago
parent
commit
cf4b176ec9
  1. 74
      lib/alias.js

74
lib/alias.js

@ -9,29 +9,75 @@ export default class Alias extends Now {
async ls (deployment) { async ls (deployment) {
if (deployment) { if (deployment) {
const target = await this.findDeployment(deployment); const target = await this.findDeployment(deployment);
const res = await this._fetch(`/now/deployments/${target.uid}/aliases`);
const body = await res.json(); return this.retry(async (bail, attempt) => {
return body.aliases; const res = await this._fetch(`/now/deployments/${target.uid}/aliases`);
const body = await res.json();
return body.aliases;
});
} }
const res = await this._fetch('/now/aliases'); return this.retry(async (bail, attempt) => {
const body = await res.json(); const res = await this._fetch('/now/aliases');
return body.aliases; const body = await res.json();
return body.aliases;
});
} }
async rm (alias) { async rm (alias) {
const res = await this._fetch(`/now/aliases/${alias}`, { const _alias = await this.findAlias(alias);
method: 'DELETE' if (!_alias) {
}); const err = new Error(`Alias not found by "${alias}". Run ${chalk.dim('`now alias ls`')} to see your aliases.`);
const body = await res.json();
if (res.status !== 200) {
const err = new Error('Deletion failed ' + body);
err.userError = true; err.userError = true;
throw err; throw err;
} }
console.log(`${chalk.cyan('> Success!')}`); return this.retry(async (bail, attempt) => {
const res = await this._fetch(`/now/aliases/${_alias.uid}`, {
method: 'DELETE'
});
if (403 === res.status) {
bail(new Error('Unauthorized'));
}
if (res.status !== 200) {
const err = new Error('Deletion failed. Try again later.');
throw err;
}
console.log(`${chalk.cyan('> Success!')}`);
});
}
async findAlias (alias) {
const list = await this.ls();
let key, val;
if (/\./.test(alias)) {
val = toHost(alias);
key = 'alias';
} else {
val = alias;
key = 'uid';
}
const _alias = list.find((d) => {
if (d[key] === val) {
if (this._debug) console.log(`> [debug] matched alias ${d.uid} by ${key} ${val}`);
return true;
}
// match prefix
if (`${val}.now.sh` === d.alias) {
if (this._debug) console.log(`> [debug] matched alias ${d.uid} by url ${d.host}`);
return true;
}
return false;
});
return _alias;
} }
async findDeployment (deployment) { async findDeployment (deployment) {

Loading…
Cancel
Save