Skip to content

Commit f58b62c

Browse files
authored
feat(configuration-loader): list all configurations at once (#336)
1 parent f1b8fe4 commit f58b62c

File tree

3 files changed

+58
-19
lines changed

3 files changed

+58
-19
lines changed

packages/configuration-loader/src/config-loader.ts

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,23 @@
11
import { env } from 'process'
22
import { EnvironmentKey } from './env'
33
import { resolveConfigurationFilePath } from './path-resolver'
4-
import type { Profile, ProfileFromFileParams } from './types'
4+
import type {
5+
AllProfilesFromFileParams,
6+
Profile,
7+
ProfileFromFileParams,
8+
} from './types'
59
import { loadConfigurationFromFile } from './yml-loader'
610

11+
const convertFileConfigToSDK = (obj: Record<string, string>): Profile => ({
12+
accessKey: obj.access_key,
13+
apiURL: obj.api_url,
14+
defaultOrganizationId: obj.default_organization_id,
15+
defaultProjectId: obj.default_project_id,
16+
defaultRegion: obj.default_region,
17+
defaultZone: obj.default_zone,
18+
secretKey: obj.secret_key,
19+
})
20+
721
/**
822
* Loads profile from environment values.
923
*
@@ -21,6 +35,35 @@ export const loadProfileFromEnvironmentValues = (): Profile => ({
2135
secretKey: env[EnvironmentKey.ScwSecretKey],
2236
})
2337

38+
/**
39+
* Loads all the profiles from configuration file.
40+
*
41+
* @param params - The parameters to load the profile
42+
* @returns The profiles filled with values found in the configuration profile
43+
*
44+
* @throws Error
45+
* Thrown if the configuration file couldn't be found.
46+
*
47+
* @public
48+
*/
49+
export const loadAllProfilesFromConfigurationFile = (
50+
params?: Readonly<AllProfilesFromFileParams>,
51+
): Record<string, Profile> => {
52+
const filePath = params?.filepath ?? resolveConfigurationFilePath()
53+
if (typeof filePath !== 'string' || filePath.length === 0) {
54+
throw new Error('Could not find the path to the configuration file.')
55+
}
56+
const configs = loadConfigurationFromFile(filePath)
57+
58+
return Object.keys(configs).reduce(
59+
(prev, pKey) => ({
60+
...prev,
61+
[pKey]: convertFileConfigToSDK(configs[pKey]),
62+
}),
63+
{} as Record<string, Profile>,
64+
)
65+
}
66+
2467
/**
2568
* Loads profile from configuration file.
2669
*
@@ -36,11 +79,7 @@ export const loadProfileFromEnvironmentValues = (): Profile => ({
3679
export const loadProfileFromConfigurationFile = (
3780
params?: Readonly<ProfileFromFileParams>,
3881
): Profile => {
39-
const filePath = params?.filepath ?? resolveConfigurationFilePath()
40-
if (typeof filePath !== 'string' || filePath.length === 0) {
41-
throw new Error('Could not find the path to the configuration file.')
42-
}
43-
const configs = loadConfigurationFromFile(filePath)
82+
const configs = loadAllProfilesFromConfigurationFile(params)
4483
const profileName = params?.profileName ?? 'default'
4584
const profileMap = configs[profileName]
4685
if (typeof profileMap !== 'object') {
@@ -49,13 +88,5 @@ export const loadProfileFromConfigurationFile = (
4988
)
5089
}
5190

52-
return {
53-
accessKey: profileMap.access_key,
54-
apiURL: profileMap.api_url,
55-
defaultOrganizationId: profileMap.default_organization_id,
56-
defaultProjectId: profileMap.default_project_id,
57-
defaultRegion: profileMap.default_region,
58-
defaultZone: profileMap.default_zone,
59-
secretKey: profileMap.secret_key,
60-
}
91+
return profileMap
6192
}
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
export {
2+
loadAllProfilesFromConfigurationFile,
23
loadProfileFromConfigurationFile,
34
loadProfileFromEnvironmentValues,
45
} from './config-loader'
5-
export type { Profile, ProfileFromFileParams } from './types'
6+
export type {
7+
AllProfilesFromFileParams,
8+
Profile,
9+
ProfileFromFileParams,
10+
} from './types'

packages/configuration-loader/src/types.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,19 @@ export type ConfigurationType = Record<
5050
'default' | string,
5151
Record<string, string>
5252
>
53-
54-
/** Parameters to load the profile from the configuration file */
55-
export type ProfileFromFileParams = {
53+
/** Parameters to load the all the profiles from the configuration file */
54+
export type AllProfilesFromFileParams = {
5655
/**
5756
* The path at which to locate the configuration file.
5857
*
5958
* Defaults to the value of the `SCW_CONFIG_PATH` environment variable
6059
* or `~/.scw/config` otherwise.
6160
*/
6261
filepath?: string
62+
}
63+
64+
/** Parameters to load the profile from the configuration file */
65+
export type ProfileFromFileParams = AllProfilesFromFileParams & {
6366
/**
6467
* Name of the profile to load.
6568
*

0 commit comments

Comments
 (0)