Skip to content

Commit

Permalink
Merge pull request actions#3 from akamai/DEVPOPS-534_edgerc_parser_ch…
Browse files Browse the repository at this point in the history
…ange

edgegrid parsing change
  • Loading branch information
bradforj287 committed Nov 1, 2018
2 parents 15385c8 + f476439 commit a489063
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 5 deletions.
20 changes: 16 additions & 4 deletions src/cli-main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import * as fs from 'fs';
import * as path from "path";
import * as os from "os";

const uuidv1 = require('uuid/v1');

const CLI_CACHE_PATH = process.env.AKAMAI_CLI_CACHE_PATH;
Expand Down Expand Up @@ -112,7 +113,15 @@ function showLocalSandboxes() {
sandbox_id: sb.sandboxId
}
});
console.table(sandboxes);
showSandboxesTable(sandboxes);
}

function showSandboxesTable(sandboxes) {
if (sandboxes.length === 0) {
console.log('no sandboxes found');
} else {
console.table(sandboxes);
}
}

async function showRemoteSandboxes() {
Expand All @@ -128,7 +137,7 @@ async function showRemoteSandboxes() {
status: sb.status
}
});
console.table(sandboxes);
showSandboxesTable(sandboxes);
}

program
Expand Down Expand Up @@ -560,6 +569,9 @@ function createFromCloneRecipe(recipe) {
}

function validateAndBuildRecipe(recipeFilePath, name, clonable): any {
if (typeof name !== 'string') {
name = null;
}
console.log('validating recipe file');
if (!fs.existsSync(recipeFilePath)) {
logAndExit(`File ${recipeFilePath} does not exist.`);
Expand All @@ -571,7 +583,7 @@ function validateAndBuildRecipe(recipeFilePath, name, clonable): any {
}
const sandboxRecipe = recipe.sandbox;
sandboxRecipe.clonable = clonable || sandboxRecipe.clonable;
sandboxRecipe.name = name;
sandboxRecipe.name = name || sandboxRecipe.name;
if (sandboxRecipe.properties) {
sandboxRecipe.properties.forEach(p => {
if (p.rulesPath) {
Expand Down Expand Up @@ -629,7 +641,7 @@ async function updateFromRecipe(sandboxId, recipeFilePath, name, clonable) {

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

Expand Down
118 changes: 118 additions & 0 deletions src/utils/edgerc-parser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import * as fs from "fs";

export function parseEdgeGridToSectionArray(edgeGridFilePath) {
if (!fs.existsSync(edgeGridFilePath)) {
throw new Error(`edge grid file does not exist: ${edgeGridFilePath}`);
}
var data = fs.readFileSync(edgeGridFilePath).toString();
var lines = data.split('\n')
.map(s => s.trim()) // remove leading and trailing spaces
.filter(l => l.length > 0) // remove empty lines
.filter(l => !isCommentedLine(l)); // remove comments

var sectionToLines = new Map();
var currentSection = getSectionNameOrNull(lines[0]);
if (currentSection === null) {
currentSection = 'default';
sectionToLines.set(currentSection, []);
}

for (var i = 0; i < lines.length; i++) {
var line = lines[i];
var sectionName = getSectionNameOrNull(line);
if (sectionName !== null) {
if (sectionToLines.has(sectionName)) {
throw new Error(`Invalid edgerc file format: found section ${sectionName} multiple times`);
}
currentSection = sectionName;
sectionToLines.set(currentSection, []);
} else {
sectionToLines.get(currentSection).push(line);
}
}

var sections = [];
sectionToLines.forEach((lines, key) => {
var s = parseSectionLinesToConfig(lines, key);
sections.push(s);
});
return sections;
}

function isCommentedLine(line) {
return line.startsWith(';') || line.startsWith('#');
}

function parseSectionLinesToConfig(lines, sectionName) {
var m = sectionLinesToMap(lines, sectionName);
var host = getEdgeProp('host', m, sectionName);
var clientToken = getEdgeProp('client_token', m, sectionName);
var clientSecret = getEdgeProp('client_secret', m, sectionName);
var accessToken = getEdgeProp('access_token', m, sectionName);
return {
sectionName,
host,
clientToken,
clientSecret,
accessToken
}
}

function getEdgeProp(key, map, sectionName) {
if (!map.has(key)) {
throw new Error(`Missing ${key} in edgegrid section: ${sectionName}`);
}
return map.get(key);
}

function sectionLinesToMap(lines, sectionName) {
var r = new Map();
lines.forEach(l => {
var kvp = lineToKvp(l);
if (r.has(kvp.key)) {
throw new Error(`Duplicate key detected in edgerc section: ${sectionName} key: ${kvp.key}`);
}
r.set(kvp.key, kvp.value);
});
return r;
}

function lineToKvp(line) {
var index = line.indexOf('=');
if (index === -1) {
throw new Error(`line is invalid: ${line} - no '=' character found`);
} else if (index === 0) {
throw new Error(`line is invalid: ${line} - empty string before '=' character`);
} else if (index === line.length - 1) {
throw new Error(`line is invalid: ${line} - value is empty`);
}
const key = line.substring(0, index).trim();
const value = line.substring(index + 1, line.length).trim();

if (key.length === 0) {
throw new Error(`line is invalid: ${line} - key is empty`);
} else if (value.length === 0) {
throw new Error(`line is invalid: ${line} - value is empty`);
}

return {
key,
value
};
}

function getSectionNameOrNull(s) {
if (!s) {
return null;
}
if (s.charAt(0) !== '[') {
return null;
}
if (s.charAt(s.length - 1) !== ']') {
throw new Error(`Invalid section string no matching closing bracket: ${s}`);
}
if (s.length == 2) {
throw new Error(`Empty section name detected: ${s}`);
}
return s.substring(1, s.length - 1);
}
14 changes: 13 additions & 1 deletion src/utils/env-utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
var EdgeGrid = require('edgegrid');
import * as os from 'os';

var findJavaHome = require('find-java-home');
var path = require('path');
import * as edgeRcParser from './edgerc-parser';

const _edge = null;
const edgeRcParams = {
Expand All @@ -10,11 +12,21 @@ const edgeRcParams = {
debug: false
};

function getEdgeGridSection(section) {
var sections = edgeRcParser.parseEdgeGridToSectionArray(edgeRcParams.path);
return sections.find(s => s.sectionName === section);
}

function getAllEdgeGridSections() {
return edgeRcParser.parseEdgeGridToSectionArray(edgeRcParams.path);
}

export function getEdgeGrid() {
if (_edge != null) {
return _edge;
}
return new EdgeGrid(edgeRcParams);
var s = getEdgeGridSection(edgeRcParams.section);
return new EdgeGrid(s.clientToken, s.clientSecret, s.accessToken, s.host, edgeRcParams.debug);
}

export function setDebugMode(debug: boolean) {
Expand Down

0 comments on commit a489063

Please sign in to comment.