Skip to content

Commit a489063

Browse files
authored
Merge pull request actions#3 from akamai/DEVPOPS-534_edgerc_parser_change
edgegrid parsing change
2 parents 15385c8 + f476439 commit a489063

File tree

3 files changed

+147
-5
lines changed

3 files changed

+147
-5
lines changed

src/cli-main.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import * as fs from 'fs';
33
import * as path from "path";
44
import * as os from "os";
5+
56
const uuidv1 = require('uuid/v1');
67

78
const CLI_CACHE_PATH = process.env.AKAMAI_CLI_CACHE_PATH;
@@ -112,7 +113,15 @@ function showLocalSandboxes() {
112113
sandbox_id: sb.sandboxId
113114
}
114115
});
115-
console.table(sandboxes);
116+
showSandboxesTable(sandboxes);
117+
}
118+
119+
function showSandboxesTable(sandboxes) {
120+
if (sandboxes.length === 0) {
121+
console.log('no sandboxes found');
122+
} else {
123+
console.table(sandboxes);
124+
}
116125
}
117126

118127
async function showRemoteSandboxes() {
@@ -128,7 +137,7 @@ async function showRemoteSandboxes() {
128137
status: sb.status
129138
}
130139
});
131-
console.table(sandboxes);
140+
showSandboxesTable(sandboxes);
132141
}
133142

134143
program
@@ -560,6 +569,9 @@ function createFromCloneRecipe(recipe) {
560569
}
561570
562571
function validateAndBuildRecipe(recipeFilePath, name, clonable): any {
572+
if (typeof name !== 'string') {
573+
name = null;
574+
}
563575
console.log('validating recipe file');
564576
if (!fs.existsSync(recipeFilePath)) {
565577
logAndExit(`File ${recipeFilePath} does not exist.`);
@@ -571,7 +583,7 @@ function validateAndBuildRecipe(recipeFilePath, name, clonable): any {
571583
}
572584
const sandboxRecipe = recipe.sandbox;
573585
sandboxRecipe.clonable = clonable || sandboxRecipe.clonable;
574-
sandboxRecipe.name = name;
586+
sandboxRecipe.name = name || sandboxRecipe.name;
575587
if (sandboxRecipe.properties) {
576588
sandboxRecipe.properties.forEach(p => {
577589
if (p.rulesPath) {
@@ -629,7 +641,7 @@ async function updateFromRecipe(sandboxId, recipeFilePath, name, clonable) {
629641

630642
for (var i = 0; i < sandboxRecipe.properties.length; i++) {
631643
const rp = sandboxRecipe.properties[i];
632-
console.log(`re-building property: ${i+1}`);
644+
console.log(`re-building property: ${i + 1}`);
633645
await cliUtils.spinner(createRecipeProperty(rp, sandboxId));
634646
}
635647

src/utils/edgerc-parser.ts

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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+
}

src/utils/env-utils.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
var EdgeGrid = require('edgegrid');
22
import * as os from 'os';
3+
34
var findJavaHome = require('find-java-home');
45
var path = require('path');
6+
import * as edgeRcParser from './edgerc-parser';
57

68
const _edge = null;
79
const edgeRcParams = {
@@ -10,11 +12,21 @@ const edgeRcParams = {
1012
debug: false
1113
};
1214

15+
function getEdgeGridSection(section) {
16+
var sections = edgeRcParser.parseEdgeGridToSectionArray(edgeRcParams.path);
17+
return sections.find(s => s.sectionName === section);
18+
}
19+
20+
function getAllEdgeGridSections() {
21+
return edgeRcParser.parseEdgeGridToSectionArray(edgeRcParams.path);
22+
}
23+
1324
export function getEdgeGrid() {
1425
if (_edge != null) {
1526
return _edge;
1627
}
17-
return new EdgeGrid(edgeRcParams);
28+
var s = getEdgeGridSection(edgeRcParams.section);
29+
return new EdgeGrid(s.clientToken, s.clientSecret, s.accessToken, s.host, edgeRcParams.debug);
1830
}
1931

2032
export function setDebugMode(debug: boolean) {

0 commit comments

Comments
 (0)