| | | 1 | | import { inject, injectable } from 'inversify'; |
| | | 2 | | import pc from 'picocolors'; |
| | | 3 | | import { EnvironmentVariable } from '../../domain/EnvironmentVariable.js'; |
| | | 4 | | import type { ILogger } from '../../domain/ports/ILogger.js'; |
| | | 5 | | import type { ISecretProvider } from '../../domain/ports/ISecretProvider.js'; |
| | | 6 | | import { TYPES } from '../../types.js'; |
| | | 7 | | import type { PushSingleCommand } from './PushSingleCommand.js'; |
| | | 8 | | |
| | | 9 | | @injectable() |
| | 8 | 10 | | export class PushSingleCommandHandler { |
| | 8 | 11 | | private static readonly RULE = pc.magenta('\u2501'.repeat(60)); |
| | | 12 | | |
| | | 13 | | constructor( |
| | | 14 | | @inject(TYPES.ISecretProvider) |
| | 25 | 15 | | private readonly secretProvider: ISecretProvider, |
| | 25 | 16 | | @inject(TYPES.ILogger) private readonly logger: ILogger, |
| | | 17 | | ) {} |
| | | 18 | | |
| | | 19 | | /** |
| | | 20 | | * Handles the PushSingleCommand which pushes a single environment variable to the secret store. |
| | | 21 | | * |
| | | 22 | | * @param command - The PushSingleCommand containing key, value and secretPath |
| | | 23 | | */ |
| | | 24 | | async handle(command: PushSingleCommand): Promise<void> { |
| | 5 | 25 | | try { |
| | 5 | 26 | | const maskedPath = EnvironmentVariable.maskSecretPath(command.secretPath); |
| | | 27 | | |
| | 5 | 28 | | this.logger.info(`\n${pc.bold(pc.magenta('\u{1F4E4} PUSHING SECRET'))}`); |
| | 5 | 29 | | this.logger.info(PushSingleCommandHandler.RULE); |
| | 5 | 30 | | this.logger.info( |
| | | 31 | | ` ${pc.dim('\u2192 ')}${pc.bold(command.key)}${pc.dim(' \u2192 ')}${pc.dim(maskedPath)}`, |
| | | 32 | | ); |
| | | 33 | | |
| | 5 | 34 | | const envVariable = new EnvironmentVariable( |
| | | 35 | | command.key, |
| | | 36 | | command.value, |
| | | 37 | | true, |
| | | 38 | | ); |
| | | 39 | | |
| | 5 | 40 | | await this.secretProvider.setSecret(command.secretPath, command.value); |
| | 4 | 41 | | this.logger.info( |
| | | 42 | | `\n${pc.bold(pc.green('\u2B50 SECRET PUSHED'))}${pc.dim( |
| | | 43 | | ' \u2014 ', |
| | | 44 | | )}${pc.bold(command.key)}${pc.dim('=')}${envVariable.maskedValue}${pc.dim( |
| | | 45 | | ' \u2192 ', |
| | | 46 | | )}${pc.dim(maskedPath)}\n`, |
| | | 47 | | ); |
| | | 48 | | } catch (error) { |
| | | 49 | | const errorMessage = |
| | 1 | 50 | | error instanceof Error ? error.message : String(error); |
| | 1 | 51 | | this.logger.error( |
| | | 52 | | `Failed to push variable to secret store: ${errorMessage}`, |
| | | 53 | | ); |
| | 1 | 54 | | throw error; |
| | | 55 | | } |
| | | 56 | | } |
| | | 57 | | } |