| | | 1 | | import type { Container } from 'inversify'; |
| | | 2 | | |
| | | 3 | | import { DispatchActionCommandHandler } from '../../core/application/dispatch/DispatchActionCommandHandler.js'; |
| | | 4 | | import { PullSecretsToEnvCommandHandler } from '../../core/application/pullSecretsToEnv/PullSecretsToEnvCommandHandler.j |
| | | 5 | | import { PushEnvToSecretsCommandHandler } from '../../core/application/pushEnvToSecrets/PushEnvToSecretsCommandHandler.j |
| | | 6 | | import { PushSingleCommandHandler } from '../../core/application/pushSingle/PushSingleCommandHandler.js'; |
| | | 7 | | import { InvalidArgumentError } from '../../core/domain/errors/DomainErrors.js'; |
| | | 8 | | import type { MapFileConfig } from '../../core/domain/MapFileConfig.js'; |
| | | 9 | | import type { ILogger } from '../../core/domain/ports/ILogger.js'; |
| | | 10 | | import type { ISecretProvider } from '../../core/domain/ports/ISecretProvider.js'; |
| | | 11 | | import type { IVariableStore } from '../../core/domain/ports/IVariableStore.js'; |
| | | 12 | | import { createAwsSecretProvider } from '../../core/infrastructure/aws/AwsSecretProviderFactory.js'; |
| | | 13 | | import { |
| | | 14 | | type AzureProviderOptions, |
| | | 15 | | createAzureSecretProvider, |
| | | 16 | | } from '../../core/infrastructure/azure/AzureSecretProviderFactory.js'; |
| | | 17 | | import { ConsoleLogger } from '../../core/infrastructure/logger/ConsoleLogger.js'; |
| | | 18 | | import { FileVariableStore } from '../../core/infrastructure/variableStore/FileVariableStore.js'; |
| | | 19 | | import { TYPES } from '../../core/types.js'; |
| | | 20 | | |
| | | 21 | | export type InfrastructureOptions = AzureProviderOptions; |
| | | 22 | | |
| | | 23 | | type ProviderFactory = ( |
| | | 24 | | config: MapFileConfig, |
| | | 25 | | options: InfrastructureOptions, |
| | | 26 | | ) => ISecretProvider; |
| | | 27 | | |
| | 7 | 28 | | const providerFactories: Record<string, ProviderFactory> = { |
| | 22 | 29 | | aws: (config) => createAwsSecretProvider(config), |
| | 17 | 30 | | azure: (config, options) => createAzureSecretProvider(config, options), |
| | | 31 | | }; |
| | | 32 | | |
| | | 33 | | export function configureInfrastructureServices( |
| | | 34 | | container: Container, |
| | | 35 | | config: MapFileConfig = {}, |
| | | 36 | | options: InfrastructureOptions = {}, |
| | | 37 | | ): void { |
| | 42 | 38 | | if (!container.isBound(TYPES.ILogger)) { |
| | 37 | 39 | | container.bind<ILogger>(TYPES.ILogger).to(ConsoleLogger).inSingletonScope(); |
| | | 40 | | } |
| | | 41 | | |
| | 42 | 42 | | if (!container.isBound(TYPES.IVariableStore)) { |
| | 41 | 43 | | container |
| | | 44 | | .bind<IVariableStore>(TYPES.IVariableStore) |
| | | 45 | | .to(FileVariableStore) |
| | | 46 | | .inSingletonScope(); |
| | | 47 | | } |
| | | 48 | | |
| | 42 | 49 | | const selectedProvider = config.provider?.toLowerCase() || 'aws'; |
| | | 50 | | |
| | 42 | 51 | | if (config.profile && selectedProvider !== 'aws') { |
| | 1 | 52 | | const logger = container.get<ILogger>(TYPES.ILogger); |
| | 1 | 53 | | logger.warn( |
| | | 54 | | `--profile is only supported with the aws provider` + |
| | | 55 | | ` and will be ignored` + |
| | | 56 | | ` (current provider: ${selectedProvider}).`, |
| | | 57 | | ); |
| | | 58 | | } |
| | | 59 | | |
| | 42 | 60 | | const factory = providerFactories[selectedProvider]; |
| | 42 | 61 | | if (!factory) { |
| | 3 | 62 | | throw new InvalidArgumentError( |
| | | 63 | | `Unsupported provider: ${config.provider}.` + |
| | | 64 | | ` Supported providers:` + |
| | | 65 | | ` ${Object.keys(providerFactories).join(', ')}`, |
| | | 66 | | ); |
| | | 67 | | } |
| | 39 | 68 | | const secretProvider = factory(config, options); |
| | | 69 | | |
| | 39 | 70 | | container |
| | | 71 | | .bind<ISecretProvider>(TYPES.ISecretProvider) |
| | | 72 | | .toConstantValue(secretProvider); |
| | | 73 | | } |
| | | 74 | | |
| | | 75 | | export function configureApplicationServices(container: Container): void { |
| | 38 | 76 | | container |
| | | 77 | | .bind<PullSecretsToEnvCommandHandler>(TYPES.PullSecretsToEnvCommandHandler) |
| | | 78 | | .to(PullSecretsToEnvCommandHandler) |
| | | 79 | | .inTransientScope(); |
| | | 80 | | |
| | 38 | 81 | | container |
| | | 82 | | .bind<PushEnvToSecretsCommandHandler>(TYPES.PushEnvToSecretsCommandHandler) |
| | | 83 | | .to(PushEnvToSecretsCommandHandler) |
| | | 84 | | .inTransientScope(); |
| | | 85 | | |
| | 38 | 86 | | container |
| | | 87 | | .bind<PushSingleCommandHandler>(TYPES.PushSingleCommandHandler) |
| | | 88 | | .to(PushSingleCommandHandler) |
| | | 89 | | .inTransientScope(); |
| | | 90 | | |
| | 38 | 91 | | container |
| | | 92 | | .bind<DispatchActionCommandHandler>(TYPES.DispatchActionCommandHandler) |
| | | 93 | | .to(DispatchActionCommandHandler) |
| | | 94 | | .inTransientScope(); |
| | | 95 | | } |