-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathhandler.ts
More file actions
181 lines (160 loc) · 5.12 KB
/
handler.ts
File metadata and controls
181 lines (160 loc) · 5.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import path from 'path';
import fs from 'node:fs/promises';
import {
DeployConfig,
getConfig,
getProject,
getSpec,
getStateFromProjectPayload,
syncRemoteSpec,
} from '@openfn/deploy';
import type { Logger } from '../util/logger';
import { PullOptions } from '../pull/command';
import beta from '../projects/pull';
import { fileExists } from '../util/file-exists';
import { yamlToJson } from '@openfn/project';
async function pullHandler(options: PullOptions, logger: Logger) {
if (options.beta) {
(options as any).project = options.projectId;
return beta(options as any, logger);
}
try {
const config = mergeOverrides(await getConfig(options.configPath), options);
const v2ConfigPath = path.join(
options.workspace || process.cwd(),
'openfn.yaml'
);
if (!process.env.PREFER_LEGACY_SYNC && (await fileExists(v2ConfigPath))) {
// override endpoint with one from openfn.yaml
const config = yamlToJson(await fs.readFile(v2ConfigPath, 'utf-8')) ?? {};
if (config?.project?.endpoint) {
config.endpoint = config.project.endpoint;
}
logger.always(
'Detected openfn.yaml file - switching to v2 pull (openfn project pull). Set PREFER_LEGACY_SYNC to disable this.'
);
return beta(
{
...options,
project: options.projectId,
force: true,
endpoint: config.endpoint,
apiKey: config.apiKey,
createCredentials: false,
},
logger
);
}
if (process.env['OPENFN_API_KEY']) {
logger.info('Using OPENFN_API_KEY environment variable');
config.apiKey = process.env['OPENFN_API_KEY'];
}
if (process.env['OPENFN_ENDPOINT']) {
logger.info('Using OPENFN_ENDPOINT environment variable');
config.endpoint = process.env['OPENFN_ENDPOINT'];
}
logger.always(
'Downloading existing project state (as JSON) from the server.'
);
// Get the project.json from Lightning
const { data: project } = await getProject(
config,
options.projectId,
options.snapshots
);
if (!project) {
logger.error('ERROR: Project not found.');
logger.warn(
'Please check the UUID and verify your endpoint and apiKey in your config.'
);
process.exitCode = 1;
process.exit(1);
}
// Build the state.json
const state = getStateFromProjectPayload(project!);
logger.always('Downloading the project spec (as YAML) from the server.');
// Get the project.yaml from Lightning
const queryParams = new URLSearchParams();
queryParams.append('id', options.projectId);
options.snapshots?.forEach((snapshot) =>
queryParams.append('snapshots[]', snapshot)
);
const url = new URL(
`api/provision/yaml?${queryParams.toString()}`,
config.endpoint
);
logger.debug('Fetching project spec from', url);
const res = await fetch(url, {
headers: {
Authorization: `Bearer ${config.apiKey}`,
Accept: 'application/json',
},
});
if (res.status != 200) {
logger.error('ERROR: Project spec not retrieved.');
logger.warn(
'No YAML representation of this project could be retrieved from the server.'
);
process.exitCode = 1;
process.exit(1);
}
const resolvedPath = path.resolve(config.specPath);
logger.debug('reading spec from', resolvedPath);
const updatedSpec = await syncRemoteSpec(
await res.text(),
state,
config,
logger
);
// Write the final project state and yaml to disk
await fs.writeFile(
path.resolve(config.statePath),
JSON.stringify(state, null, 2)
);
await fs.writeFile(resolvedPath, updatedSpec);
// Read the spec back in a parsed yaml
const spec = await getSpec(resolvedPath);
if (spec.errors.length > 0) {
logger.error('ERROR: invalid spec');
logger.error(spec.errors);
process.exitCode = 1;
process.exit(1);
}
logger.success('Project pulled successfully');
process.exitCode = 0;
return true;
} catch (error: any) {
throw error;
}
}
// Priority of Merging
// Config
// Env vars
// Options
function mergeOverrides(
config: DeployConfig,
options: PullOptions
): DeployConfig {
const workspace = options.workspace || process.cwd();
const statePath = options.statePath ?? config.statePath;
const specPath = options.projectPath ?? config.specPath;
return {
...config,
apiKey: pickFirst(process.env['OPENFN_API_KEY'], config.apiKey),
endpoint: pickFirst(process.env['OPENFN_ENDPOINT'], config.endpoint),
configPath: path.isAbsolute(options.configPath)
? options.configPath
: path.join(workspace, options.configPath),
specPath: path.isAbsolute(specPath)
? specPath
: path.join(workspace, specPath),
statePath: path.isAbsolute(statePath)
? statePath
: path.join(workspace, statePath),
requireConfirmation: pickFirst(options.confirm, config.requireConfirmation),
};
}
function pickFirst<T>(...args: (T | null | undefined)[]): T {
return args.find((arg) => arg !== undefined && arg !== null) as T;
}
export default pullHandler;