Browse Source

Merge branch 'add/now-alias'

master
Guillermo Rauch 9 years ago
parent
commit
525242ce54
  1. 4
      bin/now
  2. 122
      bin/now-alias
  3. 72
      lib/alias.js

4
bin/now

@ -19,8 +19,8 @@ const exit = (code) => {
};
const defaultCommand = 'deploy';
const commands = new Set([defaultCommand, 'list', 'ls', 'rm', 'remove']);
const aliases = new Map([['ls', 'list'], ['rm', 'remove']]);
const commands = new Set([defaultCommand, 'list', 'ls', 'rm', 'remove', 'alias']);
const aliases = new Map([['ls', 'list'], ['rm', 'remove'], ['ln', 'alias']]);
let cmd = argv._[0];
let args = [];

122
bin/now-alias

@ -0,0 +1,122 @@
#!/usr/bin/env node
import minimist from 'minimist';
// import chalk from 'chalk';
// import ms from 'ms';
// import table from 'text-table';
import NowAlias from '../lib/alias';
import login from '../lib/login';
import * as cfg from '../lib/cfg';
import { error } from '../lib/error';
const argv = minimist(process.argv.slice(2));
const subcommand = argv._[0];
// options
const help = () => {
console.log(`
𝚫 now alias <ls | set | rm> <now_url> <alias> [≤alias2>[, <alias3>]]
Alias: ln
Options:
-h, --help output usage information
-d, --debug Debug mode [off]
Examples:
Lists all the aliases you have configured:
$ now alias ls
Adds a particular alias to my-api.now.sh:
$ now alias set api-ownv3nc9f8.now.sh my-api.now.sh
The \`.now.sh\` suffix can be ommited:
$ now alias set api-ownv3nc9f8 my-api
The deployment id can be used as the source:
$ now alias set deploymentId my-alias
Custom domains work as well. They will ask you for your email
address as we dynamically provision a certificate:
$ now alias set api-ownv3nc9f8.now.sh my-api.com
You can optionally put \`http://\` or \`https://\` in front of the
parameters and they will be ignored.
Removing an alias:
$ now alias rm aliasId
To get the list of alias ids, use \`now alias ls\`.
`);
};
if (argv.h || argv.help) {
help();
process.exit(0);
}
if (!subcommand) {
error('No subcommand specified');
help();
process.exit(1);
}
// options
const debug = argv.debug || argv.d;
const apiUrl = argv.url || 'https://api.zeit.co';
const config = cfg.read();
Promise.resolve(config.token || login(apiUrl))
.then(async (token) => {
try {
await run(token);
} catch (err) {
error(`Unknown error: ${err.stack}`);
process.exit(1);
}
})
.catch((e) => {
error(`Authentication error – ${e.message}`);
process.exit(1);
});
function getArgs (args) {
return { url: args[1], aliases: args.slice(2) };
}
async function run (token) {
const alias = new NowAlias(apiUrl, token, { debug });
const { url, aliases } = getArgs(argv._);
if ('ls' !== subcommand) {
if (!url) {
error('No deployment url specified. You can see active deployments with `now ls`.');
help();
process.exit(1);
} else if (!aliases.length) {
error('No alias url specified. This is the URL your deployment will be tied to.');
}
}
switch (subcommand) {
case 'ls':
await alias.ls(url);
break;
case 'rm':
await alias.rm(url, aliases);
break;
case 'set':
await alias.set(url, aliases);
break;
default:
error('Invalid subcommand');
help();
process.exit();
}
}

72
lib/alias.js

@ -0,0 +1,72 @@
import retry from './retry';
import Now from '../lib';
export default class Alias extends Now {
async ls (url) {
console.log('list', url);
const deploymentId = url; // TODO get from API
return retry(async (bail) => {
const res = await this._fetch(`/now/aliases/${deploymentId}/`);
if (200 !== res.status && (400 <= res.status || 500 > res.status)) {
if (this._debug) console.log('> [debug] bailing on creating due to %s', res.status);
return bail(responseError(res));
}
return await res.json();
}, { retries: 3, minTimeout: 2500, onRetry: this._onRetry });
}
async rm (url, aliases) {
console.log('rm', url, aliases);
const deploymentId = url; // TODO get from API
return await Promise.all(aliases.map(async (alias) => {
retry(async (bail) => {
const res = await this._fetch(`/now/aliases/${deploymentId}/${alias}`, {
method: 'DELETE'
});
if (200 !== res.status && (400 <= res.status || 500 > res.status)) {
if (this._debug) console.log('> [debug] bailing on creating due to %s', res.status);
return bail(responseError(res));
}
return await res.json();
}, { retries: 3, minTimeout: 2500, onRetry: this._onRetry });
}));
}
async set (url, aliases) {
console.log('set', url, aliases);
const deploymentId = url; // TODO get from API
return retry(async (bail) => {
const res = await this._fetch(`/now/aliases/${deploymentId}/`, {
method: 'POST',
body: {
aliases: aliases
}
});
if (200 !== res.status && (400 <= res.status || 500 > res.status)) {
if (this._debug) console.log('> [debug] bailing on creating due to %s', res.status);
return bail(responseError(res));
}
return await res.json();
}, { retries: 3, minTimeout: 2500, onRetry: this._onRetry });
}
}
function responseError (res) {
const err = new Error('Response error');
err.status = res.status;
if (429 === res.status) {
const retryAfter = res.headers.get('Retry-After');
if (retryAfter) {
err.retryAfter = parseInt(retryAfter, 10);
}
}
return err;
}
Loading…
Cancel
Save