< Summary - Envilder CLI

Information
Class: src/envilder/core/application/pullSecretsToEnv/PullSecretsToEnvCommandHandler.ts
Assembly: Default
File(s): src/envilder/core/application/pullSecretsToEnv/PullSecretsToEnvCommandHandler.ts
Tag: 561_41292037308
Line coverage
100%
Covered lines: 20
Uncovered lines: 0
Coverable lines: 20
Total lines: 114
Line coverage: 100%
Branch coverage
100%
Covered branches: 2
Total branches: 2
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

File(s)

src/envilder/core/application/pullSecretsToEnv/PullSecretsToEnvCommandHandler.ts

#LineLine coverage
 1import { inject, injectable } from 'inversify';
 2import pc from 'picocolors';
 3import { SecretsFetchError } from '../../domain/errors/DomainErrors.js';
 4import type { ILogger } from '../../domain/ports/ILogger.js';
 5import type { ISecretMasker } from '../../domain/ports/ISecretMasker.js';
 6import type { ISecretProvider } from '../../domain/ports/ISecretProvider.js';
 7import type { IVariableStore } from '../../domain/ports/IVariableStore.js';
 8import { TYPES } from '../../types.js';
 9import { SecretResolver } from '../secretResolution/SecretResolver.js';
 10import type { PullSecretsToEnvCommand } from './PullSecretsToEnvCommand.js';
 11
 12@injectable()
 1213export class PullSecretsToEnvCommandHandler {
 14  private readonly secretResolver: SecretResolver;
 15
 16  constructor(
 17    @inject(TYPES.ISecretProvider)
 18    secretProvider: ISecretProvider,
 19    @inject(TYPES.IVariableStore)
 9320    private readonly variableStore: IVariableStore,
 9321    @inject(TYPES.ILogger) private readonly logger: ILogger,
 22    @inject(TYPES.ISecretMasker)
 23    secretMasker: ISecretMasker,
 24  ) {
 9325    this.secretResolver = new SecretResolver(
 26      secretProvider,
 27      logger,
 28      secretMasker,
 29    );
 30  }
 31
 32  /**
 33   * Handles the PullSecretsToEnvCommand which orchestrates the process of fetching
 34   * environment variable values from a secret store and writing them to a local environment file.
 35   *
 36   * @param command - The PullSecretsToEnvCommand containing mapPath and envFilePath
 37   */
 38  async handle(command: PullSecretsToEnvCommand): Promise<void> {
 39    const { requestVariables, currentVariables } =
 1740      await this.loadVariables(command);
 1641    const { variables, resolvedCount, totalCount } = await this.envild(
 42      requestVariables,
 43      currentVariables,
 44    );
 1145    await this.saveEnvFile(command.envFilePath, variables);
 46
 1147    this.logger.info(
 48      PullSecretsToEnvCommandHandler.buildSummary(
 49        resolvedCount,
 50        totalCount,
 51        command.envFilePath,
 52      ),
 53    );
 54  }
 55
 56  private async loadVariables(command: PullSecretsToEnvCommand): Promise<{
 57    requestVariables: Record<string, string>;
 58    currentVariables: Record<string, string>;
 59  }> {
 1760    const requestVariables = await this.variableStore.getMapping(
 61      command.mapPath,
 62    );
 1663    const currentVariables = await this.variableStore.getEnvironment(
 64      command.envFilePath,
 65    );
 66
 1667    return { requestVariables, currentVariables };
 68  }
 69
 70  private async saveEnvFile(
 71    envFilePath: string,
 72    variables: Record<string, string>,
 73  ): Promise<void> {
 1174    await this.variableStore.saveEnvironment(envFilePath, variables);
 75  }
 76
 77  private async envild(
 78    paramMap: Record<string, string>,
 79    existingEnvVariables: Record<string, string>,
 80  ): Promise<{
 81    variables: Record<string, string>;
 82    resolvedCount: number;
 83    totalCount: number;
 84  }> {
 85    const { resolved, warnings, errors } =
 1686      await this.secretResolver.resolve(paramMap);
 87
 1488    for (const outcome of resolved) {
 889      existingEnvVariables[outcome.envVar] = outcome.value;
 90    }
 91
 1492    this.secretResolver.logSecretsSection(resolved, warnings);
 93
 1494    if (errors.length > 0) {
 395      throw new SecretsFetchError(errors.map(SecretResolver.toFailure));
 96    }
 97
 1198    return {
 99      variables: existingEnvVariables,
 100      resolvedCount: resolved.length,
 101      totalCount: Object.keys(paramMap).length,
 102    };
 103  }
 104
 105  private static buildSummary(
 106    resolvedCount: number,
 107    totalCount: number,
 108    envFilePath: string,
 109  ): string {
 11110    return `\n${pc.bold(pc.green('\u2B50 LEVEL CLEARED'))}${pc.dim(
 111      `  \u2014  ${resolvedCount}/${totalCount} secrets loaded \u00B7 `,
 112    )}${pc.bold(envFilePath)}${pc.dim(' written')}\n`;
 113  }
 114}