|
| 1 | +import * as fs from "fs"; |
| 2 | + |
| 3 | +export function parseEdgeGridToSectionArray(edgeGridFilePath) { |
| 4 | + if (!fs.existsSync(edgeGridFilePath)) { |
| 5 | + throw new Error(`edge grid file does not exist: ${edgeGridFilePath}`); |
| 6 | + } |
| 7 | + var data = fs.readFileSync(edgeGridFilePath).toString(); |
| 8 | + var lines = data.split('\n') |
| 9 | + .map(s => s.trim()) // remove leading and trailing spaces |
| 10 | + .filter(l => l.length > 0) // remove empty lines |
| 11 | + .filter(l => !isCommentedLine(l)); // remove comments |
| 12 | + |
| 13 | + var sectionToLines = new Map(); |
| 14 | + var currentSection = getSectionNameOrNull(lines[0]); |
| 15 | + if (currentSection === null) { |
| 16 | + currentSection = 'default'; |
| 17 | + sectionToLines.set(currentSection, []); |
| 18 | + } |
| 19 | + |
| 20 | + for (var i = 0; i < lines.length; i++) { |
| 21 | + var line = lines[i]; |
| 22 | + var sectionName = getSectionNameOrNull(line); |
| 23 | + if (sectionName !== null) { |
| 24 | + if (sectionToLines.has(sectionName)) { |
| 25 | + throw new Error(`Invalid edgerc file format: found section ${sectionName} multiple times`); |
| 26 | + } |
| 27 | + currentSection = sectionName; |
| 28 | + sectionToLines.set(currentSection, []); |
| 29 | + } else { |
| 30 | + sectionToLines.get(currentSection).push(line); |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + var sections = []; |
| 35 | + sectionToLines.forEach((lines, key) => { |
| 36 | + var s = parseSectionLinesToConfig(lines, key); |
| 37 | + sections.push(s); |
| 38 | + }); |
| 39 | + return sections; |
| 40 | +} |
| 41 | + |
| 42 | +function isCommentedLine(line) { |
| 43 | + return line.startsWith(';') || line.startsWith('#'); |
| 44 | +} |
| 45 | + |
| 46 | +function parseSectionLinesToConfig(lines, sectionName) { |
| 47 | + var m = sectionLinesToMap(lines, sectionName); |
| 48 | + var host = getEdgeProp('host', m, sectionName); |
| 49 | + var clientToken = getEdgeProp('client_token', m, sectionName); |
| 50 | + var clientSecret = getEdgeProp('client_secret', m, sectionName); |
| 51 | + var accessToken = getEdgeProp('access_token', m, sectionName); |
| 52 | + return { |
| 53 | + sectionName, |
| 54 | + host, |
| 55 | + clientToken, |
| 56 | + clientSecret, |
| 57 | + accessToken |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +function getEdgeProp(key, map, sectionName) { |
| 62 | + if (!map.has(key)) { |
| 63 | + throw new Error(`Missing ${key} in edgegrid section: ${sectionName}`); |
| 64 | + } |
| 65 | + return map.get(key); |
| 66 | +} |
| 67 | + |
| 68 | +function sectionLinesToMap(lines, sectionName) { |
| 69 | + var r = new Map(); |
| 70 | + lines.forEach(l => { |
| 71 | + var kvp = lineToKvp(l); |
| 72 | + if (r.has(kvp.key)) { |
| 73 | + throw new Error(`Duplicate key detected in edgerc section: ${sectionName} key: ${kvp.key}`); |
| 74 | + } |
| 75 | + r.set(kvp.key, kvp.value); |
| 76 | + }); |
| 77 | + return r; |
| 78 | +} |
| 79 | + |
| 80 | +function lineToKvp(line) { |
| 81 | + var index = line.indexOf('='); |
| 82 | + if (index === -1) { |
| 83 | + throw new Error(`line is invalid: ${line} - no '=' character found`); |
| 84 | + } else if (index === 0) { |
| 85 | + throw new Error(`line is invalid: ${line} - empty string before '=' character`); |
| 86 | + } else if (index === line.length - 1) { |
| 87 | + throw new Error(`line is invalid: ${line} - value is empty`); |
| 88 | + } |
| 89 | + const key = line.substring(0, index).trim(); |
| 90 | + const value = line.substring(index + 1, line.length).trim(); |
| 91 | + |
| 92 | + if (key.length === 0) { |
| 93 | + throw new Error(`line is invalid: ${line} - key is empty`); |
| 94 | + } else if (value.length === 0) { |
| 95 | + throw new Error(`line is invalid: ${line} - value is empty`); |
| 96 | + } |
| 97 | + |
| 98 | + return { |
| 99 | + key, |
| 100 | + value |
| 101 | + }; |
| 102 | +} |
| 103 | + |
| 104 | +function getSectionNameOrNull(s) { |
| 105 | + if (!s) { |
| 106 | + return null; |
| 107 | + } |
| 108 | + if (s.charAt(0) !== '[') { |
| 109 | + return null; |
| 110 | + } |
| 111 | + if (s.charAt(s.length - 1) !== ']') { |
| 112 | + throw new Error(`Invalid section string no matching closing bracket: ${s}`); |
| 113 | + } |
| 114 | + if (s.length == 2) { |
| 115 | + throw new Error(`Empty section name detected: ${s}`); |
| 116 | + } |
| 117 | + return s.substring(1, s.length - 1); |
| 118 | +} |
0 commit comments