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

Process config files for profile names containing prefix separator #1100

Merged
merged 6 commits into from
Dec 6, 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
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
Loading