Browse Source

Add support for SRV DNS records (#371)

master
Olli Vanhoja 8 years ago
committed by Matheus Fernandes
parent
commit
c2c2285136
  1. 85
      bin/now-dns.js
  2. 6
      lib/domain-records.js

85
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`)} <domain> <name> <A | AAAA | ALIAS | CNAME | MX | TXT> <value> [mx_priority]
${chalk.bold(`${logo} now dns add`)} <domain> <name> <A | AAAA | ALIAS | CNAME | TXT> <value>
${chalk.bold(`${logo} now dns add`)} <domain> <name> MX <value> <mx_priority>
${chalk.bold(`${logo} now dns add`)} <domain> <name> SRV <priority> <weight> <port> <target>
${chalk.bold(`${logo} now dns rm`)} <id>
${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 <domain> <name> <type> <value> [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 <id>`')}`)
@ -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')

6
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) {

Loading…
Cancel
Save