| | | 1 | | import 'reflect-metadata'; |
| | | 2 | | import type { Container } from 'inversify'; |
| | | 3 | | import { DispatchActionCommand } from '../../core/application/dispatch/DispatchActionCommand.js'; |
| | | 4 | | import type { DispatchActionCommandHandler } from '../../core/application/dispatch/DispatchActionCommandHandler.js'; |
| | | 5 | | import type { CliOptions } from '../../core/domain/CliOptions.js'; |
| | | 6 | | import type { MapFileConfig } from '../../core/domain/MapFileConfig.js'; |
| | | 7 | | import type { ILogger } from '../../core/domain/ports/ILogger.js'; |
| | | 8 | | import { ConsoleLogger } from '../../core/infrastructure/logger/ConsoleLogger.js'; |
| | | 9 | | import { readMapFileConfig } from '../../core/infrastructure/variableStore/FileVariableStore.js'; |
| | | 10 | | import { TYPES } from '../../core/types.js'; |
| | | 11 | | import { Startup } from './Startup.js'; |
| | | 12 | | |
| | | 13 | | /** |
| | | 14 | | * Reads GitHub Actions inputs from environment variables. |
| | | 15 | | * GitHub Actions passes inputs as INPUT_<NAME> environment variables. |
| | | 16 | | */ |
| | | 17 | | function readInputs(): { |
| | | 18 | | options: CliOptions; |
| | | 19 | | provider?: string; |
| | | 20 | | vaultUrl?: string; |
| | | 21 | | } { |
| | 11 | 22 | | const mapFile = process.env.INPUT_MAP_FILE; |
| | 11 | 23 | | const envFile = process.env.INPUT_ENV_FILE; |
| | 11 | 24 | | const provider = process.env.INPUT_PROVIDER; |
| | 11 | 25 | | const vaultUrl = process.env.INPUT_VAULT_URL; |
| | | 26 | | |
| | 11 | 27 | | return { |
| | | 28 | | options: { |
| | | 29 | | map: mapFile, |
| | | 30 | | envfile: envFile, |
| | | 31 | | // GitHub Action only supports pull mode |
| | | 32 | | push: false, |
| | | 33 | | }, |
| | | 34 | | provider: provider || undefined, |
| | | 35 | | vaultUrl: vaultUrl || undefined, |
| | | 36 | | }; |
| | | 37 | | } |
| | | 38 | | |
| | | 39 | | async function executeCommand( |
| | | 40 | | serviceProvider: Container, |
| | | 41 | | options: CliOptions, |
| | | 42 | | ): Promise<void> { |
| | 7 | 43 | | const commandHandler = serviceProvider.get<DispatchActionCommandHandler>( |
| | | 44 | | TYPES.DispatchActionCommandHandler, |
| | | 45 | | ); |
| | | 46 | | |
| | 7 | 47 | | const command = DispatchActionCommand.fromCliOptions(options); |
| | 7 | 48 | | await commandHandler.handleCommand(command); |
| | | 49 | | } |
| | | 50 | | |
| | | 51 | | export async function main() { |
| | 11 | 52 | | const { options, provider, vaultUrl } = readInputs(); |
| | | 53 | | |
| | | 54 | | let serviceProvider: Container; |
| | 11 | 55 | | let logger: ILogger = new ConsoleLogger(); |
| | | 56 | | |
| | 11 | 57 | | try { |
| | 11 | 58 | | const fileConfig = options.map ? await readMapFileConfig(options.map) : {}; |
| | | 59 | | |
| | 11 | 60 | | const config: MapFileConfig = { |
| | | 61 | | ...fileConfig, |
| | | 62 | | ...(provider && { provider }), |
| | | 63 | | ...(vaultUrl && { vaultUrl }), |
| | | 64 | | }; |
| | | 65 | | |
| | 11 | 66 | | const startup = Startup.build(); |
| | 11 | 67 | | startup.configureServices().configureInfrastructure(config); |
| | 11 | 68 | | serviceProvider = startup.create(); |
| | 11 | 69 | | logger = serviceProvider.get<ILogger>(TYPES.ILogger); |
| | | 70 | | } catch (error) { |
| | 1 | 71 | | const message = error instanceof Error ? error.message : String(error); |
| | 1 | 72 | | logger.error(`🚨 Failed to initialize: ${message}`); |
| | 1 | 73 | | throw error; |
| | | 74 | | } |
| | | 75 | | |
| | 10 | 76 | | try { |
| | | 77 | | // Validate required inputs |
| | 10 | 78 | | if (!options.map || !options.envfile) { |
| | 3 | 79 | | throw new Error( |
| | | 80 | | '🚨 Missing required inputs! Please provide map-file and env-file.', |
| | | 81 | | ); |
| | | 82 | | } |
| | | 83 | | |
| | 7 | 84 | | logger.info('🔑 Envilder GitHub Action - Starting secret pull...'); |
| | 7 | 85 | | logger.info(`📋 Map file: ${options.map}`); |
| | 7 | 86 | | logger.info(`📄 Env file: ${options.envfile}`); |
| | | 87 | | |
| | 7 | 88 | | await executeCommand(serviceProvider, options); |
| | | 89 | | |
| | 7 | 90 | | logger.info('✅ Secrets pulled successfully!'); |
| | | 91 | | } catch (error) { |
| | 3 | 92 | | logger.error('🚨 Uh-oh! Looks like Mario fell into the wrong pipe! 🍄💥'); |
| | 3 | 93 | | logger.error(error instanceof Error ? error.message : String(error)); |
| | 3 | 94 | | throw error; |
| | | 95 | | } |
| | | 96 | | } |