From d3157f735c2f8c06d1c5a0ebe45c4daaf4fdd2e5 Mon Sep 17 00:00:00 2001 From: Toshimitsu Takahashi Date: Sun, 29 Oct 2023 23:08:45 +0900 Subject: [PATCH] Use aesr-config (#320) --- package-lock.json | 19 +++++++++++++++++ package.json | 3 +++ src/js/lib/profile_db.js | 46 +++++++++++++++++++++++++++++++++++++--- src/js/options.js | 10 ++++----- 4 files changed, 70 insertions(+), 8 deletions(-) diff --git a/package-lock.json b/package-lock.json index e320e52..f1886f3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8,6 +8,9 @@ "name": "aws-extend-switch-roles", "version": "3.0.0", "license": "MIT", + "dependencies": { + "aesr-config": "^0.4.0" + }, "devDependencies": { "@playwright/test": "^1.38.1", "@rollup/plugin-node-resolve": "^15.2.3", @@ -262,6 +265,17 @@ "integrity": "sha512-j2afSsaIENvHZN2B8GOpF566vZ5WVk5opAiMTvWgaQT8DkbOqsTfvNAvHoRGU2zzP8cPoqys+xHTRDWW8L+/BA==", "dev": true }, + "node_modules/aesr-config": { + "version": "0.4.0", + "resolved": "https://registry.npmjs.org/aesr-config/-/aesr-config-0.4.0.tgz", + "integrity": "sha512-mCStfK4P4RkJr5nG9s2r4aMqKkbTcrhZlCC235+MeDNcJvSSUvysf6vZYDQBeT107mZcEA8GecSb2Z+3DTotIQ==", + "bin": { + "parse-aesr-config": "bin/parse-aesr-config.js" + }, + "engines": { + "node": ">=18" + } + }, "node_modules/agent-base": { "version": "6.0.2", "resolved": "https://registry.npmjs.org/agent-base/-/agent-base-6.0.2.tgz", @@ -2008,6 +2022,11 @@ "integrity": "sha512-j2afSsaIENvHZN2B8GOpF566vZ5WVk5opAiMTvWgaQT8DkbOqsTfvNAvHoRGU2zzP8cPoqys+xHTRDWW8L+/BA==", "dev": true }, + "aesr-config": { + "version": "0.4.0", + "resolved": "https://registry.npmjs.org/aesr-config/-/aesr-config-0.4.0.tgz", + "integrity": "sha512-mCStfK4P4RkJr5nG9s2r4aMqKkbTcrhZlCC235+MeDNcJvSSUvysf6vZYDQBeT107mZcEA8GecSb2Z+3DTotIQ==" + }, "agent-base": { "version": "6.0.2", "resolved": "https://registry.npmjs.org/agent-base/-/agent-base-6.0.2.tgz", diff --git a/package.json b/package.json index 6b877ca..fcbebfa 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/js/lib/profile_db.js b/src/js/lib/profile_db.js index 1287ae0..c30b5a1 100644 --- a/src/js/lib/profile_db.js +++ b/src/js/lib/profile_db.js @@ -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'); @@ -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'); +} diff --git a/src/js/options.js b/src/js/options.js index 664fb0e..d794aa4 100644 --- a/src/js/options.js +++ b/src/js/options.js @@ -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); @@ -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 }); }