Skip to content

Commit

Permalink
Process config files for profile names containing prefix separator (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr authored Dec 6, 2023
1 parent bc4911f commit 6884910
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/sharp-pants-hug.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@smithy/shared-ini-file-loader": patch
---

Process config files for profile names containing prefix separator
12 changes: 12 additions & 0 deletions packages/shared-ini-file-loader/src/getConfigData.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ describe(getConfigData.name, () => {
it.each([IniSectionType.SSO_SESSION, IniSectionType.SERVICES])("includes sections with '%s' prefix", (prefix) => {
const mockInput = { [[prefix, "test"].join(CONFIG_PREFIX_SEPARATOR)]: { key: "value" } };
expect(getConfigData(mockInput)).toStrictEqual(mockInput);

// Profile name containing CONFIG_PREFIX_SEPARATOR
const profileName = ["foo", "bar"].join(CONFIG_PREFIX_SEPARATOR);
const mockInput2 = { [[prefix, profileName].join(CONFIG_PREFIX_SEPARATOR)]: { key: "value" } };
expect(getConfigData(mockInput2)).toStrictEqual(mockInput2);
});

describe("normalizes profile names", () => {
Expand All @@ -38,6 +43,13 @@ describe(getConfigData.name, () => {
{}
);

it("profile containing CONFIG_PREFIX_SEPARATOR", () => {
const profileName = ["foo", "bar"].join(CONFIG_PREFIX_SEPARATOR);
const mockOutput = getMockOutput([profileName]);
const mockInput = getMockInput(mockOutput);
expect(getConfigData(mockInput)).toStrictEqual(mockOutput);
});

it("single profile", () => {
const mockOutput = getMockOutput(["one"]);
const mockInput = getMockInput(mockOutput);
Expand Down
17 changes: 10 additions & 7 deletions packages/shared-ini-file-loader/src/getConfigData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,21 @@ import { CONFIG_PREFIX_SEPARATOR } from "./loadSharedConfigFiles";
*/
export const getConfigData = (data: ParsedIniData): ParsedIniData =>
Object.entries(data)
// filter out
.filter(([key]) => {
const sections = key.split(CONFIG_PREFIX_SEPARATOR);
if (sections.length === 2 && Object.values(IniSectionType).includes(sections[0] as IniSectionType)) {
return true;
const indexOfSeparator = key.indexOf(CONFIG_PREFIX_SEPARATOR);
if (indexOfSeparator === -1) {
// filter out keys which do not contain CONFIG_PREFIX_SEPARATOR.
return false;
}
return false;
// Check if prefix is a valid IniSectionType.
return Object.values(IniSectionType).includes(key.substring(0, indexOfSeparator) as IniSectionType);
})
// replace profile prefix, if present.
// remove profile prefix, if present.
.reduce(
(acc, [key, value]) => {
const updatedKey = key.startsWith(IniSectionType.PROFILE) ? key.split(CONFIG_PREFIX_SEPARATOR)[1] : key;
const indexOfSeparator = key.indexOf(CONFIG_PREFIX_SEPARATOR);
const updatedKey =
key.substring(0, indexOfSeparator) === IniSectionType.PROFILE ? key.substring(indexOfSeparator + 1) : key;
acc[updatedKey] = value;
return acc;
},
Expand Down

0 comments on commit 6884910

Please sign in to comment.