11import { env } from 'process'
22import { EnvironmentKey } from './env'
33import { resolveConfigurationFilePath } from './path-resolver'
4- import type { Profile , ProfileFromFileParams } from './types'
4+ import type {
5+ AllProfilesFromFileParams ,
6+ Profile ,
7+ ProfileFromFileParams ,
8+ } from './types'
59import { 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 => ({
3679export 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}
0 commit comments