Skip to content

Commit

Permalink
chore: replace credentials.json for a environmental variable
Browse files Browse the repository at this point in the history
  • Loading branch information
UlisesGascon committed Jul 11, 2023
1 parent 0897b32 commit 107b480
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 27 deletions.
4 changes: 2 additions & 2 deletions update/add-route.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ const qs = require('querystring')

const url = 'https://api.mailgun.net/v3/routes'

function addRoute (domain, creds, description, expression, actions, callback) {
function addRoute (domain, description, expression, actions, callback) {
const params = {
description,
expression,
action: actions
}
const data = qs.stringify(params)
const options = {
auth: `api:${creds['api-key']}`,
auth: `api:${process.env.MAILGUN_API_KEY}`,
headers: {
'content-type': 'application/x-www-form-urlencoded',
'content-length': Buffer.byteLength(data)
Expand Down
4 changes: 2 additions & 2 deletions update/delete-route.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ const jsonist = require('jsonist')

const url = 'https://api.mailgun.net/v3/routes'

function deleteRoute (domain, creds, id, callback) {
const options = { auth: `api:${creds['api-key']}` }
function deleteRoute (domain, id, callback) {
const options = { auth: `api:${process.env.MAILGUN_API_KEY}` }
jsonist.delete(`${url}/${id}`, options, callback)
}

Expand Down
4 changes: 2 additions & 2 deletions update/list-routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ const jsonist = require('jsonist')

const url = 'https://api.mailgun.net/v3/routes'

function listRoutes (domain, creds, callback) {
const options = { auth: `api:${creds['api-key']}` }
function listRoutes (domain, callback) {
const options = { auth: `api:${cprocess.env.MAILGUN_API_KEY}` }
jsonist.get(url, options, callback)
}

Expand Down
8 changes: 4 additions & 4 deletions update/update-aliases.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function diff (domain, a1, a2) {
})
}

function updateAliases (domain, creds, aliases, dryRun, callback) {
function updateAliases (domain, aliases, dryRun, callback) {
function adjustRoutes (routes) {
const current = routes.items

Expand All @@ -56,19 +56,19 @@ function updateAliases (domain, creds, aliases, dryRun, callback) {
const to = Array.isArray(alias.to) ? alias.to : [alias.to]
console.log(`Adding ${alias.from} -> ${to.join(', ')}...`)
if (!dryRun) {
addRoute(domain, creds, alias.from, alias.expression, alias.actions, done)
addRoute(domain, alias.from, alias.expression, alias.actions, done)
}
})

toRemove.forEach(function (alias) {
console.log(`Deleting ${alias.expression}...`)
if (!dryRun) {
deleteRoute(domain, creds, alias.id, done)
deleteRoute(domain, alias.id, done)
}
})
}

listRoutes(domain, creds, function (err, routes) {
listRoutes(domain, function (err, routes) {
if (err) { throw err }
adjustRoutes(routes)
})
Expand Down
20 changes: 3 additions & 17 deletions update/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

const fs = require('fs')
, path = require('path')

, updateAliases = require('./update-aliases')

require('http').globalAgent.maxSockets = 20
Expand All @@ -15,28 +14,15 @@ if (process.argv.length < 3) {
const dryRun = process.argv.includes('--dry-run')
, domain = process.argv.filter((a) => a !== '--dry-run')[2].replace(/\/$/, '')
, dir = path.join(__dirname, '..', domain)
, credsFile = path.join(dir, 'credentials.json')

if (!fs.statSync(dir).isDirectory()) {
console.error(`Usage: update <domain> ("domain" must be a directory above ${__dirname}`)
return process.exit(1)
}

if (!fs.existsSync(credsFile)) {
console.error(`Error: ${dir} does not have a credentials.json file`)
return process.exit(1)
}

const creds = require(credsFile)

if (typeof creds['api-key'] != 'string') {
console.error(`Error: ${credsFile} does not have an "api-key" property`)
if (!process.env.MAILGUN_API_KEY) {
console.error(`Error: MAILGUN_API_KEY environment variable is not set`)
return process.exit(1)
}

const aliases = require(path.join(dir, 'aliases.json'))

updateAliases(domain, creds, aliases, dryRun, function (err) {
updateAliases(domain, aliases, dryRun, function (err) {
if (err)
throw err
})

0 comments on commit 107b480

Please sign in to comment.