├── .eslintignore ├── .gitignore ├── bin ├── .gitignore └── csv-to-zone-file ├── .eslintrc ├── README.md ├── LICENSE.md ├── package.json └── app.js /.eslintignore: -------------------------------------------------------------------------------- 1 | node_modules -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /bin/.gitignore: -------------------------------------------------------------------------------- 1 | node_modules -------------------------------------------------------------------------------- /bin/csv-to-zone-file: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | require('../app.js'); 4 | -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "apostrophe", 3 | "rules": { 4 | "no-console": 0 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Produces a zone file suitable for import into Amazon Route 53. If your goal is 2 | to actually use it in a standalone DNS server, you'll want to add 3 | an SOA (Start Of Authority) record to the output manually. 4 | 5 | Run: 6 | 7 | ``` 8 | npm install -g csv-to-zone-file 9 | csv-to-zone-file < myfile.csv > myfile.zone 10 | ``` 11 | 12 | The CSV file must have columns as shown in this example: 13 | 14 | | Type | Host | Value | 15 | | ------ | ------ | ------- | 16 | | A | www | a.b.c.d | 17 | 18 | `Type` may be `A`, `CNAME`, etc. The suffix ` Record` is stripped from `Type` if present. 19 | 20 | `TTL` is an optional column. If not present the TTL is 1 hour. 21 | 22 | If `MX` records are present there must be a `Priority` column as well. 23 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | Copyright (c) 2021 Apostrophe Technologies, Inc. 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 8 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "csv-to-zone-file", 3 | "version": "1.0.0", 4 | "description": "Converts CSV to a zone-file just good enough to import into Amazon Route 53", 5 | "main": "app.js", 6 | "scripts": { 7 | "test": "npx eslint ." 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/apostrophecms/csv-to-zone-file.git" 12 | }, 13 | "keywords": [ 14 | "zone", 15 | "file" 16 | ], 17 | "author": "Apostrophe Technologies, Inc.", 18 | "license": "MIT", 19 | "bugs": { 20 | "url": "https://github.com/apostrophecms/csv-to-zone-file/issues" 21 | }, 22 | "homepage": "https://github.com/apostrophecms/csv-to-zone-file#readme", 23 | "bin": { 24 | "csv-to-zone-file": "./bin/csv-to-zone-file" 25 | }, 26 | "dependencies": { 27 | "csv-parse": "^4.16.3", 28 | "eslint": "^7.0.0", 29 | "eslint-config-apostrophe": "^3.4.1", 30 | "eslint-config-standard": "^16.0.3", 31 | "eslint-plugin-import": "^2.25.2", 32 | "eslint-plugin-node": "^11.1.0", 33 | "eslint-plugin-promise": "^5.1.1", 34 | "eslint-plugin-standard": "^5.0.0" 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /app.js: -------------------------------------------------------------------------------- 1 | const fs = require('fs'); 2 | const csvParse = require('csv-parse/lib/sync'); 3 | 4 | const data = fs.readFileSync('/dev/stdin', 'utf8'); 5 | const records = parse(data); 6 | 7 | let failed = false; 8 | 9 | for (const record of records) { 10 | if (!record.Type) { 11 | continue; 12 | } 13 | if (!record.Host) { 14 | fail('No Host specified', record); 15 | } 16 | if (!record.Value) { 17 | fail('No Value specified', record); 18 | } 19 | if (!record.TTL) { 20 | record.TTL = 3600; 21 | } 22 | // Normalize case and deal with a common suffix when copying and pasting to a spreadsheet 23 | const type = record.Type.toUpperCase().replace(' RECORD', ''); 24 | if (type === 'MX') { 25 | if (!record.Priority) { 26 | fail('No Priority specified', record); 27 | } 28 | console.log(`${record.Host} ${record.TTL} ${type} ${record.Priority} ${record.Value}`); 29 | } else { 30 | console.log(`${record.Host} ${record.TTL} ${type} ${record.Value}`); 31 | } 32 | } 33 | 34 | if (failed) { 35 | process.exit(1); 36 | } 37 | 38 | function fail(msg, record) { 39 | console.error(`${msg}:`, record); 40 | failed = true; 41 | } 42 | 43 | function parse (input) { 44 | return csvParse(input, { 45 | columns: true, 46 | skip_empty_lines: true, 47 | // Does not trim quoted values, see below 48 | trim: true 49 | }).filter(row => Object.keys(row).length > 0).map(row => 50 | Object.fromEntries( 51 | Object.entries(row).map(([ key, val ]) => [ key.trim(), (val != null) ? val.toString().trim() : val ]) 52 | ) 53 | ); 54 | } 55 | --------------------------------------------------------------------------------