24 lines
1.2 KiB
JavaScript
24 lines
1.2 KiB
JavaScript
import { CredentialsProviderError } from "@smithy/property-provider";
|
|
import { getProfileName, loadSharedConfigFiles } from "@smithy/shared-ini-file-loader";
|
|
import { getSelectorName } from "./getSelectorName";
|
|
export const fromSharedConfigFiles = (configSelector, { preferredFile = "config", ...init } = {}) => async () => {
|
|
const profile = getProfileName(init);
|
|
const { configFile, credentialsFile } = await loadSharedConfigFiles(init);
|
|
const profileFromCredentials = credentialsFile[profile] || {};
|
|
const profileFromConfig = configFile[profile] || {};
|
|
const mergedProfile = preferredFile === "config"
|
|
? { ...profileFromCredentials, ...profileFromConfig }
|
|
: { ...profileFromConfig, ...profileFromCredentials };
|
|
try {
|
|
const cfgFile = preferredFile === "config" ? configFile : credentialsFile;
|
|
const configValue = configSelector(mergedProfile, cfgFile);
|
|
if (configValue === undefined) {
|
|
throw new Error();
|
|
}
|
|
return configValue;
|
|
}
|
|
catch (e) {
|
|
throw new CredentialsProviderError(e.message || `Not found in config files w/ profile [${profile}]: ${getSelectorName(configSelector.toString())}`, { logger: init.logger });
|
|
}
|
|
};
|