| | | 1 | | import { inject, injectable } from 'inversify'; |
| | | 2 | | import { InvalidArgumentError } from '../../domain/errors/DomainErrors.js'; |
| | | 3 | | import { OperationMode } from '../../domain/OperationMode.js'; |
| | | 4 | | import type { ISecretProvider } from '../../domain/ports/ISecretProvider.js'; |
| | | 5 | | import { TYPES } from '../../types.js'; |
| | | 6 | | import { PullSecretsToEnvCommand } from '../pullSecretsToEnv/PullSecretsToEnvCommand.js'; |
| | | 7 | | import type { PullSecretsToEnvCommandHandler } from '../pullSecretsToEnv/PullSecretsToEnvCommandHandler.js'; |
| | | 8 | | import { PushEnvToSecretsCommand } from '../pushEnvToSecrets/PushEnvToSecretsCommand.js'; |
| | | 9 | | import type { PushEnvToSecretsCommandHandler } from '../pushEnvToSecrets/PushEnvToSecretsCommandHandler.js'; |
| | | 10 | | import { PushSingleCommand } from '../pushSingle/PushSingleCommand.js'; |
| | | 11 | | import type { PushSingleCommandHandler } from '../pushSingle/PushSingleCommandHandler.js'; |
| | | 12 | | import type { DispatchActionCommand } from './DispatchActionCommand.js'; |
| | | 13 | | |
| | | 14 | | @injectable() |
| | 8 | 15 | | export class DispatchActionCommandHandler { |
| | | 16 | | constructor( |
| | | 17 | | @inject(TYPES.PullSecretsToEnvCommandHandler) |
| | 22 | 18 | | private readonly pullHandler: PullSecretsToEnvCommandHandler, |
| | | 19 | | @inject(TYPES.PushEnvToSecretsCommandHandler) |
| | 22 | 20 | | private readonly pushHandler: PushEnvToSecretsCommandHandler, |
| | | 21 | | @inject(TYPES.PushSingleCommandHandler) |
| | 22 | 22 | | private readonly pushSingleHandler: PushSingleCommandHandler, |
| | | 23 | | @inject(TYPES.ISecretProvider) |
| | 22 | 24 | | private readonly secretProvider: ISecretProvider, |
| | | 25 | | ) {} |
| | | 26 | | |
| | | 27 | | async handleCommand(command: DispatchActionCommand): Promise<void> { |
| | 13 | 28 | | this.validateRequiredArguments(command); |
| | | 29 | | |
| | 13 | 30 | | if (typeof this.secretProvider.logIdentity === 'function') { |
| | 5 | 31 | | await this.secretProvider.logIdentity(); |
| | | 32 | | } |
| | 10 | 33 | | switch (command.mode) { |
| | | 34 | | case OperationMode.PUSH_SINGLE: |
| | 2 | 35 | | await this.handlePushSingle(command); |
| | 2 | 36 | | break; |
| | | 37 | | case OperationMode.PUSH_ENV_TO_SECRETS: |
| | 2 | 38 | | await this.handlePush(command); |
| | 2 | 39 | | break; |
| | | 40 | | case OperationMode.PULL_SECRETS_TO_ENV: |
| | 5 | 41 | | await this.handlePull(command); |
| | 5 | 42 | | break; |
| | | 43 | | default: |
| | 1 | 44 | | throw new InvalidArgumentError( |
| | | 45 | | `Unsupported operation mode: ${command.mode}`, |
| | | 46 | | ); |
| | | 47 | | } |
| | | 48 | | } |
| | | 49 | | |
| | | 50 | | private validateRequiredArguments(command: DispatchActionCommand): void { |
| | 13 | 51 | | if (command.mode === OperationMode.PUSH_SINGLE) { |
| | 2 | 52 | | this.validatePushSingleOptions(command); |
| | 2 | 53 | | return; |
| | | 54 | | } |
| | | 55 | | |
| | 11 | 56 | | this.validateMapAndEnvFileOptions(command); |
| | | 57 | | } |
| | | 58 | | |
| | | 59 | | private async handlePushSingle( |
| | | 60 | | command: DispatchActionCommand, |
| | | 61 | | ): Promise<void> { |
| | 2 | 62 | | this.validatePushSingleOptions(command); |
| | 2 | 63 | | const pushSingleCommand = PushSingleCommand.create( |
| | | 64 | | command.key, |
| | | 65 | | command.value, |
| | | 66 | | command.secretPath, |
| | | 67 | | ); |
| | | 68 | | |
| | 2 | 69 | | await this.pushSingleHandler.handle(pushSingleCommand); |
| | | 70 | | } |
| | | 71 | | |
| | | 72 | | private async handlePush(command: DispatchActionCommand): Promise<void> { |
| | 2 | 73 | | this.validateMapAndEnvFileOptions(command); |
| | 2 | 74 | | const pushEnvToSecretsCommand = PushEnvToSecretsCommand.create( |
| | | 75 | | command.map, |
| | | 76 | | command.envfile, |
| | | 77 | | ); |
| | | 78 | | |
| | 2 | 79 | | await this.pushHandler.handle(pushEnvToSecretsCommand); |
| | | 80 | | } |
| | | 81 | | |
| | | 82 | | private async handlePull(command: DispatchActionCommand): Promise<void> { |
| | 5 | 83 | | this.validateMapAndEnvFileOptions(command); |
| | 5 | 84 | | const pullSecretsToEnvCommand = PullSecretsToEnvCommand.create( |
| | | 85 | | command.map, |
| | | 86 | | command.envfile, |
| | | 87 | | ); |
| | | 88 | | |
| | 5 | 89 | | await this.pullHandler.handle(pullSecretsToEnvCommand); |
| | | 90 | | } |
| | | 91 | | |
| | | 92 | | private validatePushSingleOptions( |
| | | 93 | | command: DispatchActionCommand, |
| | | 94 | | ): asserts command is DispatchActionCommand & { |
| | | 95 | | key: string; |
| | | 96 | | value: string; |
| | | 97 | | secretPath: string; |
| | | 98 | | } { |
| | 4 | 99 | | if (!command.key || !command.value || !command.secretPath) { |
| | 0 | 100 | | throw new InvalidArgumentError( |
| | | 101 | | 'Missing required arguments: --key, --value, and --secret-path', |
| | | 102 | | ); |
| | | 103 | | } |
| | | 104 | | } |
| | | 105 | | |
| | | 106 | | private validateMapAndEnvFileOptions( |
| | | 107 | | command: DispatchActionCommand, |
| | | 108 | | ): asserts command is DispatchActionCommand & { |
| | | 109 | | map: string; |
| | | 110 | | envfile: string; |
| | | 111 | | } { |
| | 18 | 112 | | if (!command.map || !command.envfile) { |
| | 3 | 113 | | throw new InvalidArgumentError( |
| | | 114 | | 'Missing required arguments: --map and --envfile', |
| | | 115 | | ); |
| | | 116 | | } |
| | | 117 | | } |
| | | 118 | | } |