| | | 1 | | /** |
| | | 2 | | * Which `$config` fields each provider accepts, and the messages that report a |
| | | 3 | | * provider configuration that cannot work. Shared by the provider wiring (pull, |
| | | 4 | | * push, exec) and the map file validator, so a check and a run describe the |
| | | 5 | | * same problem with the same words. |
| | | 6 | | */ |
| | | 7 | | |
| | | 8 | | /** Every provider the map file schema (`spec/map-file.v1.json`) declares. */ |
| | 14 | 9 | | export const MAP_FILE_PROVIDERS: readonly string[] = [ |
| | | 10 | | 'aws', |
| | | 11 | | 'azure', |
| | | 12 | | 'gcp', |
| | | 13 | | 'hashicorp', |
| | | 14 | | 'file', |
| | | 15 | | ]; |
| | | 16 | | |
| | 14 | 17 | | export const DEFAULT_PROVIDER = 'aws'; |
| | | 18 | | |
| | | 19 | | /** Provider-specific `$config` fields and the providers that accept each. */ |
| | | 20 | | export const PROVIDER_SPECIFIC_FIELDS: Readonly< |
| | | 21 | | Record<string, readonly string[]> |
| | 14 | 22 | | > = { |
| | | 23 | | profile: ['aws'], |
| | | 24 | | vaultUrl: ['azure', 'hashicorp'], |
| | | 25 | | projectId: ['gcp'], |
| | | 26 | | namespace: ['hashicorp'], |
| | | 27 | | path: ['file'], |
| | | 28 | | }; |
| | | 29 | | |
| | | 30 | | /** The `$config` field each provider cannot work without. */ |
| | 14 | 31 | | export const REQUIRED_PROVIDER_FIELDS: Readonly<Record<string, string>> = { |
| | | 32 | | azure: 'vaultUrl', |
| | | 33 | | gcp: 'projectId', |
| | | 34 | | hashicorp: 'vaultUrl', |
| | | 35 | | file: 'path', |
| | | 36 | | }; |
| | | 37 | | |
| | 14 | 38 | | const PROVIDER_LABELS: Readonly<Record<string, string>> = { |
| | | 39 | | aws: 'AWS', |
| | | 40 | | azure: 'Azure', |
| | | 41 | | gcp: 'GCP', |
| | | 42 | | hashicorp: 'HashiCorp', |
| | | 43 | | file: 'file', |
| | | 44 | | }; |
| | | 45 | | |
| | 14 | 46 | | const FIELD_FLAGS: Readonly<Record<string, string>> = { |
| | | 47 | | profile: '--profile', |
| | | 48 | | vaultUrl: '--vault-url', |
| | | 49 | | }; |
| | | 50 | | |
| | | 51 | | export function unsupportedProviderMessage( |
| | | 52 | | provider: string, |
| | | 53 | | supportedProviders: readonly string[], |
| | | 54 | | ): string { |
| | 9 | 55 | | return ( |
| | | 56 | | `Unsupported provider: ${provider}.` + |
| | | 57 | | ` Supported providers:` + |
| | | 58 | | ` ${supportedProviders.join(', ')}` |
| | | 59 | | ); |
| | | 60 | | } |
| | | 61 | | |
| | | 62 | | /** |
| | | 63 | | * Reports a provider-specific field set for a provider that does not use it. |
| | | 64 | | * `subject` names where the field came from, such as `--profile` or |
| | | 65 | | * `$config.profile`. |
| | | 66 | | */ |
| | | 67 | | export function fieldOutsideProviderMessage( |
| | | 68 | | subject: string, |
| | | 69 | | validProviders: readonly string[], |
| | | 70 | | provider: string, |
| | | 71 | | ): string { |
| | 17 | 72 | | return ( |
| | | 73 | | `${subject} is only supported with the` + |
| | | 74 | | ` ${validProviders.join(' or ')} provider` + |
| | | 75 | | ` (current provider: ${provider}).` |
| | | 76 | | ); |
| | | 77 | | } |
| | | 78 | | |
| | | 79 | | export function requiredFieldMessage(field: string, provider: string): string { |
| | 9 | 80 | | const flag = FIELD_FLAGS[field]; |
| | 9 | 81 | | return ( |
| | | 82 | | `${field} is required when using` + |
| | | 83 | | ` ${PROVIDER_LABELS[provider] ?? provider} provider.` + |
| | | 84 | | ` Set it in $config.${field} in your map file` + |
| | | 85 | | (flag === undefined ? '.' : ` or via ${flag} flag.`) |
| | | 86 | | ); |
| | | 87 | | } |