From c2c2285136deb30214a54f2c33a023ad812368f0 Mon Sep 17 00:00:00 2001 From: Olli Vanhoja Date: Fri, 10 Mar 2017 21:35:26 +0100 Subject: [PATCH] Add support for SRV DNS records (#371) --- bin/now-dns.js | 85 ++++++++++++++++++++++++++++++++++++------- lib/domain-records.js | 6 +-- 2 files changed, 74 insertions(+), 17 deletions(-) diff --git a/bin/now-dns.js b/bin/now-dns.js index a064462..4eeff5b 100755 --- a/bin/now-dns.js +++ b/bin/now-dns.js @@ -33,7 +33,9 @@ const subcommand = argv._[0] const help = () => { console.log(` ${chalk.bold(`${logo} now dns ls`)} [domain] - ${chalk.bold(`${logo} now dns add`)} [mx_priority] + ${chalk.bold(`${logo} now dns add`)} + ${chalk.bold(`${logo} now dns add`)} MX + ${chalk.bold(`${logo} now dns add`)} SRV ${chalk.bold(`${logo} now dns rm`)} ${chalk.dim('Options:')} @@ -117,7 +119,7 @@ async function run(token) { record.name, record.type, record.value, - record.mxPriority ? record.mxPriority : '', + record.mxPriority || record.priority || '', time ] })), {align: ['l', 'r', 'l', 'l', 'l', 'l'], hsep: ' '.repeat(2), stringLength: strlen}) @@ -127,21 +129,14 @@ async function run(token) { console.log(`> ${count} record${count === 1 ? '' : 's'} found ${chalk.gray(`[${elapsed}]`)}`) console.log(text.join('')) } else if (subcommand === 'add') { - const domain = args[0] - const name = args[1] === '@' ? '' : args[1] - const type = args[2] - const value = args[3] - const mxPriority = args[4] - - if (!(args.length >= 4 && args.length <= 5) || - (type === 'MX' ? !mxPriority : mxPriority)) { - error(`Invalid number of arguments. Usage: ${chalk.cyan('`now dns add [mx_priority]`')}`) + const param = parseAddArgs(args) + if (!param) { + error(`Invalid number of arguments. See: ${chalk.cyan('`now dns --help`')} for usage.`) return exit(1) } - - const record = await domainRecords.create(domain, {name, type, value, mxPriority}) + const record = await domainRecords.create(param.domain, param.data) const elapsed = ms(new Date() - start) - console.log(`${chalk.cyan('> Success!')} A new DNS record for domain ${chalk.bold(domain)} ${chalk.gray(`(${record.uid})`)} created ${chalk.gray(`[${elapsed}]`)}`) + console.log(`${chalk.cyan('> Success!')} A new DNS record for domain ${chalk.bold(param.domain)} ${chalk.gray(`(${record.uid})`)} created ${chalk.gray(`[${elapsed}]`)}`) } else if (subcommand === 'rm' || subcommand === 'remove') { if (args.length !== 1) { error(`Invalid number of arguments. Usage: ${chalk.cyan('`now dns rm `')}`) @@ -176,6 +171,68 @@ process.on('uncaughtException', err => { exit(1) }) +function parseAddArgs(args) { + if (!args || args.length < 4) { + return null + } + + const domain = args[0] + const name = args[1] === '@' ? '' : args[1] + const type = args[2] + const value = args[3] + + if (!(domain && typeof name === 'string' && type)) { + return null + } + + if (type === 'MX') { + if (args.length !== 5) { + return null + } + + return { + domain, + data: { + name, + type, + value, + mxPriority: args[4] + } + } + } else if (type === 'SRV') { + if (args.length !== 7) { + return null + } + + return { + domain, + data: { + name, + type, + srv: { + priority: value, + weight: args[4], + port: args[5], + target: args[6] + } + } + } + } + + if (args.length !== 4) { + return null + } + + return { + domain, + data: { + name, + type, + value + } + } +} + function readConfirmation(record, msg) { return new Promise(resolve => { const time = chalk.gray(ms(new Date() - new Date(Number(record.created))) + ' ago') diff --git a/lib/domain-records.js b/lib/domain-records.js index fbc14f1..5465e65 100644 --- a/lib/domain-records.js +++ b/lib/domain-records.js @@ -53,7 +53,7 @@ module.exports = class DomainRecords extends Now { return records } - create(domain, {name, type, value, mxPriority} = {}) { + create(domain, data) { const url = `/domains/${domain}/records` return this.retry(async (bail, attempt) => { @@ -62,7 +62,7 @@ module.exports = class DomainRecords extends Now { } const res = await this._fetch(url, { method: 'POST', - body: {name, value, type, mxPriority} + body: data }) if (this._debug) { console.timeEnd(`> [debug] #${attempt} POST ${url}`) @@ -72,7 +72,7 @@ module.exports = class DomainRecords extends Now { if (res.status === 400) { return bail(new Error(body.error ? body.error.message : 'Unknown error')) } else if (res.status === 403) { - const err = new Error(`Not authorized to access the domain "${name}"`) + const err = new Error(`Not authorized to access the domain "${domain}"`) err.userError = true return bail(err) } else if (res.status === 404) {