From 113e6308341fbeb1405163b5d5de962a85236f04 Mon Sep 17 00:00:00 2001 From: Louis Capitanchik <contact@louiscap.co> Date: Fri, 18 Oct 2019 15:12:39 +0100 Subject: [PATCH] Fix whitespace --- .editorconfig | 2 + index.js | 111 +++++++++++++++++++++++++------------------------- 2 files changed, 57 insertions(+), 56 deletions(-) create mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..8306dad --- /dev/null +++ b/.editorconfig @@ -0,0 +1,2 @@ +[*] +indent_style=tab \ No newline at end of file diff --git a/index.js b/index.js index f1679a0..fde8d40 100644 --- a/index.js +++ b/index.js @@ -5,86 +5,85 @@ const base = 'https://api.cloudflare.com/client/v4/' const gurl = ep => `${ base }${ ep }` async function getIp() { - console.log(`[CFDNS] Fetching IP from https://api.ipify.org`) + console.log(`[CFDNS] Fetching IP from https://api.ipify.org`) const res = await request('https://api.ipify.org?format=json') const { ip } = JSON.parse(res) return ip } async function getRecord(zone_id, domain, opts) { - const params = new URLSearchParams() - params.set('type', 'A') - params.set('name', domain) + const params = new URLSearchParams() + params.set('type', 'A') + params.set('name', domain) - const parts = await request(gurl(`zones/${ zone_id }/dns_records?${ params.toString() }`), opts) + const parts = await request(gurl(`zones/${ zone_id }/dns_records?${ params.toString() }`), opts) const json = JSON.parse(parts) return json.result[0] - } async function setRecordIp(zone_id, record, ip, opts) { - const url = gurl(`zones/${ zone_id }/dns_records/${ record.id }`) - const method = 'PUT' - const json = { - type: 'A', - name: record.name, - content: ip, - proxies: true, - } + const url = gurl(`zones/${ zone_id }/dns_records/${ record.id }`) + const method = 'PUT' + const json = { + type: 'A', + name: record.name, + content: ip, + proxies: true, + } - return request(url, { - method, - json, - ...opts, - }) + return request(url, { + method, + json, + ...opts, + }) } async function createRecord(zone_id, domain, ip, opts) { - const url = gurl(`zones/${ zone_id }/dns_records`) - const method = 'POST' - const json = { - type: 'A', - name: domain, - content: ip, - proxies: true, - } + const url = gurl(`zones/${ zone_id }/dns_records`) + const method = 'POST' + const json = { + type: 'A', + name: domain, + content: ip, + proxies: true, + } - return request(url, { - method, - json, - ...opts, - }) + return request(url, { + method, + json, + ...opts, + }) } async function main() { - const config = await fs.readAsync('./config.json', 'json') - const { zone_id, token, hosts } = config + const config = await fs.readAsync('./config.json', 'json') + const { zone_id, token, hosts } = config - const requestOpts = { - headers: { - "Authorization": `Bearer ${ token }`, - }, - } + const requestOpts = { + headers: { + "Authorization": `Bearer ${ token }`, + }, + } - const ip = await getIp() + const ip = await getIp() - console.log('[CFDNS] Using IP %s for all domains', ip) + console.log('[CFDNS] Using IP %s for all domains', ip) - for (const entry of hosts) { - const { domain } = entry - const record = await getRecord(zone_id, domain, requestOpts) - if (record) { - if (record.content === ip) { - console.log('[CFDNS] Skipping %s', domain) - continue - } - console.log('[CFDNS] Updating record for %s', domain) - await setRecordIp(zone_id, record, ip, requestOpts) - } else { - console.log('[CFDNS] Creating new record for %s', domain) - await createRecord(zone_id, domain, ip, requestOpts) - } - } + for (const entry of hosts) { + const { domain } = entry + const record = await getRecord(zone_id, domain, requestOpts) + if (record) { + if (record.content === ip) { + console.log('[CFDNS] Skipping %s', domain) + continue + } + console.log('[CFDNS] Updating record for %s', domain) + await setRecordIp(zone_id, record, ip, requestOpts) + } else { + console.log('[CFDNS] Creating new record for %s', domain) + await createRecord(zone_id, domain, ip, requestOpts) + } + } } main() \ No newline at end of file -- GitLab