Skip to content

Commit

Permalink
Use aesr-config (#320)
Browse files Browse the repository at this point in the history
  • Loading branch information
tilfin authored Oct 29, 2023
1 parent 4df1522 commit d3157f7
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 8 deletions.
19 changes: 19 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
"url": "https://github.com/tilfinltd/aws-extend-switch-roles/issues"
},
"homepage": "https://github.com/tilfinltd/aws-extend-switch-roles#readme",
"dependencies": {
"aesr-config": "^0.4.0"
},
"devDependencies": {
"@playwright/test": "^1.38.1",
"@rollup/plugin-node-resolve": "^15.2.3",
Expand Down
46 changes: 43 additions & 3 deletions src/js/lib/profile_db.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,42 @@
import { ConfigParser } from 'aesr-config';
import { DBManager } from './db.js';
import { loadConfigIni } from './config_ini.js';
import { loadAwsConfig } from './load_aws_config.js';

export async function writeProfileSetToTable(profileSet) {
const dbManager = new DBManager('aesr');
await dbManager.open();

await dbManager.transaction('profiles', async dbTable => {
await dbTable.truncate();
});

await dbManager.transaction('profiles', async dbTable => {
let i = 0;
for (const profile of profileSet.singles) {
await dbTable.insert({
profilePath: `[SINGLE];${formatNum(++i)}`,
...profile,
});
}

for (const baseProfile of profileSet.complexes) {
const { targets, ...props } = baseProfile;
await dbTable.insert({
profilePath: `[COMPLEX];${formatNum(++i)}`,
...props,
});

for (const targetProfile of targets) {
await dbTable.insert({
profilePath: `${props.name};${formatNum(++i)}`,
...targetProfile,
});
}
}
});

await dbManager.close();
}

export async function writeProfileItemsToTable(items, replace = false) {
const dbManager = new DBManager('aesr');
Expand Down Expand Up @@ -41,8 +77,12 @@ export async function writeProfileItemsToTable(items, replace = false) {
export async function refreshDB(storageRepo) {
const cfgText = await loadConfigIni(storageRepo);
if (cfgText) {
const items = loadAwsConfig(cfgText);
await writeProfileItemsToTable(items, true);
const profileSet = ConfigParser.parseIni(cfgText);
await writeProfileSetToTable(profileSet);
}
return cfgText;
}

function formatNum(num) {
return `${num}`.padStart(6, '0');
}
10 changes: 5 additions & 5 deletions src/js/options.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { ConfigParser } from 'aesr-config';
import { nowEpochSeconds } from './lib/util.js';
import { deleteConfigIni, loadConfigIni, saveConfigIni } from './lib/config_ini.js';
import { loadConfigIni, saveConfigIni } from './lib/config_ini.js';
import { ColorPicker } from './lib/color_picker.js';
import { SessionMemory, StorageProvider } from './lib/storage_repository.js';
import { loadAwsConfig } from './lib/load_aws_config.js';
import { writeProfileItemsToTable } from "./lib/profile_db.js";
import { writeProfileSetToTable } from "./lib/profile_db.js";

function elById(id) {
return document.getElementById(id);
Expand Down Expand Up @@ -159,8 +159,8 @@ async function saveConfiguration(text, storageArea) {
await syncRepo.set({ profilesLastUpdated: now });
}

const items = loadAwsConfig(text);
await writeProfileItemsToTable(items, true);
const profileSet = ConfigParser.parseIni(text);
await writeProfileSetToTable(profileSet);
await localRepo.set({ profilesTableUpdated: now });
}

Expand Down

0 comments on commit d3157f7

Please sign in to comment.