Skip to content

Commit

Permalink
cleanup: remove nearley parsers
Browse files Browse the repository at this point in the history
  • Loading branch information
msimerson committed Apr 15, 2022
1 parent 4a125c8 commit 9517626
Show file tree
Hide file tree
Showing 17 changed files with 118 additions and 449 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,4 @@ dist
dist/*-grammar.js
dist/*.js
package-lock.json
ignore/*
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,12 @@ Returns a list of zones (and zone files) from the specified nameserver config fi
- [x] knot
- [x] maradns
- [x] tinydns
- [ ] powerdns
- [ ] config generator
- [ ] bind
- [ ] nsd
- [ ] knot
- [ ] maradns
- [ ] tinydns
- [ ] powerdns



## SEE ALSO
Expand Down
63 changes: 47 additions & 16 deletions bin/nt-ns.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,46 @@ function usage () {
const opts = cmdLineArgs(usageOptions())._all
if (opts.verbose) console.error(opts)
if (opts.help) usage()
if (!opts.import) usage()
if (!opts.file) usage()


import zone from 'dns-zone-validator'
import bind from '../lib/bind.js'
import knot from '../lib/knot.js'
import maradns from '../lib/maradns.js'
import nsd from '../lib/nsd.js'
import tinydns from '../lib/tinydns.js'

const nsTypes = {
bind,
knot,
maradns,
nsd,
tinydns,
}

function usageOptions () {
return [
{
name : 'import',
alias : 'i',
defaultValue: 'bind',
type : String,
typeLabel : '<bind | knot | maradns | nsd | tinydns>',
description : 'nameserver type',
group : 'io',
name : 'import',
alias : 'i',
type : String,
typeLabel : '<bind | knot | maradns | nsd | tinydns>',
description: 'nameserver type',
group : 'io',
},
{
name : 'export',
alias : 'e',
defaultValue: 'js',
type : String,
typeLabel : '<bind | knot | maradns | nsd | tinydns>',
description : 'nameserver type',
group : 'io',
name : 'export',
alias : 'e',
type : String,
typeLabel : '<bind | knot | maradns | nsd | tinydns>',
description: 'nameserver type',
group : 'io',
},
{
name : 'file',
alias : 'f',
// defaultValue: '',
type : String,
typeLabel : '<file path>',
description: 'source of DNS server config file',
Expand All @@ -54,7 +68,6 @@ function usageOptions () {
{
name : 'base',
alias : 'b',
// defaultValue: '',
type : String,
typeLabel : '<zones dir>',
description: 'path prefix for zone files',
Expand Down Expand Up @@ -118,3 +131,21 @@ function usageSections () {
},
]
}


getZoneList()
.then(r => {
console.log(r)
return r
})
.catch(e => {
console.error(e)
})

async function getZoneList () {
const zoneList = await nsTypes[opts.import].getZones(opts.file, opts.base)
for (const z in zoneList) {

}
return zoneList
}
10 changes: 9 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@

import path from 'path'


export default {
fullPath,
}


export function fullPath (basePath, filePath) {
if (!basePath) return filePath
// if (filePath.startsWith('/')) return filePath
return path.resolve(basePath, path.basename(filePath))
}

export function valueCleanup (str) {

Expand Down
29 changes: 7 additions & 22 deletions lib/bind.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@

import fs from 'fs/promises'

import { valueCleanup } from '../index.js'
import { fullPath, valueCleanup } from '../index.js'

export default {
fullPath,
getZones,
}

export async function getZones (filePath) {
export async function getZones (filePath, basePath) {
const buf = await fs.readFile(filePath)
const r = parseConfig(buf.toString())
const s = new Map()
r.zone.map(z => s.set(z.name.toLowerCase(), z.file))
r.zone.map(z => s.set(z.name.toLowerCase(), fullPath(basePath, z.file)))
return s
}

Expand Down Expand Up @@ -87,22 +91,3 @@ export function parseConfig (str) {

return res
}



/*
const os = require('os')
const nearley = require('nearley')
exports.parseConfig = async str => {
const grammar = nearley.Grammar.fromCompiled(require('../dist/bind.js'))
grammar.start = 'main'
const parser = new nearley.Parser(grammar)
parser.feed(str)
if (!str.endsWith(os.EOL)) parser.feed(os.EOL)
if (parser.length > 1) console.error(`ERROR: ambigious parser rule`)
if (parser.results.length === 0) return []
return parser.results[0]
.filter(z => z[0] && z[0].type) // weed out nulls
.map(z => z[0]) // remove array nesting
}
*/
33 changes: 7 additions & 26 deletions lib/knot.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@

import fs from 'fs/promises'

import { valueCleanup } from '../index.js'
import { fullPath, valueCleanup } from '../index.js'

export async function getZones (filePath) {
export default {
getZones,
}

export async function getZones (filePath, basePath) {
const buf = await fs.readFile(filePath)
const r = parseConfig(buf.toString())
const s = new Map()
r.zone.map(z => s.set(z.domain.toLowerCase(), z.file))
r.zone.map(z => s.set(z.domain.toLowerCase(), fullPath(basePath, z.file)))
return s
}

Expand Down Expand Up @@ -76,26 +80,3 @@ export function parseConfig (str) {

return res
}



/*
const os = require('os')
const nearley = require('nearley')
exports.parseConfig = async str => {
const grammar = nearley.Grammar.fromCompiled(require('../dist/knot.js'))
grammar.start = 'main'
const parser = new nearley.Parser(grammar)
parser.feed(str)
if (!str.endsWith(os.EOL)) parser.feed(os.EOL)
if (parser.results.length === 0) return []
const r = {}
const sections = [ 'acl', 'key', 'log', 'policy', 'remote', 'server', 'template', 'zone' ]
parser.results[0][0]
.filter(z => z !== null)
.map(z => { for (const s of sections) {
if (z[s]) { if (!r[s]) r[s] = [] r[s].push(...z[s]) }
}})
return r
}
*/
9 changes: 6 additions & 3 deletions lib/maradns.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@

import fs from 'fs/promises'

import { valueCleanup } from '../index.js'
import { fullPath, valueCleanup } from '../index.js'

export default {
getZones,
}

export async function getZones (filePath) {
export async function getZones (filePath, basePath) {
const buf = await fs.readFile(filePath)
const r = parseConfig(buf.toString())
const s = new Map()
for (const z in r) {
const zf = z.match(/csv2\["(.*)"]/)
if (zf) {
s.set(zf[1].toLowerCase(), r[z])
s.set(zf[1].toLowerCase(), fullPath(basePath, r[z]))
}
}
return s
Expand Down
33 changes: 6 additions & 27 deletions lib/nsd.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@

import fs from 'fs/promises'

import { valueCleanup } from '../index.js'
import { fullPath, valueCleanup } from '../index.js'

export default {
getZones,
}

export async function getZones (filePath) {
export async function getZones (filePath, basePath) {
const buf = await fs.readFile(filePath)
const r = parseConfig(buf.toString())
const s = new Map()
r.zone.map(z => s.set(z.name.toLowerCase(), z.zonefile))
r.zone.map(z => s.set(z.name.toLowerCase(), fullPath(basePath, z.zonefile)))
return s
}

Expand Down Expand Up @@ -58,27 +61,3 @@ export function parseConfig (str) {

return res
}


/*
const os = require('os')
const nearley = require('nearley')
const ns = require('../index')
exports.parseConfig = async str => {
const grammar = nearley.Grammar.fromCompiled(require('../dist/nsd.js'))
grammar.start = 'main'
const parser = new nearley.Parser(grammar)
parser.feed(str + os.EOL)
if (!str.endsWith(os.EOL)) parser.feed(os.EOL)
if (parser.results.length === 0) return []
const r = {}
const sections = [ 'key', 'pattern', 'remote-control', 'server', 'tls-auth', 'zone' ]
parser.results[0]
.filter(z => z !== null)
.map(z => { for (const s of sections) {
if (z[s]) { if (!r[s]) r[s] = []; r[s].push(z[s]) }
}})
return r
}
*/
3 changes: 3 additions & 0 deletions lib/tinydns.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@

import fs from 'fs/promises'

export default {
getZones,
}

export async function getZones (filePath) {
const buf = await fs.readFile(filePath)
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"dependencies": {
"chalk": "^5.0.1",
"command-line-args": "^5.2.1",
"command-line-usage": "^6.1.2"
"command-line-usage": "^6.1.2",
"dns-zone-validator": "^0.8.0"
}
}
80 changes: 0 additions & 80 deletions src/bind-moo.ne

This file was deleted.

Loading

0 comments on commit 9517626

Please sign in to comment.