| | | 1 | | import type { CliOptions } from '../../domain/CliOptions.js'; |
| | | 2 | | import type { MapFileTarget } from '../../domain/MapFileUpdate.js'; |
| | | 3 | | import { OperationMode } from '../../domain/OperationMode.js'; |
| | | 4 | | |
| | | 5 | | export class DispatchActionCommand { |
| | | 6 | | constructor( |
| | 98 | 7 | | public readonly map?: string, |
| | 98 | 8 | | public readonly envfile?: string, |
| | 98 | 9 | | public readonly key?: string, |
| | 98 | 10 | | public readonly value?: string, |
| | 98 | 11 | | public readonly secretPath?: string, |
| | 98 | 12 | | public readonly profile?: string, |
| | 98 | 13 | | public readonly mode: OperationMode = OperationMode.PULL_SECRETS_TO_ENV, |
| | | 14 | | /** Exec mode: the program to run. */ |
| | 98 | 15 | | public readonly command?: string, |
| | | 16 | | /** Exec mode: arguments passed to the program verbatim. */ |
| | 98 | 17 | | public readonly args: readonly string[] = [], |
| | | 18 | | /** Exec mode: where secrets come from, shown in the summary line. */ |
| | 98 | 19 | | public readonly sourceLabel?: string, |
| | | 20 | | /** Single-secret push: where the secret goes, for the map file update. */ |
| | 98 | 21 | | public readonly mapTarget?: MapFileTarget, |
| | | 22 | | ) {} |
| | | 23 | | |
| | | 24 | | /** |
| | | 25 | | * Builds the command for `envilder exec`: run `command` with the secrets of |
| | | 26 | | * the map file in its environment. |
| | | 27 | | */ |
| | | 28 | | static forExec( |
| | | 29 | | map: string, |
| | | 30 | | command: string, |
| | | 31 | | args: readonly string[], |
| | | 32 | | sourceLabel?: string, |
| | | 33 | | ): DispatchActionCommand { |
| | 39 | 34 | | return new DispatchActionCommand( |
| | | 35 | | map, |
| | | 36 | | undefined, |
| | | 37 | | undefined, |
| | | 38 | | undefined, |
| | | 39 | | undefined, |
| | | 40 | | undefined, |
| | | 41 | | OperationMode.EXEC, |
| | | 42 | | command, |
| | | 43 | | [...args], |
| | | 44 | | sourceLabel, |
| | | 45 | | ); |
| | | 46 | | } |
| | | 47 | | |
| | | 48 | | static fromCliOptions(options: CliOptions): DispatchActionCommand { |
| | 40 | 49 | | const mode = DispatchActionCommand.determineOperationMode(options); |
| | 40 | 50 | | return new DispatchActionCommand( |
| | | 51 | | options.map, |
| | | 52 | | options.envfile, |
| | | 53 | | options.key, |
| | | 54 | | options.value, |
| | | 55 | | options.secretPath, |
| | | 56 | | options.profile, |
| | | 57 | | mode, |
| | | 58 | | undefined, |
| | | 59 | | [], |
| | | 60 | | undefined, |
| | | 61 | | options.mapTarget, |
| | | 62 | | ); |
| | | 63 | | } |
| | | 64 | | |
| | | 65 | | static determineOperationMode(options: CliOptions): OperationMode { |
| | 65 | 66 | | if (options.key && options.value && options.secretPath) { |
| | 32 | 67 | | return OperationMode.PUSH_SINGLE; |
| | | 68 | | } |
| | | 69 | | |
| | 33 | 70 | | if (options.push) { |
| | 4 | 71 | | return OperationMode.PUSH_ENV_TO_SECRETS; |
| | | 72 | | } |
| | | 73 | | |
| | 29 | 74 | | return OperationMode.PULL_SECRETS_TO_ENV; |
| | | 75 | | } |
| | | 76 | | } |