< Summary - Envilder CLI

Information
Class: src/envilder/core/application/validateEnvFile/CompareEnvFileValuesCommandHandler.ts
Assembly: Default
File(s): src/envilder/core/application/validateEnvFile/CompareEnvFileValuesCommandHandler.ts
Tag: 605_44106794472
Line coverage
95%
Covered lines: 20
Uncovered lines: 1
Coverable lines: 21
Total lines: 86
Line coverage: 95.2%
Branch coverage
83%
Covered branches: 10
Total branches: 12
Branch coverage: 83.3%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

File(s)

src/envilder/core/application/validateEnvFile/CompareEnvFileValuesCommandHandler.ts

#LineLine coverage
 1import { inject, injectable } from 'inversify';
 2import type { EnvValueCheck } from '../../domain/EnvFileComparison.js';
 3import { EnvironmentFileError } 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 {
 10  type SecretOutcome,
 11  SecretResolver,
 12} from '../secretResolution/SecretResolver.js';
 13import type { CompareEnvFileValuesCommand } from './CompareEnvFileValuesCommand.js';
 14
 15/**
 16 * Compares the local value of each requested variable with its remote value
 17 * and reports only `matches`, `differs` or `unreadable`. Remote values are
 18 * read through the same resolver pull and exec use, so they reach the secret
 19 * masker, and are dropped as soon as they are compared. Neither value ever
 20 * reaches the result. When a key repeats in the env file, the last assignment
 21 * is compared, as dotenv would load it.
 22 */
 23@injectable()
 924export class CompareEnvFileValuesCommandHandler {
 25  private readonly secretResolver: SecretResolver;
 26
 27  constructor(
 28    @inject(TYPES.ISecretProvider) secretProvider: ISecretProvider,
 29    @inject(TYPES.IVariableStore)
 1630    private readonly variableStore: IVariableStore,
 31    @inject(TYPES.ILogger) logger: ILogger,
 32    @inject(TYPES.ISecretMasker) secretMasker: ISecretMasker,
 33  ) {
 1634    this.secretResolver = new SecretResolver(
 35      secretProvider,
 36      logger,
 37      secretMasker,
 38    );
 39  }
 40
 41  async handle(command: CompareEnvFileValuesCommand): Promise<EnvValueCheck[]> {
 1642    const mapping = await this.variableStore.getMapping(command.mapPath);
 1643    const entries = await this.variableStore.readEnvironmentEntries(
 44      command.envFilePath,
 45    );
 1646    if (entries === null) {
 047      throw new EnvironmentFileError(
 48        `Env file not found: ${command.envFilePath}`,
 49      );
 50    }
 2351    const localValues = new Map(entries.map(({ key, value }) => [key, value]));
 1652    const requested = new Set(command.variables);
 1653    const selected = Object.fromEntries(
 2354      Object.entries(mapping).filter(([variable]) => requested.has(variable)),
 55    );
 56
 1657    const { outcomes } = await this.secretResolver.resolve(selected);
 1558    return outcomes.map((outcome) =>
 1859      toValueCheck(
 60        outcome,
 61        selected[outcome.envVar],
 62        localValues.get(outcome.envVar),
 63      ),
 64    );
 65  }
 66}
 67
 68function toValueCheck(
 69  outcome: SecretOutcome,
 70  path: string,
 71  localValue: string | undefined,
 72): EnvValueCheck {
 1873  const variable = outcome.envVar;
 1874  if (outcome.status === 'error') {
 175    return { variable, path, status: 'unreadable', reason: outcome.reason };
 76  }
 1777  if (outcome.status === 'warning' && outcome.reason === 'not-found') {
 178    return { variable, path, status: 'unreadable', reason: 'not found' };
 79  }
 1680  const remoteValue = outcome.status === 'resolved' ? outcome.value : '';
 1881  return {
 82    variable,
 83    path,
 84    status: localValue === remoteValue ? 'matches' : 'differs',
 85  };
 86}