diff --git a/packages/configuration-loader/src/config-loader.ts b/packages/configuration-loader/src/config-loader.ts index 6013a87bf..a44d15215 100644 --- a/packages/configuration-loader/src/config-loader.ts +++ b/packages/configuration-loader/src/config-loader.ts @@ -1,9 +1,23 @@ import { env } from 'process' import { EnvironmentKey } from './env' import { resolveConfigurationFilePath } from './path-resolver' -import type { Profile, ProfileFromFileParams } from './types' +import type { + AllProfilesFromFileParams, + Profile, + ProfileFromFileParams, +} from './types' import { loadConfigurationFromFile } from './yml-loader' +const convertFileConfigToSDK = (obj: Record): Profile => ({ + accessKey: obj.access_key, + apiURL: obj.api_url, + defaultOrganizationId: obj.default_organization_id, + defaultProjectId: obj.default_project_id, + defaultRegion: obj.default_region, + defaultZone: obj.default_zone, + secretKey: obj.secret_key, +}) + /** * Loads profile from environment values. * @@ -21,6 +35,35 @@ export const loadProfileFromEnvironmentValues = (): Profile => ({ secretKey: env[EnvironmentKey.ScwSecretKey], }) +/** + * Loads all the profiles from configuration file. + * + * @param params - The parameters to load the profile + * @returns The profiles filled with values found in the configuration profile + * + * @throws Error + * Thrown if the configuration file couldn't be found. + * + * @public + */ +export const loadAllProfilesFromConfigurationFile = ( + params?: Readonly, +): Record => { + const filePath = params?.filepath ?? resolveConfigurationFilePath() + if (typeof filePath !== 'string' || filePath.length === 0) { + throw new Error('Could not find the path to the configuration file.') + } + const configs = loadConfigurationFromFile(filePath) + + return Object.keys(configs).reduce( + (prev, pKey) => ({ + ...prev, + [pKey]: convertFileConfigToSDK(configs[pKey]), + }), + {} as Record, + ) +} + /** * Loads profile from configuration file. * @@ -36,11 +79,7 @@ export const loadProfileFromEnvironmentValues = (): Profile => ({ export const loadProfileFromConfigurationFile = ( params?: Readonly, ): Profile => { - const filePath = params?.filepath ?? resolveConfigurationFilePath() - if (typeof filePath !== 'string' || filePath.length === 0) { - throw new Error('Could not find the path to the configuration file.') - } - const configs = loadConfigurationFromFile(filePath) + const configs = loadAllProfilesFromConfigurationFile(params) const profileName = params?.profileName ?? 'default' const profileMap = configs[profileName] if (typeof profileMap !== 'object') { @@ -49,13 +88,5 @@ export const loadProfileFromConfigurationFile = ( ) } - return { - accessKey: profileMap.access_key, - apiURL: profileMap.api_url, - defaultOrganizationId: profileMap.default_organization_id, - defaultProjectId: profileMap.default_project_id, - defaultRegion: profileMap.default_region, - defaultZone: profileMap.default_zone, - secretKey: profileMap.secret_key, - } + return profileMap } diff --git a/packages/configuration-loader/src/index.ts b/packages/configuration-loader/src/index.ts index b8c4c3b71..6e64907ce 100644 --- a/packages/configuration-loader/src/index.ts +++ b/packages/configuration-loader/src/index.ts @@ -1,5 +1,10 @@ export { + loadAllProfilesFromConfigurationFile, loadProfileFromConfigurationFile, loadProfileFromEnvironmentValues, } from './config-loader' -export type { Profile, ProfileFromFileParams } from './types' +export type { + AllProfilesFromFileParams, + Profile, + ProfileFromFileParams, +} from './types' diff --git a/packages/configuration-loader/src/types.ts b/packages/configuration-loader/src/types.ts index 9487e0617..cca00747c 100644 --- a/packages/configuration-loader/src/types.ts +++ b/packages/configuration-loader/src/types.ts @@ -50,9 +50,8 @@ export type ConfigurationType = Record< 'default' | string, Record > - -/** Parameters to load the profile from the configuration file */ -export type ProfileFromFileParams = { +/** Parameters to load the all the profiles from the configuration file */ +export type AllProfilesFromFileParams = { /** * The path at which to locate the configuration file. * @@ -60,6 +59,10 @@ export type ProfileFromFileParams = { * or `~/.scw/config` otherwise. */ filepath?: string +} + +/** Parameters to load the profile from the configuration file */ +export type ProfileFromFileParams = AllProfilesFromFileParams & { /** * Name of the profile to load. *