| | | 1 | | import { inject, injectable } from 'inversify'; |
| | | 2 | | import { InvalidArgumentError } from '../../domain/errors/DomainErrors.js'; |
| | | 3 | | import { OperationMode } from '../../domain/OperationMode.js'; |
| | | 4 | | import { TYPES } from '../../types.js'; |
| | | 5 | | import { PullSecretsToEnvCommand } from '../pullSecretsToEnv/PullSecretsToEnvCommand.js'; |
| | | 6 | | import type { PullSecretsToEnvCommandHandler } from '../pullSecretsToEnv/PullSecretsToEnvCommandHandler.js'; |
| | | 7 | | import { PushEnvToSecretsCommand } from '../pushEnvToSecrets/PushEnvToSecretsCommand.js'; |
| | | 8 | | import type { PushEnvToSecretsCommandHandler } from '../pushEnvToSecrets/PushEnvToSecretsCommandHandler.js'; |
| | | 9 | | import { PushSingleCommand } from '../pushSingle/PushSingleCommand.js'; |
| | | 10 | | import type { PushSingleCommandHandler } from '../pushSingle/PushSingleCommandHandler.js'; |
| | | 11 | | import type { DispatchActionCommand } from './DispatchActionCommand.js'; |
| | | 12 | | |
| | | 13 | | @injectable() |
| | 8 | 14 | | export class DispatchActionCommandHandler { |
| | | 15 | | constructor( |
| | | 16 | | @inject(TYPES.PullSecretsToEnvCommandHandler) |
| | 20 | 17 | | private readonly pullHandler: PullSecretsToEnvCommandHandler, |
| | | 18 | | @inject(TYPES.PushEnvToSecretsCommandHandler) |
| | 20 | 19 | | private readonly pushHandler: PushEnvToSecretsCommandHandler, |
| | | 20 | | @inject(TYPES.PushSingleCommandHandler) |
| | 20 | 21 | | private readonly pushSingleHandler: PushSingleCommandHandler, |
| | | 22 | | ) {} |
| | | 23 | | |
| | | 24 | | async handleCommand(command: DispatchActionCommand): Promise<void> { |
| | 10 | 25 | | switch (command.mode) { |
| | | 26 | | case OperationMode.PUSH_SINGLE: |
| | 2 | 27 | | await this.handlePushSingle(command); |
| | 2 | 28 | | break; |
| | | 29 | | case OperationMode.PUSH_ENV_TO_SECRETS: |
| | 2 | 30 | | await this.handlePush(command); |
| | 2 | 31 | | break; |
| | | 32 | | case OperationMode.PULL_SECRETS_TO_ENV: |
| | 6 | 33 | | await this.handlePull(command); |
| | 4 | 34 | | break; |
| | | 35 | | default: |
| | 0 | 36 | | await this.handlePull(command); |
| | 0 | 37 | | break; |
| | | 38 | | } |
| | | 39 | | } |
| | | 40 | | |
| | | 41 | | private async handlePushSingle( |
| | | 42 | | command: DispatchActionCommand, |
| | | 43 | | ): Promise<void> { |
| | 2 | 44 | | if (!command.key || !command.value || !command.secretPath) { |
| | 0 | 45 | | throw new InvalidArgumentError( |
| | | 46 | | 'Missing required arguments: --key, --value, and --secret-path', |
| | | 47 | | ); |
| | | 48 | | } |
| | | 49 | | |
| | 2 | 50 | | const pushSingleCommand = PushSingleCommand.create( |
| | | 51 | | command.key, |
| | | 52 | | command.value, |
| | | 53 | | command.secretPath, |
| | | 54 | | ); |
| | | 55 | | |
| | 2 | 56 | | await this.pushSingleHandler.handle(pushSingleCommand); |
| | | 57 | | } |
| | | 58 | | |
| | | 59 | | private async handlePush(command: DispatchActionCommand): Promise<void> { |
| | 2 | 60 | | this.validateMapAndEnvFileOptions(command); |
| | | 61 | | |
| | 2 | 62 | | const pushEnvToSecretsCommand = PushEnvToSecretsCommand.create( |
| | | 63 | | command.map as string, |
| | | 64 | | command.envfile as string, |
| | | 65 | | ); |
| | | 66 | | |
| | 2 | 67 | | await this.pushHandler.handle(pushEnvToSecretsCommand); |
| | | 68 | | } |
| | | 69 | | |
| | | 70 | | private async handlePull(command: DispatchActionCommand): Promise<void> { |
| | 6 | 71 | | this.validateMapAndEnvFileOptions(command); |
| | | 72 | | |
| | 6 | 73 | | const pullSecretsToEnvCommand = PullSecretsToEnvCommand.create( |
| | | 74 | | command.map as string, |
| | | 75 | | command.envfile as string, |
| | | 76 | | ); |
| | | 77 | | |
| | 6 | 78 | | await this.pullHandler.handle(pullSecretsToEnvCommand); |
| | | 79 | | } |
| | | 80 | | |
| | | 81 | | private validateMapAndEnvFileOptions(command: DispatchActionCommand): void { |
| | 8 | 82 | | if (!command.map || !command.envfile) { |
| | 2 | 83 | | throw new InvalidArgumentError( |
| | | 84 | | 'Missing required arguments: --map and --envfile', |
| | | 85 | | ); |
| | | 86 | | } |
| | | 87 | | } |
| | | 88 | | } |