|
1 | 1 | import type { ExecutionPlan } from '@openfn/lexicon'; |
| 2 | +import { yamlToJson } from '@openfn/project'; |
| 3 | +import { readFile } from 'node:fs/promises'; |
| 4 | +import path from 'node:path'; |
2 | 5 |
|
3 | 6 | import type { ExecuteOptions } from './command'; |
4 | 7 | import execute from './execute'; |
5 | 8 | import serializeOutput from './serialize-output'; |
6 | 9 | import getAutoinstallTargets from './get-autoinstall-targets'; |
| 10 | +import applyCredentialMap from './apply-credential-map'; |
7 | 11 |
|
8 | 12 | import { install } from '../repo/handler'; |
9 | 13 | import compile from '../compile/compile'; |
@@ -44,14 +48,43 @@ const matchStep = ( |
44 | 48 | return ''; |
45 | 49 | }; |
46 | 50 |
|
| 51 | +const loadAndApplyCredentialMap = async ( |
| 52 | + plan: ExecutionPlan, |
| 53 | + options: ExecuteOptions, |
| 54 | + logger: Logger |
| 55 | +) => { |
| 56 | + let creds = {}; |
| 57 | + if (options.credentials) { |
| 58 | + try { |
| 59 | + const credsRaw = await readFile( |
| 60 | + path.resolve(options.credentials), |
| 61 | + 'utf8' |
| 62 | + ); |
| 63 | + if (options.credentials.endsWith('.json')) { |
| 64 | + creds = JSON.parse(credsRaw); |
| 65 | + } else { |
| 66 | + creds = yamlToJson(credsRaw); |
| 67 | + } |
| 68 | + } catch (e) { |
| 69 | + logger.error('Error processing credential map:'); |
| 70 | + logger.error(e); |
| 71 | + // probably want to exist if the credential map is invalid |
| 72 | + process.exitCode = 1; |
| 73 | + return; |
| 74 | + } |
| 75 | + logger.info('Credential map loaded '); |
| 76 | + } |
| 77 | + return applyCredentialMap(plan, creds, logger); |
| 78 | +}; |
| 79 | + |
47 | 80 | const executeHandler = async (options: ExecuteOptions, logger: Logger) => { |
48 | 81 | const start = new Date().getTime(); |
49 | 82 | assertPath(options.path); |
50 | 83 | await validateAdaptors(options, logger); |
51 | 84 |
|
52 | 85 | let plan = await loadPlan(options, logger); |
53 | 86 | validatePlan(plan, logger); |
54 | | - |
| 87 | + await loadAndApplyCredentialMap(plan, options, logger); |
55 | 88 | if (options.cacheSteps) { |
56 | 89 | await clearCache(plan, options, logger); |
57 | 90 | } |
|
0 commit comments