Skip to content

Instantly share code, notes, and snippets.

@jrgm
Created January 15, 2016 21:15
Show Gist options
  • Save jrgm/96922c7cec1e8fbbff55 to your computer and use it in GitHub Desktop.
Save jrgm/96922c7cec1e8fbbff55 to your computer and use it in GitHub Desktop.
check for typos in config file
#!/usr/bin/env node
// Run this script after editing ./fxa/yaml/type/fxa.fxa_oauth.prod.yaml or
// ./fxa/yaml/type/fxa.fxa_oauth.stage.yaml and fix any typos before
// committing changes.
var assert = require('assert')
var fs = require('fs')
var yaml
try {
yaml = require('js-yaml')
} catch(e) {
console.log("You need to do `npm install js-yaml` to use this script")
}
var file = process.argv[2]
if (!file) {
console.log('Usage:', process.argv[1], '/path/to/yaml/file')
process.exit(1)
}
function errorExit() {
console.log('*** Checks failed ***')
process.exit(1)
}
var expectedKeys = [
"id",
"name",
"imageUri",
"redirectUri",
"termsUri",
"privacyUri",
"canGrant",
"trusted",
"hashedSecret"
].sort()
var clients = yaml.safeLoad(fs.readFileSync(file, 'utf8'))['fxa_oauth::clients']
var ids = {}
clients.forEach(function(client) {
var name = client.name
assert.ok(name, 'client has a name')
var id = client.id
assert.ok(name, 'client has an id')
console.log('Checking entry for', id, "/", name)
if (ids[id]) {
console.log('Duplicate client id', id)
errorExit()
}
ids[id] = 1
var keys = Object.keys(client).sort()
if (keys.length !== expectedKeys.length) {
console.log('Unexpected number of keys')
console.log('Got:', keys.join(','))
console.log('Exp:', expectedKeys.join(','))
errorExit()
}
if (keys.join(',') !== expectedKeys.join(',')) {
console.log('Key names do not match. Possible typo in key?')
console.log('Got:', keys.join(','))
console.log('Exp:', expectedKeys.join(','))
errorExit()
}
if (!client.id.match(/^[a-f0-9]{16}$/)) {
console.log(client.name, ': "id" must be 16 hexadecimal characters!')
errorExit()
}
if (!client.hashedSecret.match(/^[a-f0-9]{64}$/)) {
console.log(client.name, ': "hashedSecret" must be 64 hexadecimal characters!')
errorExit()
}
})
console.log('Checks passed OK.')
process.exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment