< Summary - Envilder CLI

Information
Class: src/envilder/core/application/dispatch/DispatchActionCommandHandler.ts
Assembly: Default
File(s): src/envilder/core/application/dispatch/DispatchActionCommandHandler.ts
Tag: 427_29134414720
Line coverage
96%
Covered lines: 32
Uncovered lines: 1
Coverable lines: 33
Total lines: 118
Line coverage: 96.9%
Branch coverage
94%
Covered branches: 16
Total branches: 17
Branch coverage: 94.1%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

File(s)

src/envilder/core/application/dispatch/DispatchActionCommandHandler.ts

#LineLine coverage
 1import { inject, injectable } from 'inversify';
 2import { InvalidArgumentError } from '../../domain/errors/DomainErrors.js';
 3import { OperationMode } from '../../domain/OperationMode.js';
 4import type { ISecretProvider } from '../../domain/ports/ISecretProvider.js';
 5import { TYPES } from '../../types.js';
 6import { PullSecretsToEnvCommand } from '../pullSecretsToEnv/PullSecretsToEnvCommand.js';
 7import type { PullSecretsToEnvCommandHandler } from '../pullSecretsToEnv/PullSecretsToEnvCommandHandler.js';
 8import { PushEnvToSecretsCommand } from '../pushEnvToSecrets/PushEnvToSecretsCommand.js';
 9import type { PushEnvToSecretsCommandHandler } from '../pushEnvToSecrets/PushEnvToSecretsCommandHandler.js';
 10import { PushSingleCommand } from '../pushSingle/PushSingleCommand.js';
 11import type { PushSingleCommandHandler } from '../pushSingle/PushSingleCommandHandler.js';
 12import type { DispatchActionCommand } from './DispatchActionCommand.js';
 13
 14@injectable()
 815export class DispatchActionCommandHandler {
 16  constructor(
 17    @inject(TYPES.PullSecretsToEnvCommandHandler)
 2218    private readonly pullHandler: PullSecretsToEnvCommandHandler,
 19    @inject(TYPES.PushEnvToSecretsCommandHandler)
 2220    private readonly pushHandler: PushEnvToSecretsCommandHandler,
 21    @inject(TYPES.PushSingleCommandHandler)
 2222    private readonly pushSingleHandler: PushSingleCommandHandler,
 23    @inject(TYPES.ISecretProvider)
 2224    private readonly secretProvider: ISecretProvider,
 25  ) {}
 26
 27  async handleCommand(command: DispatchActionCommand): Promise<void> {
 1328    this.validateRequiredArguments(command);
 29
 1330    if (typeof this.secretProvider.logIdentity === 'function') {
 531      await this.secretProvider.logIdentity();
 32    }
 1033    switch (command.mode) {
 34      case OperationMode.PUSH_SINGLE:
 235        await this.handlePushSingle(command);
 236        break;
 37      case OperationMode.PUSH_ENV_TO_SECRETS:
 238        await this.handlePush(command);
 239        break;
 40      case OperationMode.PULL_SECRETS_TO_ENV:
 541        await this.handlePull(command);
 542        break;
 43      default:
 144        throw new InvalidArgumentError(
 45          `Unsupported operation mode: ${command.mode}`,
 46        );
 47    }
 48  }
 49
 50  private validateRequiredArguments(command: DispatchActionCommand): void {
 1351    if (command.mode === OperationMode.PUSH_SINGLE) {
 252      this.validatePushSingleOptions(command);
 253      return;
 54    }
 55
 1156    this.validateMapAndEnvFileOptions(command);
 57  }
 58
 59  private async handlePushSingle(
 60    command: DispatchActionCommand,
 61  ): Promise<void> {
 262    this.validatePushSingleOptions(command);
 263    const pushSingleCommand = PushSingleCommand.create(
 64      command.key,
 65      command.value,
 66      command.secretPath,
 67    );
 68
 269    await this.pushSingleHandler.handle(pushSingleCommand);
 70  }
 71
 72  private async handlePush(command: DispatchActionCommand): Promise<void> {
 273    this.validateMapAndEnvFileOptions(command);
 274    const pushEnvToSecretsCommand = PushEnvToSecretsCommand.create(
 75      command.map,
 76      command.envfile,
 77    );
 78
 279    await this.pushHandler.handle(pushEnvToSecretsCommand);
 80  }
 81
 82  private async handlePull(command: DispatchActionCommand): Promise<void> {
 583    this.validateMapAndEnvFileOptions(command);
 584    const pullSecretsToEnvCommand = PullSecretsToEnvCommand.create(
 85      command.map,
 86      command.envfile,
 87    );
 88
 589    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  } {
 499    if (!command.key || !command.value || !command.secretPath) {
 0100      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  } {
 18112    if (!command.map || !command.envfile) {
 3113      throw new InvalidArgumentError(
 114        'Missing required arguments: --map and --envfile',
 115      );
 116    }
 117  }
 118}