| | | 1 | | import { inject, injectable, optional } 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 { ExecWithSecretsCommand } from '../execWithSecrets/ExecWithSecretsCommand.js'; |
| | | 7 | | // Type-only: the GitHub Action bundle must not pull in exec mode. |
| | | 8 | | import type { ExecWithSecretsCommandHandler } from '../execWithSecrets/ExecWithSecretsCommandHandler.js'; |
| | | 9 | | import { PullSecretsToEnvCommand } from '../pullSecretsToEnv/PullSecretsToEnvCommand.js'; |
| | | 10 | | import type { PullSecretsToEnvCommandHandler } from '../pullSecretsToEnv/PullSecretsToEnvCommandHandler.js'; |
| | | 11 | | import { PushEnvToSecretsCommand } from '../pushEnvToSecrets/PushEnvToSecretsCommand.js'; |
| | | 12 | | import type { PushEnvToSecretsCommandHandler } from '../pushEnvToSecrets/PushEnvToSecretsCommandHandler.js'; |
| | | 13 | | import { PushSingleCommand } from '../pushSingle/PushSingleCommand.js'; |
| | | 14 | | import type { PushSingleCommandHandler } from '../pushSingle/PushSingleCommandHandler.js'; |
| | | 15 | | import type { DispatchActionCommand } from './DispatchActionCommand.js'; |
| | | 16 | | |
| | | 17 | | @injectable() |
| | 13 | 18 | | export class DispatchActionCommandHandler { |
| | | 19 | | constructor( |
| | | 20 | | @inject(TYPES.PullSecretsToEnvCommandHandler) |
| | 90 | 21 | | private readonly pullHandler: PullSecretsToEnvCommandHandler, |
| | | 22 | | @inject(TYPES.PushEnvToSecretsCommandHandler) |
| | 90 | 23 | | private readonly pushHandler: PushEnvToSecretsCommandHandler, |
| | | 24 | | @inject(TYPES.PushSingleCommandHandler) |
| | 90 | 25 | | private readonly pushSingleHandler: PushSingleCommandHandler, |
| | | 26 | | @inject(TYPES.ISecretProvider) |
| | 90 | 27 | | private readonly secretProvider: ISecretProvider, |
| | | 28 | | // Bound only by the CLI exec startup; absent in the GitHub Action. |
| | | 29 | | @inject(TYPES.ExecWithSecretsCommandHandler) |
| | | 30 | | @optional() |
| | 90 | 31 | | private readonly execHandler?: ExecWithSecretsCommandHandler, |
| | | 32 | | ) {} |
| | | 33 | | |
| | | 34 | | /** |
| | | 35 | | * Runs the command's mode. Returns the exit code of the child process in |
| | | 36 | | * exec mode, and `undefined` for every other mode. |
| | | 37 | | */ |
| | | 38 | | async handleCommand( |
| | | 39 | | command: DispatchActionCommand, |
| | | 40 | | ): Promise<number | undefined> { |
| | 67 | 41 | | if (command.mode === OperationMode.EXEC) { |
| | | 42 | | // No identity banner: exec prints a single summary line. |
| | 48 | 43 | | return this.handleExec(command); |
| | | 44 | | } |
| | | 45 | | |
| | 19 | 46 | | this.validateRequiredArguments(command); |
| | | 47 | | |
| | 19 | 48 | | if (typeof this.secretProvider.logIdentity === 'function') { |
| | 9 | 49 | | await this.secretProvider.logIdentity(); |
| | | 50 | | } |
| | 15 | 51 | | switch (command.mode) { |
| | | 52 | | case OperationMode.PUSH_SINGLE: |
| | 5 | 53 | | await this.handlePushSingle(command); |
| | 5 | 54 | | break; |
| | | 55 | | case OperationMode.PUSH_ENV_TO_SECRETS: |
| | 3 | 56 | | await this.handlePush(command); |
| | 3 | 57 | | break; |
| | | 58 | | case OperationMode.PULL_SECRETS_TO_ENV: |
| | 6 | 59 | | await this.handlePull(command); |
| | 6 | 60 | | break; |
| | | 61 | | default: |
| | 1 | 62 | | throw new InvalidArgumentError( |
| | | 63 | | `Unsupported operation mode: ${command.mode}`, |
| | | 64 | | ); |
| | | 65 | | } |
| | | 66 | | } |
| | | 67 | | |
| | | 68 | | private validateRequiredArguments(command: DispatchActionCommand): void { |
| | 19 | 69 | | if (command.mode === OperationMode.PUSH_SINGLE) { |
| | 5 | 70 | | this.validatePushSingleOptions(command); |
| | 5 | 71 | | return; |
| | | 72 | | } |
| | | 73 | | |
| | 14 | 74 | | this.validateMapAndEnvFileOptions(command); |
| | | 75 | | } |
| | | 76 | | |
| | | 77 | | private async handleExec(command: DispatchActionCommand): Promise<number> { |
| | 48 | 78 | | if (!command.map || !command.command) { |
| | 2 | 79 | | throw new InvalidArgumentError( |
| | | 80 | | 'Missing required arguments for exec: a map file and a command', |
| | | 81 | | ); |
| | | 82 | | } |
| | 46 | 83 | | if (this.execHandler === undefined) { |
| | 1 | 84 | | throw new InvalidArgumentError( |
| | | 85 | | 'exec mode is not available in this entry point', |
| | | 86 | | ); |
| | | 87 | | } |
| | | 88 | | |
| | 45 | 89 | | return this.execHandler.handle( |
| | | 90 | | ExecWithSecretsCommand.create( |
| | | 91 | | command.map, |
| | | 92 | | command.command, |
| | | 93 | | command.args, |
| | | 94 | | command.sourceLabel, |
| | | 95 | | ), |
| | | 96 | | ); |
| | | 97 | | } |
| | | 98 | | |
| | | 99 | | private async handlePushSingle( |
| | | 100 | | command: DispatchActionCommand, |
| | | 101 | | ): Promise<void> { |
| | 5 | 102 | | this.validatePushSingleOptions(command); |
| | 5 | 103 | | const pushSingleCommand = PushSingleCommand.create( |
| | | 104 | | command.key, |
| | | 105 | | command.value, |
| | | 106 | | command.secretPath, |
| | | 107 | | command.map, |
| | | 108 | | command.mapTarget, |
| | | 109 | | ); |
| | | 110 | | |
| | 5 | 111 | | await this.pushSingleHandler.handle(pushSingleCommand); |
| | | 112 | | } |
| | | 113 | | |
| | | 114 | | private async handlePush(command: DispatchActionCommand): Promise<void> { |
| | 3 | 115 | | this.validateMapAndEnvFileOptions(command); |
| | 3 | 116 | | const pushEnvToSecretsCommand = PushEnvToSecretsCommand.create( |
| | | 117 | | command.map, |
| | | 118 | | command.envfile, |
| | | 119 | | ); |
| | | 120 | | |
| | 3 | 121 | | await this.pushHandler.handle(pushEnvToSecretsCommand); |
| | | 122 | | } |
| | | 123 | | |
| | | 124 | | private async handlePull(command: DispatchActionCommand): Promise<void> { |
| | 6 | 125 | | this.validateMapAndEnvFileOptions(command); |
| | 6 | 126 | | const pullSecretsToEnvCommand = PullSecretsToEnvCommand.create( |
| | | 127 | | command.map, |
| | | 128 | | command.envfile, |
| | | 129 | | ); |
| | | 130 | | |
| | 6 | 131 | | await this.pullHandler.handle(pullSecretsToEnvCommand); |
| | | 132 | | } |
| | | 133 | | |
| | | 134 | | private validatePushSingleOptions( |
| | | 135 | | command: DispatchActionCommand, |
| | | 136 | | ): asserts command is DispatchActionCommand & { |
| | | 137 | | key: string; |
| | | 138 | | value: string; |
| | | 139 | | secretPath: string; |
| | | 140 | | } { |
| | 10 | 141 | | if (!command.key || !command.value || !command.secretPath) { |
| | 0 | 142 | | throw new InvalidArgumentError( |
| | | 143 | | 'Missing required arguments: --key, --value, and --secret-path', |
| | | 144 | | ); |
| | | 145 | | } |
| | | 146 | | } |
| | | 147 | | |
| | | 148 | | private validateMapAndEnvFileOptions( |
| | | 149 | | command: DispatchActionCommand, |
| | | 150 | | ): asserts command is DispatchActionCommand & { |
| | | 151 | | map: string; |
| | | 152 | | envfile: string; |
| | | 153 | | } { |
| | 23 | 154 | | if (!command.map || !command.envfile) { |
| | 4 | 155 | | throw new InvalidArgumentError( |
| | | 156 | | 'Missing required arguments: --map and --envfile', |
| | | 157 | | ); |
| | | 158 | | } |
| | | 159 | | } |
| | | 160 | | } |