Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use aesr-config #320

Merged
merged 1 commit into from
Oct 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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