Skip to content
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
61 changes: 46 additions & 15 deletions packages/configuration-loader/src/config-loader.ts
Original file line number Diff line number Diff line change
@@ -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<string, string>): 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.
*
Expand All @@ -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<AllProfilesFromFileParams>,
): Record<string, 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)

return Object.keys(configs).reduce(
(prev, pKey) => ({
...prev,
[pKey]: convertFileConfigToSDK(configs[pKey]),
}),
{} as Record<string, Profile>,
)
}

/**
* Loads profile from configuration file.
*
Expand All @@ -36,11 +79,7 @@ export const loadProfileFromEnvironmentValues = (): Profile => ({
export const loadProfileFromConfigurationFile = (
params?: Readonly<ProfileFromFileParams>,
): 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') {
Expand All @@ -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
}
7 changes: 6 additions & 1 deletion packages/configuration-loader/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
export {
loadAllProfilesFromConfigurationFile,
loadProfileFromConfigurationFile,
loadProfileFromEnvironmentValues,
} from './config-loader'
export type { Profile, ProfileFromFileParams } from './types'
export type {
AllProfilesFromFileParams,
Profile,
ProfileFromFileParams,
} from './types'
9 changes: 6 additions & 3 deletions packages/configuration-loader/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,19 @@ export type ConfigurationType = Record<
'default' | string,
Record<string, string>
>

/** 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.
*
* Defaults to the value of the `SCW_CONFIG_PATH` environment variable
* 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.
*
Expand Down