Skip to content

Commit 6689ad0

Browse files
authored
CLI: native collections support (#1197)
* cli: auto-append collections adaptor * changeset * track collections on credentials * cli: execute with collections * update command aliases * make api-token/pat usage more consistent * lightning mock: add very basic collections support * changeset * Collections integration test (#1199) * attempt test * fix test * tidying * reduce minimum release age and install * fix lockfile * remove adaptor override * package lock again * more cleanup * remove another wierd local dep * package lock yet again * fix an option conflict * fix the mock * update mock to better match lightning
1 parent 4cc799b commit 6689ad0

File tree

20 files changed

+747
-32
lines changed

20 files changed

+747
-32
lines changed

.changeset/brave-islands-search.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@openfn/cli': minor
3+
---
4+
5+
Support aliases on all project subcommands

.changeset/tiny-forks-brake.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@openfn/cli': minor
3+
---
4+
5+
Auto-load collections adptor when using collections

.changeset/wicked-plants-push.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@openfn/lightning-mock': minor
3+
---
4+
5+
Add basic collections support (GET only)

integration-tests/cli/test/execute-workflow.test.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,24 @@
11
import test from 'ava';
22
import { rm, mkdir } from 'node:fs/promises';
33
import path from 'node:path';
4+
5+
import createLightningServer from '@openfn/lightning-mock';
6+
47
import run from '../src/run';
58
import { getJSON } from '../src/util';
69

10+
// set up a lightning mock
11+
let server: any;
12+
13+
const port = 8968;
14+
15+
test.before(async () => {
16+
server = await createLightningServer({ port });
17+
server.collections.createCollection('stuff');
18+
// Important: the collection value MUST be as string
19+
server.collections.upsert('stuff', 'x', JSON.stringify({ id: 'x' }));
20+
});
21+
722
const jobsPath = path.resolve('test/fixtures');
823

924
// Note that these tests are STATEFUL
@@ -151,7 +166,6 @@ test.serial(
151166
`openfn ${jobsPath}/wf-creds.json --credentials ${jobsPath}/creds.json`,
152167
async (t) => {
153168
const { err, stdout, stderr } = await run(t.title);
154-
console.log({ stdout, stderr });
155169
t.falsy(err);
156170

157171
const out = getJSON();
@@ -285,3 +299,16 @@ test.serial(
285299
});
286300
}
287301
);
302+
303+
// collections basic test
304+
test.serial(
305+
`openfn ${jobsPath}/collections.json --endpoint http://localhost:${port} --api-key xyz`,
306+
async (t) => {
307+
const { err } = await run(t.title);
308+
t.falsy(err);
309+
310+
const out = getJSON();
311+
312+
t.deepEqual(out.data, { id: 'x' });
313+
}
314+
);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"workflow": {
3+
"steps": [
4+
{
5+
"adaptor": "common",
6+
"expression": "collections.get('stuff', 'x')"
7+
}
8+
]
9+
}
10+
}

packages/cli/src/collections/command.ts

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,6 @@ const key = {
8888
},
8989
};
9090

91-
const token = {
92-
name: 'pat',
93-
yargs: {
94-
alias: ['token'],
95-
description: 'Lightning Personal Access Token (PAT)',
96-
},
97-
};
98-
9991
const endpoint = {
10092
name: 'endpoint',
10193
yargs: {
@@ -160,7 +152,7 @@ const updatedAfter = {
160152
const getOptions = [
161153
collectionName,
162154
key,
163-
token,
155+
o.apiKey,
164156
endpoint,
165157
pageSize,
166158
limit,
@@ -201,7 +193,7 @@ const dryRun = {
201193
const removeOptions = [
202194
collectionName,
203195
key,
204-
token,
196+
o.apiKey,
205197
endpoint,
206198
dryRun,
207199

@@ -243,7 +235,7 @@ const setOptions = [
243235
override(key as any, {
244236
demand: false,
245237
}),
246-
token,
238+
o.apiKey,
247239
endpoint,
248240
value,
249241
items,

packages/cli/src/execute/command.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
import yargs from 'yargs';
2-
import { build, ensure } from '../util/command-builders';
2+
import { build, ensure, override } from '../util/command-builders';
33
import * as o from '../options';
44

55
import type { Opts } from '../options';
66

77
export type ExecuteOptions = Required<
88
Pick<
99
Opts,
10+
| 'apiKey'
1011
| 'adaptors'
1112
| 'autoinstall'
1213
| 'baseDir'
1314
| 'cacheSteps'
1415
| 'command'
1516
| 'compile'
1617
| 'credentials'
18+
| 'collectionsEndpoint'
19+
| 'collectionsVersion'
1720
| 'expandAdaptors'
1821
| 'end'
1922
| 'immutable'
@@ -44,10 +47,16 @@ const options = [
4447
o.expandAdaptors, // order is important
4548

4649
o.adaptors,
50+
override(o.apiKey, {
51+
description: 'API token for collections',
52+
alias: ['collections-api-key', 'collections-token', 'pat'],
53+
}),
4754
o.autoinstall,
4855
o.cacheSteps,
4956
o.compile,
5057
o.credentials,
58+
o.collectionsEndpoint,
59+
o.collectionsVersion,
5160
o.end,
5261
o.ignoreImports,
5362
o.immutable,

packages/cli/src/options.ts

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ export type Opts = {
3131
configPath?: string;
3232
confirm?: boolean;
3333
credentials?: string;
34+
collectionsEndpoint?: string;
35+
collectionsVersion?: string;
3436
describe?: string;
3537
end?: string; // workflow end node
3638
expandAdaptors?: boolean; // for unit tests really
@@ -136,12 +138,17 @@ export const autoinstall: CLIOption = {
136138
},
137139
};
138140

139-
export const apikey: CLIOption = {
141+
export const apiKey: CLIOption = {
140142
name: 'apikey',
141143
yargs: {
142-
alias: ['key', 'pat', 'token'],
144+
alias: ['pat', 'token', 'api-key'],
143145
description:
144-
'[beta only] API Key, Personal Access Token (Pat), or other access token',
146+
'API Key, Personal Access Token (PAT), or other access token from Lightning',
147+
},
148+
ensure: (opts: any) => {
149+
if (!opts.apikey) {
150+
opts.apiKey = process.env.OPENFN_API_KEY;
151+
}
145152
},
146153
};
147154

@@ -240,6 +247,23 @@ export const configPath: CLIOption = {
240247
},
241248
};
242249

250+
export const collectionsVersion: CLIOption = {
251+
name: 'collections-version',
252+
yargs: {
253+
description:
254+
'The version of the collections adaptor to use. Defaults to latest. Use OPENFN_COLLECTIONS_VERSION env.',
255+
},
256+
};
257+
258+
export const collectionsEndpoint: CLIOption = {
259+
name: 'collections-endpoint',
260+
yargs: {
261+
alias: ['endpoint'],
262+
description:
263+
'The Lightning server to use for collections. Will use the project endpoint if available. Use OPENFN_COLLECTIONS_ENDPOINT env.',
264+
},
265+
};
266+
243267
export const credentials: CLIOption = {
244268
name: 'credentials',
245269
yargs: {

packages/cli/src/projects/fetch.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export type FetchOptions = Pick<
3535

3636
const options = [
3737
po.alias,
38-
o.apikey,
38+
o.apiKey,
3939
o.endpoint,
4040
o.log,
4141
o.logJson,

packages/cli/src/projects/pull.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const options = [
3030
o2.workspace,
3131

3232
// general options
33-
o.apikey,
33+
o.apiKey,
3434
o.endpoint,
3535
o.log,
3636
override(o.path, {

0 commit comments

Comments
 (0)