@greenmind/cli 0.0.0-stage → 0.9.40
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +40 -4
- package/src/bin.ts +15 -0
- package/src/bundle.ts +100 -0
- package/src/client.ts +549 -0
- package/src/command.ts +1054 -0
- package/src/commands.ts +274 -0
- package/src/constants.ts +39 -0
- package/src/credentials.ts +159 -0
- package/src/environment-commands.ts +470 -0
- package/src/ext-config.ts +107 -0
- package/src/identity.ts +287 -0
- package/src/index.ts +58 -0
- package/src/json-read.ts +29 -0
- package/src/load.ts +127 -0
- package/src/org-commands.ts +579 -0
- package/src/protocol.ts +28 -0
- package/src/runner-command.ts +118 -0
- package/src/session.ts +278 -0
- package/README.md +0 -3
package/src/command.ts
ADDED
|
@@ -0,0 +1,1054 @@
|
|
|
1
|
+
import { readFileSync } from 'node:fs'
|
|
2
|
+
import {
|
|
3
|
+
Argument,
|
|
4
|
+
Command,
|
|
5
|
+
type CommandUnknownOpts,
|
|
6
|
+
InvalidArgumentError,
|
|
7
|
+
Option,
|
|
8
|
+
type OptionValues,
|
|
9
|
+
} from '@commander-js/extra-typings'
|
|
10
|
+
import {
|
|
11
|
+
ENVIRONMENT_COLLECTION_STORAGE,
|
|
12
|
+
ENVIRONMENT_FUNCTION_RUNTIMES,
|
|
13
|
+
type Json,
|
|
14
|
+
KEY_PERMISSIONS,
|
|
15
|
+
parseJsonText,
|
|
16
|
+
POOL_KINDS,
|
|
17
|
+
SCHEDULE_TICK_DAYS,
|
|
18
|
+
} from '@greenmind/ext-schema'
|
|
19
|
+
import { PlatformError } from './client'
|
|
20
|
+
import { runCheck, runCompile, runFunction, runPush, runUpload } from './commands'
|
|
21
|
+
import { ENV_ISSUER, ENV_KEY, ENV_ORG, ENV_URL } from './constants'
|
|
22
|
+
import {
|
|
23
|
+
flowKeysOf,
|
|
24
|
+
readSeedFile,
|
|
25
|
+
runConfigSeed,
|
|
26
|
+
runConnectorCheck,
|
|
27
|
+
runDeploy,
|
|
28
|
+
runEnvironmentList,
|
|
29
|
+
runEnvironmentStatus,
|
|
30
|
+
runEventsReplay,
|
|
31
|
+
runFlowSwitch,
|
|
32
|
+
runRuntimeGet,
|
|
33
|
+
runRuntimeSet,
|
|
34
|
+
runStorageGet,
|
|
35
|
+
runStorageSetDefault,
|
|
36
|
+
} from './environment-commands'
|
|
37
|
+
import { ExtConfigError, loadExtConfig } from './ext-config'
|
|
38
|
+
import { IdentityError, type IdentityOptions } from './identity'
|
|
39
|
+
import { LoadError } from './load'
|
|
40
|
+
import {
|
|
41
|
+
runJobCancel,
|
|
42
|
+
runJobEnqueue,
|
|
43
|
+
runJobList,
|
|
44
|
+
runJobRetry,
|
|
45
|
+
runJobShow,
|
|
46
|
+
runKeyCreate,
|
|
47
|
+
runKeyList,
|
|
48
|
+
runKeyRevoke,
|
|
49
|
+
runKeyUpdate,
|
|
50
|
+
runPoolCreate,
|
|
51
|
+
runPoolList,
|
|
52
|
+
runPoolRemove,
|
|
53
|
+
runPoolShow,
|
|
54
|
+
runPoolUpdate,
|
|
55
|
+
runRunnerDrop,
|
|
56
|
+
runRunnerList,
|
|
57
|
+
runScheduleList,
|
|
58
|
+
runSchedulePause,
|
|
59
|
+
runScheduleRun,
|
|
60
|
+
} from './org-commands'
|
|
61
|
+
import { runnerCommand } from './runner-command'
|
|
62
|
+
import { connect, runLogin, runLogout, runWhoami, SessionError } from './session'
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* The `gm` command, GreenMind's one command line. Only what concerns the whole account is at the
|
|
66
|
+
* top (`gm login`, `gm logout`, `gm whoami`); everything else belongs to a product and sits under
|
|
67
|
+
* that product's group (`gm pub ...` for the publishing platform). A platform address and a
|
|
68
|
+
* credential come from options, the environment or a stored sign-in, never from a file in the
|
|
69
|
+
* repository.
|
|
70
|
+
*/
|
|
71
|
+
|
|
72
|
+
/** A further `gm login` switch, which asks the identity server for more scopes when given. */
|
|
73
|
+
export interface LoginScopeOption {
|
|
74
|
+
/** As commander takes it, such as `--extra`. */
|
|
75
|
+
readonly flag: string
|
|
76
|
+
readonly description: string
|
|
77
|
+
readonly scopes: readonly string[]
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** A command built for the shared context: given the sign-in options every command uses. */
|
|
81
|
+
export type CommandBuilder = (context: GmCommandContext) => CommandUnknownOpts
|
|
82
|
+
|
|
83
|
+
export interface GmCommandContext {
|
|
84
|
+
/** Replaced in tests, so sign-in polling costs no time and goes only to the fake server. */
|
|
85
|
+
readonly identity?: IdentityOptions
|
|
86
|
+
/** Switches `gm login` takes beyond its own. */
|
|
87
|
+
readonly loginScopes?: readonly LoginScopeOption[]
|
|
88
|
+
/**
|
|
89
|
+
* More commands for a product's group, by the product's name: for a build of the command line
|
|
90
|
+
* that carries more than the published one does.
|
|
91
|
+
*/
|
|
92
|
+
readonly productCommands?: Readonly<Record<string, readonly CommandBuilder[]>>
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* One product's group of commands, such as `gm pub`. A product adds its commands by writing one of
|
|
97
|
+
* these and listing it in `COMMAND_GROUPS`; the sign-in and every credential option are shared, so
|
|
98
|
+
* a new product needs no second tool and no second sign-in.
|
|
99
|
+
*/
|
|
100
|
+
export interface ProductGroup {
|
|
101
|
+
readonly name: string
|
|
102
|
+
readonly aliases?: readonly string[]
|
|
103
|
+
readonly description: string
|
|
104
|
+
readonly commands: (context: GmCommandContext) => readonly CommandUnknownOpts[]
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function print(line: string): void {
|
|
108
|
+
console.log(line)
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function finish(ok: boolean): void {
|
|
112
|
+
if (!ok) process.exitCode = 1
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Runs a command's action. Errors a person caused or can mend are a message and exit code 1, not a
|
|
117
|
+
* stack; anything else is thrown on.
|
|
118
|
+
*/
|
|
119
|
+
export async function guarded(action: () => Promise<boolean> | boolean): Promise<void> {
|
|
120
|
+
try {
|
|
121
|
+
finish(await action())
|
|
122
|
+
} catch (error) {
|
|
123
|
+
if (
|
|
124
|
+
error instanceof PlatformError ||
|
|
125
|
+
error instanceof SessionError ||
|
|
126
|
+
error instanceof IdentityError ||
|
|
127
|
+
error instanceof ExtConfigError ||
|
|
128
|
+
error instanceof LoadError
|
|
129
|
+
) {
|
|
130
|
+
console.error(error.message)
|
|
131
|
+
process.exitCode = 1
|
|
132
|
+
return
|
|
133
|
+
}
|
|
134
|
+
throw error
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function sourceFrom(options: { repository?: string; commit?: string; branch?: string }) {
|
|
139
|
+
const source = {
|
|
140
|
+
repository: options.repository ?? process.env.GITHUB_REPOSITORY,
|
|
141
|
+
commit: options.commit ?? process.env.GITHUB_SHA,
|
|
142
|
+
branch: options.branch ?? process.env.GITHUB_REF_NAME,
|
|
143
|
+
}
|
|
144
|
+
return Object.fromEntries(Object.entries(source).filter(([, value]) => value))
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const URL_HELP = `The developer API's address (or ${ENV_URL})`
|
|
148
|
+
const ORG_HELP = `The handle of the organisation the environment is in (or ${ENV_ORG})`
|
|
149
|
+
const KEY_HELP = `An org key reaching the environment, holding edit_config there to change it (or ${ENV_KEY}); with one, no sign-in is read`
|
|
150
|
+
const CONFIG_HELP = 'The project config (default: ext.config.ts in this directory)'
|
|
151
|
+
const APP_KEY_HELP = "The environment's app key"
|
|
152
|
+
const ENV_HELP =
|
|
153
|
+
"The environment's app key: with an org key whose scope names one app alone, that app"
|
|
154
|
+
const RETIRE_HELP =
|
|
155
|
+
'With a live push, take the flows and schedules the files no longer define off the live set'
|
|
156
|
+
|
|
157
|
+
/** `gm login`, `gm logout` and `gm whoami`: one sign-in for every product. */
|
|
158
|
+
function signInCommands({ identity, loginScopes = [] }: GmCommandContext): CommandUnknownOpts[] {
|
|
159
|
+
const login = new Command('login')
|
|
160
|
+
.description(
|
|
161
|
+
'Sign in with your GreenMind account: the command prints a code to confirm in a browser',
|
|
162
|
+
)
|
|
163
|
+
.option('--url <url>', URL_HELP)
|
|
164
|
+
.option(
|
|
165
|
+
'--issuer <url>',
|
|
166
|
+
`The identity server's address (or ${ENV_ISSUER}); the platform names it otherwise`,
|
|
167
|
+
)
|
|
168
|
+
.option(
|
|
169
|
+
'--client-id <id>',
|
|
170
|
+
'The identity client to sign in to; the platform names it otherwise',
|
|
171
|
+
)
|
|
172
|
+
const extras = loginScopes.map((each) => {
|
|
173
|
+
const option = new Option(each.flag, each.description)
|
|
174
|
+
login.addOption(option)
|
|
175
|
+
return { name: option.attributeName(), scopes: each.scopes }
|
|
176
|
+
})
|
|
177
|
+
login.action((options, command) =>
|
|
178
|
+
guarded(() => {
|
|
179
|
+
const extraScopes = extras.flatMap((each) =>
|
|
180
|
+
command.getOptionValue(each.name) === true ? each.scopes : [],
|
|
181
|
+
)
|
|
182
|
+
return runLogin({ ...options, identity, extraScopes }, print)
|
|
183
|
+
}),
|
|
184
|
+
)
|
|
185
|
+
|
|
186
|
+
const logout = new Command('logout')
|
|
187
|
+
.description('Revoke this sign-in at the identity server and forget it')
|
|
188
|
+
.option('--url <url>', URL_HELP)
|
|
189
|
+
.action((options) => guarded(() => runLogout({ ...options, identity }, print)))
|
|
190
|
+
|
|
191
|
+
const whoami = new Command('whoami')
|
|
192
|
+
.description('Show who is signed in, and to which platform')
|
|
193
|
+
.option('--url <url>', URL_HELP)
|
|
194
|
+
.action((options) => guarded(() => runWhoami(options, print)))
|
|
195
|
+
|
|
196
|
+
return [login, logout, whoami]
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/** `gm pub env`: the environments a person can read, and where one stands. */
|
|
200
|
+
const envCommand: CommandBuilder = ({ identity }) =>
|
|
201
|
+
new Command('env')
|
|
202
|
+
.description('The environments you can read')
|
|
203
|
+
.addCommand(
|
|
204
|
+
new Command('list')
|
|
205
|
+
.description('List the environments of a project you can read')
|
|
206
|
+
.requiredOption('--project <key>', "The project's key")
|
|
207
|
+
.option('--url <url>', URL_HELP)
|
|
208
|
+
.option('--org <handle>', ORG_HELP)
|
|
209
|
+
.action(({ project, ...options }) =>
|
|
210
|
+
guarded(() =>
|
|
211
|
+
runEnvironmentList(
|
|
212
|
+
connect({ ...options, identity }, 'person', 'Listing environments'),
|
|
213
|
+
project,
|
|
214
|
+
print,
|
|
215
|
+
),
|
|
216
|
+
),
|
|
217
|
+
),
|
|
218
|
+
)
|
|
219
|
+
.addCommand(
|
|
220
|
+
new Command('status')
|
|
221
|
+
.description(
|
|
222
|
+
'Show where an environment stands: placement, writer, runtime, definitions, flows, landing and the org keys reaching it',
|
|
223
|
+
)
|
|
224
|
+
.argument('<env>', APP_KEY_HELP)
|
|
225
|
+
.option('--url <url>', URL_HELP)
|
|
226
|
+
.option('--org <handle>', ORG_HELP)
|
|
227
|
+
.option('--key <key>', KEY_HELP)
|
|
228
|
+
.action((appKey, options) =>
|
|
229
|
+
guarded(() =>
|
|
230
|
+
runEnvironmentStatus(
|
|
231
|
+
connect({ ...options, identity }, 'any', 'Reading a status'),
|
|
232
|
+
appKey,
|
|
233
|
+
print,
|
|
234
|
+
),
|
|
235
|
+
),
|
|
236
|
+
),
|
|
237
|
+
)
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* The project's definitions: flows, functions, events, collections, sockets, webhooks and
|
|
241
|
+
* permissions written in TypeScript. Check, compile, push and upload them, run a function on a recorded event, and deploy
|
|
242
|
+
* everything `ext.config.ts` names at once.
|
|
243
|
+
*/
|
|
244
|
+
function definitionCommands({ identity }: GmCommandContext): CommandUnknownOpts[] {
|
|
245
|
+
const check = new Command('check')
|
|
246
|
+
.description(
|
|
247
|
+
'Compile definitions and hold them to the gateway rules (a GET flow never writes; an anonymous caller is limited and never granted to), so what the SDK and the stored form refuse is refused here, and with a platform address and a key or sign-in check them against that environment',
|
|
248
|
+
)
|
|
249
|
+
.argument(
|
|
250
|
+
'<paths...>',
|
|
251
|
+
'TypeScript or JavaScript modules, JSON files in the stored form, or directories of them',
|
|
252
|
+
)
|
|
253
|
+
.option('--env <env>', ENV_HELP)
|
|
254
|
+
.option('--url <url>', URL_HELP)
|
|
255
|
+
.option('--org <handle>', ORG_HELP)
|
|
256
|
+
.option('--key <key>', KEY_HELP)
|
|
257
|
+
.action(async (paths, { env: appKey, ...options }) => {
|
|
258
|
+
finish(
|
|
259
|
+
await runCheck(
|
|
260
|
+
{ paths, client: connect({ ...options, identity }, 'optional', 'Checking'), appKey },
|
|
261
|
+
print,
|
|
262
|
+
),
|
|
263
|
+
)
|
|
264
|
+
})
|
|
265
|
+
|
|
266
|
+
const compile = new Command('compile')
|
|
267
|
+
.description('Print the compiled definitions as JSON, as they would be pushed')
|
|
268
|
+
.argument(
|
|
269
|
+
'<paths...>',
|
|
270
|
+
'TypeScript or JavaScript modules, JSON files in the stored form, or directories of them',
|
|
271
|
+
)
|
|
272
|
+
.action(async (paths) => {
|
|
273
|
+
await runCompile(paths, print)
|
|
274
|
+
})
|
|
275
|
+
|
|
276
|
+
const run = new Command('run')
|
|
277
|
+
.description(
|
|
278
|
+
'Run a hosted function on a recorded event, in process, with no engine and no network',
|
|
279
|
+
)
|
|
280
|
+
.argument('<file>', 'A module that exports the function')
|
|
281
|
+
.argument('<function>', "The function's key")
|
|
282
|
+
.requiredOption(
|
|
283
|
+
'--request <file>',
|
|
284
|
+
'A JSON file with the recorded input and, if the function reads, its data',
|
|
285
|
+
)
|
|
286
|
+
.option('--seed <hex>', 'The random seed, 32 bytes as 64 hex digits (default: zeros)')
|
|
287
|
+
.option('--now <time>', "The run's clock as an RFC 3339 instant (default: now)")
|
|
288
|
+
.action(async (file, functionKey, options) => {
|
|
289
|
+
const answer = await runFunction(
|
|
290
|
+
{ file, functionKey, recordedFile: options.request, seed: options.seed, now: options.now },
|
|
291
|
+
print,
|
|
292
|
+
)
|
|
293
|
+
finish(answer.outcome !== 'failed')
|
|
294
|
+
})
|
|
295
|
+
|
|
296
|
+
const push = new Command('push')
|
|
297
|
+
.description(
|
|
298
|
+
'Check definitions against the environment, and if it accepts them and you may change it, push them as new versions',
|
|
299
|
+
)
|
|
300
|
+
.argument(
|
|
301
|
+
'<paths...>',
|
|
302
|
+
'TypeScript or JavaScript modules, JSON files in the stored form, or directories of them',
|
|
303
|
+
)
|
|
304
|
+
.option('--url <url>', URL_HELP)
|
|
305
|
+
.option('--org <handle>', ORG_HELP)
|
|
306
|
+
.option('--key <key>', KEY_HELP)
|
|
307
|
+
.option('--repository <name>', 'The repository the definitions are in, kept with each version')
|
|
308
|
+
.option('--commit <sha>', 'The commit, kept with each version')
|
|
309
|
+
.option('--branch <name>', 'The branch, kept with each version')
|
|
310
|
+
.option('--live', 'Also make each pushed version live (without it a push only saves versions)')
|
|
311
|
+
.option('--retire', RETIRE_HELP)
|
|
312
|
+
.option('--env <env>', ENV_HELP)
|
|
313
|
+
.action(async (paths, { env: appKey, ...options }) => {
|
|
314
|
+
const client = connect({ ...options, identity }, 'any', 'Pushing')
|
|
315
|
+
finish(
|
|
316
|
+
await runPush(
|
|
317
|
+
{
|
|
318
|
+
paths,
|
|
319
|
+
client,
|
|
320
|
+
source: sourceFrom(options),
|
|
321
|
+
live: options.live ? true : undefined,
|
|
322
|
+
retire: options.retire ? true : undefined,
|
|
323
|
+
appKey,
|
|
324
|
+
},
|
|
325
|
+
print,
|
|
326
|
+
),
|
|
327
|
+
)
|
|
328
|
+
})
|
|
329
|
+
|
|
330
|
+
const upload = new Command('upload')
|
|
331
|
+
.description(
|
|
332
|
+
'Build the hosted functions the modules define into one bundle and upload it to the environment, with each function saved as a version that runs it',
|
|
333
|
+
)
|
|
334
|
+
.argument('<paths...>', 'TypeScript or JavaScript modules, or directories of them')
|
|
335
|
+
.option('--url <url>', URL_HELP)
|
|
336
|
+
.option('--org <handle>', ORG_HELP)
|
|
337
|
+
.option('--key <key>', KEY_HELP)
|
|
338
|
+
.option('--repository <name>', 'The repository the functions are in, kept with each version')
|
|
339
|
+
.option('--commit <sha>', 'The commit, kept with each version')
|
|
340
|
+
.option('--branch <name>', 'The branch, kept with each version')
|
|
341
|
+
.option('--env <env>', ENV_HELP)
|
|
342
|
+
.action(async (paths, { env: appKey, ...options }) => {
|
|
343
|
+
const client = connect({ ...options, identity }, 'any', 'Uploading')
|
|
344
|
+
finish(await runUpload({ paths, client, source: sourceFrom(options), appKey }, print))
|
|
345
|
+
})
|
|
346
|
+
|
|
347
|
+
const deploy = new Command('deploy')
|
|
348
|
+
.description(
|
|
349
|
+
'Upload the functions, push every definition live, seed config and switch flows on, as ext.config.ts says; stops at the first step that fails',
|
|
350
|
+
)
|
|
351
|
+
.option('--env <env>', ENV_HELP)
|
|
352
|
+
.option('--url <url>', URL_HELP)
|
|
353
|
+
.option('--org <handle>', ORG_HELP)
|
|
354
|
+
.option('--key <key>', KEY_HELP)
|
|
355
|
+
.option('--config <file>', CONFIG_HELP)
|
|
356
|
+
.option('--repository <name>', 'The repository the definitions are in, kept with each version')
|
|
357
|
+
.option('--commit <sha>', 'The commit, kept with each version')
|
|
358
|
+
.option('--branch <name>', 'The branch, kept with each version')
|
|
359
|
+
.option('--retire', RETIRE_HELP)
|
|
360
|
+
.action(({ env: appKey, ...options }) =>
|
|
361
|
+
guarded(async () => {
|
|
362
|
+
const loaded = await loadExtConfig(options.config)
|
|
363
|
+
const client = connect({ ...options, identity }, 'any', 'Deploying')
|
|
364
|
+
return runDeploy(
|
|
365
|
+
{
|
|
366
|
+
client,
|
|
367
|
+
config: loaded,
|
|
368
|
+
source: sourceFrom(options),
|
|
369
|
+
appKey,
|
|
370
|
+
retire: options.retire ? true : undefined,
|
|
371
|
+
},
|
|
372
|
+
print,
|
|
373
|
+
)
|
|
374
|
+
}),
|
|
375
|
+
)
|
|
376
|
+
|
|
377
|
+
return [check, compile, run, push, upload, deploy]
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
/** `gm pub flows on|off`. */
|
|
381
|
+
const flowsCommand: CommandBuilder = ({ identity }) => {
|
|
382
|
+
const switchCommand = (name: 'on' | 'off') =>
|
|
383
|
+
new Command(name)
|
|
384
|
+
.description(`Switch flows ${name} in an environment`)
|
|
385
|
+
.argument('[keys...]', 'The flows to switch, by key')
|
|
386
|
+
.option('--all', "Every flow the project's paths define")
|
|
387
|
+
.option('--env <env>', ENV_HELP)
|
|
388
|
+
.option('--url <url>', URL_HELP)
|
|
389
|
+
.option('--org <handle>', ORG_HELP)
|
|
390
|
+
.option('--key <key>', KEY_HELP)
|
|
391
|
+
.option('--config <file>', CONFIG_HELP)
|
|
392
|
+
.action((flowKeys, { env: appKey, ...options }) =>
|
|
393
|
+
guarded(async () => {
|
|
394
|
+
if (options.all && flowKeys.length > 0) {
|
|
395
|
+
throw new SessionError('Name flows or give --all, not both.')
|
|
396
|
+
}
|
|
397
|
+
if (!options.all && flowKeys.length === 0) {
|
|
398
|
+
throw new SessionError('Name the flows to switch, or give --all.')
|
|
399
|
+
}
|
|
400
|
+
const toSwitch = options.all
|
|
401
|
+
? await flowKeysOf((await loadExtConfig(options.config)).paths)
|
|
402
|
+
: flowKeys
|
|
403
|
+
return runFlowSwitch(
|
|
404
|
+
connect({ ...options, identity }, 'any', 'Switching flows'),
|
|
405
|
+
toSwitch,
|
|
406
|
+
name === 'on',
|
|
407
|
+
print,
|
|
408
|
+
appKey,
|
|
409
|
+
)
|
|
410
|
+
}),
|
|
411
|
+
)
|
|
412
|
+
return new Command('flows')
|
|
413
|
+
.description("Switch an environment's flows")
|
|
414
|
+
.addCommand(switchCommand('on'))
|
|
415
|
+
.addCommand(switchCommand('off'))
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
/** An ISO time a person typed, as the wire takes it; anything else is refused before a call. */
|
|
419
|
+
function isoTime(text: string): string {
|
|
420
|
+
const at = new Date(text)
|
|
421
|
+
if (Number.isNaN(at.getTime())) throw new SessionError(`${text} is not a date and time.`)
|
|
422
|
+
return at.toISOString()
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
/** The window a replay asks for, each edge given only when typed, as the wire takes it. */
|
|
426
|
+
function replayWindow(flow: string, from?: string, to?: string) {
|
|
427
|
+
if (from !== undefined && to !== undefined) {
|
|
428
|
+
return { flow, from: isoTime(from), to: isoTime(to) }
|
|
429
|
+
}
|
|
430
|
+
if (from !== undefined) return { flow, from: isoTime(from) }
|
|
431
|
+
if (to !== undefined) return { flow, to: isoTime(to) }
|
|
432
|
+
return { flow }
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
/** `gm pub events replay`: kept events re-delivered to one flow. */
|
|
436
|
+
const eventsCommand: CommandBuilder = ({ identity }) =>
|
|
437
|
+
new Command('events').description("An environment's kept events").addCommand(
|
|
438
|
+
new Command('replay')
|
|
439
|
+
.description(
|
|
440
|
+
"Replay the events an environment kept into one of its flows, over a window its plan's kept days hold; a run the flow already has is not started again",
|
|
441
|
+
)
|
|
442
|
+
.argument('<flow>', 'The flow, by key: a live, switched-on flow started by an event')
|
|
443
|
+
.option('--from <time>', 'The window starts here; the oldest event kept when left out')
|
|
444
|
+
.option('--to <time>', 'The window ends here; now when left out')
|
|
445
|
+
.option('--env <env>', ENV_HELP)
|
|
446
|
+
.option('--url <url>', URL_HELP)
|
|
447
|
+
.option('--org <handle>', ORG_HELP)
|
|
448
|
+
.option('--key <key>', KEY_HELP)
|
|
449
|
+
.action((flow, { env: appKey, from, to, ...options }) =>
|
|
450
|
+
guarded(() =>
|
|
451
|
+
runEventsReplay(
|
|
452
|
+
connect({ ...options, identity }, 'any', 'Replaying events'),
|
|
453
|
+
replayWindow(flow, from, to),
|
|
454
|
+
print,
|
|
455
|
+
appKey,
|
|
456
|
+
),
|
|
457
|
+
),
|
|
458
|
+
),
|
|
459
|
+
)
|
|
460
|
+
|
|
461
|
+
/** `gm pub functions runtime get|set`: where an environment's hosted functions run. */
|
|
462
|
+
const functionsCommand: CommandBuilder = ({ identity }) =>
|
|
463
|
+
new Command('functions').description("An environment's hosted functions").addCommand(
|
|
464
|
+
new Command('runtime')
|
|
465
|
+
.description('Where an environment runs its hosted functions')
|
|
466
|
+
.addCommand(
|
|
467
|
+
new Command('get')
|
|
468
|
+
.description('Show where its functions are to run, and where they run now')
|
|
469
|
+
.argument('<env>', APP_KEY_HELP)
|
|
470
|
+
.option('--url <url>', URL_HELP)
|
|
471
|
+
.option('--org <handle>', ORG_HELP)
|
|
472
|
+
.action((appKey, options) =>
|
|
473
|
+
guarded(() =>
|
|
474
|
+
runRuntimeGet(
|
|
475
|
+
connect({ ...options, identity }, 'person', 'Reading a runtime'),
|
|
476
|
+
appKey,
|
|
477
|
+
print,
|
|
478
|
+
),
|
|
479
|
+
),
|
|
480
|
+
),
|
|
481
|
+
)
|
|
482
|
+
.addCommand(
|
|
483
|
+
new Command('set')
|
|
484
|
+
.description('Choose where its functions run; `default` follows the stage default')
|
|
485
|
+
.argument('<env>', APP_KEY_HELP)
|
|
486
|
+
.addArgument(
|
|
487
|
+
new Argument('<runtime>', 'Where they run').choices([
|
|
488
|
+
...ENVIRONMENT_FUNCTION_RUNTIMES,
|
|
489
|
+
'default',
|
|
490
|
+
] as const),
|
|
491
|
+
)
|
|
492
|
+
.option('--url <url>', URL_HELP)
|
|
493
|
+
.option('--org <handle>', ORG_HELP)
|
|
494
|
+
.action((appKey, chosen, options) =>
|
|
495
|
+
guarded(() =>
|
|
496
|
+
runRuntimeSet(
|
|
497
|
+
connect({ ...options, identity }, 'person', 'Choosing a runtime'),
|
|
498
|
+
appKey,
|
|
499
|
+
chosen === 'default' ? null : chosen,
|
|
500
|
+
print,
|
|
501
|
+
),
|
|
502
|
+
),
|
|
503
|
+
),
|
|
504
|
+
),
|
|
505
|
+
)
|
|
506
|
+
|
|
507
|
+
const DEFAULTABLE_STORAGE = [
|
|
508
|
+
'collection',
|
|
509
|
+
'dedicated',
|
|
510
|
+
] as const satisfies readonly (typeof ENVIRONMENT_COLLECTION_STORAGE)[number][]
|
|
511
|
+
|
|
512
|
+
/** `gm pub storage get|set-default`: where an environment's collections keep their rows. */
|
|
513
|
+
const storageCommand: CommandBuilder = ({ identity }) =>
|
|
514
|
+
new Command('storage')
|
|
515
|
+
.description("Where an environment's collections keep their rows")
|
|
516
|
+
.addCommand(
|
|
517
|
+
new Command('get')
|
|
518
|
+
.description(
|
|
519
|
+
'Show the storage modes it may use and why not, its default for new collections, and where each collection is kept',
|
|
520
|
+
)
|
|
521
|
+
.argument('<env>', APP_KEY_HELP)
|
|
522
|
+
.option('--url <url>', URL_HELP)
|
|
523
|
+
.option('--org <handle>', ORG_HELP)
|
|
524
|
+
.action((appKey, options) =>
|
|
525
|
+
guarded(() =>
|
|
526
|
+
runStorageGet(
|
|
527
|
+
connect({ ...options, identity }, 'person', 'Reading collection storage'),
|
|
528
|
+
appKey,
|
|
529
|
+
print,
|
|
530
|
+
),
|
|
531
|
+
),
|
|
532
|
+
),
|
|
533
|
+
)
|
|
534
|
+
.addCommand(
|
|
535
|
+
new Command('set-default')
|
|
536
|
+
.description(
|
|
537
|
+
'Choose where new collections that name no mode are kept; `default` follows its organisation',
|
|
538
|
+
)
|
|
539
|
+
.argument('<env>', APP_KEY_HELP)
|
|
540
|
+
.addArgument(
|
|
541
|
+
// Self-hosted is never a default: it needs a data connection, which a definition names.
|
|
542
|
+
new Argument('<mode>', 'Where they are kept').choices([
|
|
543
|
+
...DEFAULTABLE_STORAGE,
|
|
544
|
+
'default',
|
|
545
|
+
] as const),
|
|
546
|
+
)
|
|
547
|
+
.option('--url <url>', URL_HELP)
|
|
548
|
+
.option('--org <handle>', ORG_HELP)
|
|
549
|
+
.action((appKey, chosen, options) =>
|
|
550
|
+
guarded(() =>
|
|
551
|
+
runStorageSetDefault(
|
|
552
|
+
connect({ ...options, identity }, 'person', 'Choosing a storage default'),
|
|
553
|
+
appKey,
|
|
554
|
+
chosen === 'default' ? null : chosen,
|
|
555
|
+
print,
|
|
556
|
+
),
|
|
557
|
+
),
|
|
558
|
+
),
|
|
559
|
+
)
|
|
560
|
+
|
|
561
|
+
/** `gm pub connector check`: a data connection's connector check, a self-hosted collection's gate. */
|
|
562
|
+
const connectorCommand: CommandBuilder = ({ identity }) =>
|
|
563
|
+
new Command('connector')
|
|
564
|
+
.description("An environment's data connectors, behind self-hosted collections")
|
|
565
|
+
.addCommand(
|
|
566
|
+
new Command('check')
|
|
567
|
+
.description(
|
|
568
|
+
"Check a data connection's connector for self-hosted collections, through the platform against your connected server; a pass lets a collection name it for 30 days",
|
|
569
|
+
)
|
|
570
|
+
.argument('<env>', APP_KEY_HELP)
|
|
571
|
+
.argument('<connection>', 'The data connection, by its key on the Connections page')
|
|
572
|
+
.option('--url <url>', URL_HELP)
|
|
573
|
+
.option('--org <handle>', ORG_HELP)
|
|
574
|
+
.action((appKey, connection, options) =>
|
|
575
|
+
guarded(() =>
|
|
576
|
+
runConnectorCheck(
|
|
577
|
+
connect({ ...options, identity }, 'person', 'Checking a connector'),
|
|
578
|
+
appKey,
|
|
579
|
+
connection,
|
|
580
|
+
print,
|
|
581
|
+
),
|
|
582
|
+
),
|
|
583
|
+
),
|
|
584
|
+
)
|
|
585
|
+
|
|
586
|
+
/** `gm pub config seed`: the config values a build's functions need. */
|
|
587
|
+
const configCommand: CommandBuilder = ({ identity }) =>
|
|
588
|
+
new Command('config').description("An environment's config").addCommand(
|
|
589
|
+
new Command('seed')
|
|
590
|
+
.description(
|
|
591
|
+
"Give an environment the config values it lacks, from a JSON file or ext.config.ts's configDefaults; a value it holds is left as it is",
|
|
592
|
+
)
|
|
593
|
+
.argument('[file]', 'A JSON file: the list of values, or { values: [...] }')
|
|
594
|
+
.option('--env <env>', ENV_HELP)
|
|
595
|
+
.option('--url <url>', URL_HELP)
|
|
596
|
+
.option('--org <handle>', ORG_HELP)
|
|
597
|
+
.option('--key <key>', KEY_HELP)
|
|
598
|
+
.option('--config <file>', CONFIG_HELP)
|
|
599
|
+
.action((file, { env: appKey, ...options }) =>
|
|
600
|
+
guarded(async () => {
|
|
601
|
+
const values = file
|
|
602
|
+
? readSeedFile(file)
|
|
603
|
+
: (await loadExtConfig(options.config)).configDefaults
|
|
604
|
+
return runConfigSeed(
|
|
605
|
+
connect({ ...options, identity }, 'any', 'Seeding config'),
|
|
606
|
+
values,
|
|
607
|
+
print,
|
|
608
|
+
appKey,
|
|
609
|
+
)
|
|
610
|
+
}),
|
|
611
|
+
),
|
|
612
|
+
)
|
|
613
|
+
|
|
614
|
+
const SIGNED_IN_OR_KEY_HELP = `An org key holding the permission the command needs (or ${ENV_KEY}); without one, your sign-in`
|
|
615
|
+
|
|
616
|
+
/** Where to call, which org, and as whom: what every command below hands `connect`. */
|
|
617
|
+
interface CallOptions {
|
|
618
|
+
url?: string
|
|
619
|
+
org?: string
|
|
620
|
+
key?: string
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
/** Options every command below takes: where, which org, and as whom. */
|
|
624
|
+
function orgLevel<A extends unknown[], O extends OptionValues, G extends OptionValues>(
|
|
625
|
+
command: Command<A, O, G>,
|
|
626
|
+
) {
|
|
627
|
+
return command
|
|
628
|
+
.option('--url <url>', URL_HELP)
|
|
629
|
+
.option('--org <handle>', ORG_HELP)
|
|
630
|
+
.option('--key <key>', SIGNED_IN_OR_KEY_HELP)
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
/** A whole number of at least `min`, as an option's value. */
|
|
634
|
+
function wholeNumber(min: number) {
|
|
635
|
+
return (value: string): number => {
|
|
636
|
+
const parsed = Number(value)
|
|
637
|
+
if (!/^\d+$/.test(value.trim()) || parsed < min) {
|
|
638
|
+
throw new InvalidArgumentError(`Give a whole number of at least ${min}.`)
|
|
639
|
+
}
|
|
640
|
+
return parsed
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
/** `gm pub keys`: the org's keys, which servers and pipelines call the platform with. */
|
|
645
|
+
const keysCommand: CommandBuilder = ({ identity }) => {
|
|
646
|
+
const client = (options: CallOptions, what: string) =>
|
|
647
|
+
connect({ ...options, identity }, 'any', what)
|
|
648
|
+
return new Command('keys')
|
|
649
|
+
.description("The org's keys, which servers and pipelines call the platform with")
|
|
650
|
+
.addCommand(
|
|
651
|
+
orgLevel(
|
|
652
|
+
new Command('list')
|
|
653
|
+
.description("List the org's keys, by the start of each; never a secret")
|
|
654
|
+
.option('--project <key>', 'Only those reaching this project')
|
|
655
|
+
.option('--app <key>', 'Only those reaching this environment'),
|
|
656
|
+
).action(({ project, app, ...options }) =>
|
|
657
|
+
guarded(() => runKeyList(client(options, 'Listing keys'), { project, app }, print)),
|
|
658
|
+
),
|
|
659
|
+
)
|
|
660
|
+
.addCommand(
|
|
661
|
+
orgLevel(
|
|
662
|
+
new Command('create')
|
|
663
|
+
.description(
|
|
664
|
+
'Make a key; its secret is printed once, here, and never again. With no --project or --app it reaches the whole org',
|
|
665
|
+
)
|
|
666
|
+
.argument('<name>', 'What it is for, such as backend or ci')
|
|
667
|
+
.addOption(
|
|
668
|
+
new Option('--permission <permission...>', 'What it may do')
|
|
669
|
+
.choices(KEY_PERMISSIONS)
|
|
670
|
+
.makeOptionMandatory(),
|
|
671
|
+
)
|
|
672
|
+
.option('--project <key...>', 'Reach only these projects')
|
|
673
|
+
.option('--app <key...>', 'Reach only these environments, by app key')
|
|
674
|
+
.addOption(
|
|
675
|
+
new Option(
|
|
676
|
+
'--expires-in-days <days>',
|
|
677
|
+
'Days until it stops working by itself',
|
|
678
|
+
).argParser(wholeNumber(1)),
|
|
679
|
+
),
|
|
680
|
+
).action((name, { permission, project, app, expiresInDays, ...options }) =>
|
|
681
|
+
guarded(() =>
|
|
682
|
+
runKeyCreate(
|
|
683
|
+
client(options, 'Creating a key'),
|
|
684
|
+
{ name, permissions: permission, projects: project, apps: app, expiresInDays },
|
|
685
|
+
print,
|
|
686
|
+
),
|
|
687
|
+
),
|
|
688
|
+
),
|
|
689
|
+
)
|
|
690
|
+
.addCommand(
|
|
691
|
+
orgLevel(
|
|
692
|
+
new Command('update')
|
|
693
|
+
.description(
|
|
694
|
+
'Rename a key, or change what it may do or reach; what you leave out stays as it is',
|
|
695
|
+
)
|
|
696
|
+
.argument('<keyId>', 'The key, by its id from `gm pub keys list`')
|
|
697
|
+
.option('--name <name>', 'Its new name')
|
|
698
|
+
.addOption(
|
|
699
|
+
new Option('--permission <permission...>', 'Everything it may do from now on').choices(
|
|
700
|
+
KEY_PERMISSIONS,
|
|
701
|
+
),
|
|
702
|
+
)
|
|
703
|
+
.option('--project <key...>', 'Reach only these projects from now on')
|
|
704
|
+
.option('--app <key...>', 'Reach only these environments from now on'),
|
|
705
|
+
).action((keyId, { name, permission, project, app, ...options }) =>
|
|
706
|
+
guarded(() =>
|
|
707
|
+
runKeyUpdate(
|
|
708
|
+
client(options, 'Changing a key'),
|
|
709
|
+
keyId,
|
|
710
|
+
{ name, permissions: permission, projects: project, apps: app },
|
|
711
|
+
print,
|
|
712
|
+
),
|
|
713
|
+
),
|
|
714
|
+
),
|
|
715
|
+
)
|
|
716
|
+
.addCommand(
|
|
717
|
+
orgLevel(
|
|
718
|
+
new Command('revoke')
|
|
719
|
+
.description('Revoke a key: it stops working at once, everywhere, and cannot be restored')
|
|
720
|
+
.argument('<keyId>', 'The key, by its id from `gm pub keys list`'),
|
|
721
|
+
).action((keyId, options) =>
|
|
722
|
+
guarded(() => runKeyRevoke(client(options, 'Revoking a key'), keyId, print)),
|
|
723
|
+
),
|
|
724
|
+
)
|
|
725
|
+
}
|
|
726
|
+
|
|
727
|
+
/** `gm pub pools`: the org's job pools and the runners connected to them. */
|
|
728
|
+
const poolsCommand: CommandBuilder = ({ identity }) => {
|
|
729
|
+
const client = (options: CallOptions, what: string) =>
|
|
730
|
+
connect({ ...options, identity }, 'any', what)
|
|
731
|
+
const settings = <A extends unknown[], O extends OptionValues, G extends OptionValues>(
|
|
732
|
+
command: Command<A, O, G>,
|
|
733
|
+
) =>
|
|
734
|
+
command
|
|
735
|
+
.option('--label <label...>', 'The labels a job must ask for to be taken here')
|
|
736
|
+
.option('--app <key...>', 'The environments it serves, by app key')
|
|
737
|
+
.addOption(
|
|
738
|
+
new Option('--max-running <jobs>', 'Jobs at once across its runners').argParser(
|
|
739
|
+
wholeNumber(1),
|
|
740
|
+
),
|
|
741
|
+
)
|
|
742
|
+
.addOption(
|
|
743
|
+
new Option('--max-runners <runners>', 'Runners connected at once').argParser(
|
|
744
|
+
wholeNumber(1),
|
|
745
|
+
),
|
|
746
|
+
)
|
|
747
|
+
return new Command('pools')
|
|
748
|
+
.description("The org's job pools, and the runners connected to them")
|
|
749
|
+
.addCommand(
|
|
750
|
+
orgLevel(new Command('list').description("List the org's pools, with their queues")).action(
|
|
751
|
+
(options) => guarded(() => runPoolList(client(options, 'Listing pools'), print)),
|
|
752
|
+
),
|
|
753
|
+
)
|
|
754
|
+
.addCommand(
|
|
755
|
+
orgLevel(
|
|
756
|
+
new Command('show').description('Show one pool').argument('<pool>', "The pool's name"),
|
|
757
|
+
).action((pool, options) =>
|
|
758
|
+
guarded(() => runPoolShow(client(options, 'Reading a pool'), pool, print)),
|
|
759
|
+
),
|
|
760
|
+
)
|
|
761
|
+
.addCommand(
|
|
762
|
+
settings(
|
|
763
|
+
orgLevel(
|
|
764
|
+
new Command('create')
|
|
765
|
+
.description('Make a pool')
|
|
766
|
+
.argument('<name>', 'Its name: lower case letters, digits and dashes')
|
|
767
|
+
.addOption(
|
|
768
|
+
new Option('--kind <kind>', 'Who runs its jobs')
|
|
769
|
+
.choices(POOL_KINDS)
|
|
770
|
+
.makeOptionMandatory(),
|
|
771
|
+
),
|
|
772
|
+
),
|
|
773
|
+
).action((name, { kind, label, app, maxRunning, maxRunners, ...options }) =>
|
|
774
|
+
guarded(() =>
|
|
775
|
+
runPoolCreate(
|
|
776
|
+
client(options, 'Creating a pool'),
|
|
777
|
+
{ name, kind, labels: label, apps: app, maxRunning, maxRunners },
|
|
778
|
+
print,
|
|
779
|
+
),
|
|
780
|
+
),
|
|
781
|
+
),
|
|
782
|
+
)
|
|
783
|
+
.addCommand(
|
|
784
|
+
settings(
|
|
785
|
+
orgLevel(
|
|
786
|
+
new Command('update')
|
|
787
|
+
.description("Change a pool's labels, environments or limits; what you leave out stays")
|
|
788
|
+
.argument('<pool>', "The pool's name"),
|
|
789
|
+
),
|
|
790
|
+
).action((pool, { label, app, maxRunning, maxRunners, ...options }) =>
|
|
791
|
+
guarded(() =>
|
|
792
|
+
runPoolUpdate(
|
|
793
|
+
client(options, 'Changing a pool'),
|
|
794
|
+
pool,
|
|
795
|
+
{ labels: label, apps: app, maxRunning, maxRunners },
|
|
796
|
+
print,
|
|
797
|
+
),
|
|
798
|
+
),
|
|
799
|
+
),
|
|
800
|
+
)
|
|
801
|
+
.addCommand(
|
|
802
|
+
orgLevel(
|
|
803
|
+
new Command('remove')
|
|
804
|
+
.description('Remove a pool; its jobs go back to the queue')
|
|
805
|
+
.argument('<pool>', "The pool's name"),
|
|
806
|
+
).action((pool, options) =>
|
|
807
|
+
guarded(() => runPoolRemove(client(options, 'Removing a pool'), pool, print)),
|
|
808
|
+
),
|
|
809
|
+
)
|
|
810
|
+
.addCommand(
|
|
811
|
+
orgLevel(
|
|
812
|
+
new Command('runners')
|
|
813
|
+
.description('List the runners connected to a pool now')
|
|
814
|
+
.argument('<pool>', "The pool's name"),
|
|
815
|
+
).action((pool, options) =>
|
|
816
|
+
guarded(() => runRunnerList(client(options, 'Listing runners'), pool, print)),
|
|
817
|
+
),
|
|
818
|
+
)
|
|
819
|
+
.addCommand(
|
|
820
|
+
orgLevel(
|
|
821
|
+
new Command('drop-runner')
|
|
822
|
+
.description('Drop one runner from a pool; the jobs it holds go back to the queue')
|
|
823
|
+
.argument('<pool>', "The pool's name")
|
|
824
|
+
.argument('<runner>', 'The runner, by its id from `gm pub pools runners`'),
|
|
825
|
+
).action((pool, runner, options) =>
|
|
826
|
+
guarded(() => runRunnerDrop(client(options, 'Dropping a runner'), pool, runner, print)),
|
|
827
|
+
),
|
|
828
|
+
)
|
|
829
|
+
}
|
|
830
|
+
|
|
831
|
+
/** A job's input, from `--input` or `--input-file`; an empty object with neither. */
|
|
832
|
+
function jobInputOf(options: { input?: string; inputFile?: string }): Json {
|
|
833
|
+
if (options.input !== undefined && options.inputFile !== undefined) {
|
|
834
|
+
throw new PlatformError('Give --input or --input-file, not both.', 400)
|
|
835
|
+
}
|
|
836
|
+
const text =
|
|
837
|
+
options.inputFile !== undefined ? readFileSync(options.inputFile, 'utf8') : options.input
|
|
838
|
+
if (text === undefined) return {}
|
|
839
|
+
try {
|
|
840
|
+
return parseJsonText(text)
|
|
841
|
+
} catch {
|
|
842
|
+
throw new PlatformError("The job's input is not JSON.", 400)
|
|
843
|
+
}
|
|
844
|
+
}
|
|
845
|
+
|
|
846
|
+
/** `gm pub jobs`: an environment's jobs. */
|
|
847
|
+
const jobsCommand: CommandBuilder = ({ identity }) => {
|
|
848
|
+
const client = (options: CallOptions, what: string) =>
|
|
849
|
+
connect({ ...options, identity }, 'any', what)
|
|
850
|
+
return new Command('jobs')
|
|
851
|
+
.description("An environment's jobs")
|
|
852
|
+
.addCommand(
|
|
853
|
+
orgLevel(
|
|
854
|
+
new Command('list')
|
|
855
|
+
.description('List its jobs, newest first')
|
|
856
|
+
.argument('<env>', APP_KEY_HELP)
|
|
857
|
+
.option('--state <states>', 'Only these states, comma separated, such as failed,queued')
|
|
858
|
+
.option('--type <type>', 'Only jobs of this type')
|
|
859
|
+
.option('--schedule <key>', 'Only jobs this schedule queued')
|
|
860
|
+
.addOption(new Option('--limit <count>', 'At most this many').argParser(wholeNumber(1)))
|
|
861
|
+
.option('--cursor <cursor>', 'The page after the one that printed it'),
|
|
862
|
+
).action((appKey, { state, type, schedule, limit, cursor, ...options }) =>
|
|
863
|
+
guarded(() =>
|
|
864
|
+
runJobList(
|
|
865
|
+
client(options, 'Listing jobs'),
|
|
866
|
+
appKey,
|
|
867
|
+
{ state, type, schedule, limit, cursor },
|
|
868
|
+
print,
|
|
869
|
+
),
|
|
870
|
+
),
|
|
871
|
+
),
|
|
872
|
+
)
|
|
873
|
+
.addCommand(
|
|
874
|
+
orgLevel(
|
|
875
|
+
new Command('show')
|
|
876
|
+
.description('Show one job: its state, attempts, progress, input, outputs and log')
|
|
877
|
+
.argument('<env>', APP_KEY_HELP)
|
|
878
|
+
.argument('<job>', "The job's id"),
|
|
879
|
+
).action((appKey, job, options) =>
|
|
880
|
+
guarded(() => runJobShow(client(options, 'Reading a job'), appKey, job, print)),
|
|
881
|
+
),
|
|
882
|
+
)
|
|
883
|
+
.addCommand(
|
|
884
|
+
orgLevel(
|
|
885
|
+
new Command('enqueue')
|
|
886
|
+
.description('Queue a job')
|
|
887
|
+
.argument('<env>', APP_KEY_HELP)
|
|
888
|
+
.argument('<type>', "The job's type")
|
|
889
|
+
.option('--input <json>', 'Its input, as JSON')
|
|
890
|
+
.option('--input-file <file>', 'Its input, from a JSON file')
|
|
891
|
+
.option('--idempotency-key <key>', 'A repeat with the same key answers the first job')
|
|
892
|
+
.addOption(
|
|
893
|
+
new Option('--delay <seconds>', 'Seconds before it may run').argParser(wholeNumber(0)),
|
|
894
|
+
),
|
|
895
|
+
).action((appKey, type, { input, inputFile, idempotencyKey, delay, ...options }) =>
|
|
896
|
+
guarded(() =>
|
|
897
|
+
runJobEnqueue(
|
|
898
|
+
client(options, 'Queueing a job'),
|
|
899
|
+
appKey,
|
|
900
|
+
{
|
|
901
|
+
type,
|
|
902
|
+
input: jobInputOf({ input, inputFile }),
|
|
903
|
+
idempotencyKey,
|
|
904
|
+
delaySeconds: delay,
|
|
905
|
+
},
|
|
906
|
+
print,
|
|
907
|
+
),
|
|
908
|
+
),
|
|
909
|
+
),
|
|
910
|
+
)
|
|
911
|
+
.addCommand(
|
|
912
|
+
orgLevel(
|
|
913
|
+
new Command('retry')
|
|
914
|
+
.description('Queue a failed or dead-lettered job again')
|
|
915
|
+
.argument('<env>', APP_KEY_HELP)
|
|
916
|
+
.argument('<job>', "The job's id"),
|
|
917
|
+
).action((appKey, job, options) =>
|
|
918
|
+
guarded(() => runJobRetry(client(options, 'Retrying a job'), appKey, job, print)),
|
|
919
|
+
),
|
|
920
|
+
)
|
|
921
|
+
.addCommand(
|
|
922
|
+
orgLevel(
|
|
923
|
+
new Command('cancel')
|
|
924
|
+
.description('Cancel a job; a running one stops at its next heartbeat')
|
|
925
|
+
.argument('<env>', APP_KEY_HELP)
|
|
926
|
+
.argument('<job>', "The job's id"),
|
|
927
|
+
).action((appKey, job, options) =>
|
|
928
|
+
guarded(() => runJobCancel(client(options, 'Cancelling a job'), appKey, job, print)),
|
|
929
|
+
),
|
|
930
|
+
)
|
|
931
|
+
}
|
|
932
|
+
|
|
933
|
+
/** `gm pub schedules`: an environment's schedules. */
|
|
934
|
+
const schedulesCommand: CommandBuilder = ({ identity }) => {
|
|
935
|
+
const client = (options: CallOptions, what: string) =>
|
|
936
|
+
connect({ ...options, identity }, 'any', what)
|
|
937
|
+
const switching = (name: 'pause' | 'resume', description: string) =>
|
|
938
|
+
orgLevel(
|
|
939
|
+
new Command(name)
|
|
940
|
+
.description(description)
|
|
941
|
+
.argument('<env>', APP_KEY_HELP)
|
|
942
|
+
.argument('<schedule>', "The schedule's key"),
|
|
943
|
+
).action((appKey, schedule, options) =>
|
|
944
|
+
guarded(() =>
|
|
945
|
+
runSchedulePause(
|
|
946
|
+
client(options, name === 'pause' ? 'Pausing a schedule' : 'Resuming a schedule'),
|
|
947
|
+
appKey,
|
|
948
|
+
schedule,
|
|
949
|
+
name === 'pause',
|
|
950
|
+
print,
|
|
951
|
+
),
|
|
952
|
+
),
|
|
953
|
+
)
|
|
954
|
+
return new Command('schedules')
|
|
955
|
+
.description("An environment's schedules")
|
|
956
|
+
.addCommand(
|
|
957
|
+
orgLevel(
|
|
958
|
+
new Command('list')
|
|
959
|
+
.description('List its live schedules, and whether each is paused')
|
|
960
|
+
.argument('<env>', APP_KEY_HELP),
|
|
961
|
+
).action((appKey, options) =>
|
|
962
|
+
guarded(() => runScheduleList(client(options, 'Listing schedules'), appKey, print)),
|
|
963
|
+
),
|
|
964
|
+
)
|
|
965
|
+
.addCommand(switching('pause', 'Stop a schedule ticking'))
|
|
966
|
+
.addCommand(switching('resume', 'Start it again; ticks missed while paused are skipped'))
|
|
967
|
+
.addCommand(
|
|
968
|
+
orgLevel(
|
|
969
|
+
new Command('run')
|
|
970
|
+
.description('Fire one tick now, outside the schedule')
|
|
971
|
+
.argument('<env>', APP_KEY_HELP)
|
|
972
|
+
.argument('<schedule>', "The schedule's key")
|
|
973
|
+
.option(
|
|
974
|
+
'--at <time>',
|
|
975
|
+
`The tick to fire, as an RFC 3339 time no more than ${SCHEDULE_TICK_DAYS} days ago, as long as the engine keeps a schedule's ticks; now when left out`,
|
|
976
|
+
),
|
|
977
|
+
).action((appKey, schedule, { at, ...options }) =>
|
|
978
|
+
guarded(() =>
|
|
979
|
+
runScheduleRun(client(options, 'Running a schedule'), appKey, schedule, at, print),
|
|
980
|
+
),
|
|
981
|
+
),
|
|
982
|
+
)
|
|
983
|
+
}
|
|
984
|
+
|
|
985
|
+
/** `gm pub`: GreenMind's publishing platform. */
|
|
986
|
+
export const PUBLISHING: ProductGroup = {
|
|
987
|
+
name: 'pub',
|
|
988
|
+
aliases: ['publishing'],
|
|
989
|
+
description:
|
|
990
|
+
"GreenMind's publishing platform: environments, the project's flows, functions and config, the org's keys and job pools, and environments' jobs and schedules",
|
|
991
|
+
commands: (context) => [
|
|
992
|
+
envCommand(context),
|
|
993
|
+
...definitionCommands(context),
|
|
994
|
+
flowsCommand(context),
|
|
995
|
+
eventsCommand(context),
|
|
996
|
+
functionsCommand(context),
|
|
997
|
+
storageCommand(context),
|
|
998
|
+
connectorCommand(context),
|
|
999
|
+
configCommand(context),
|
|
1000
|
+
keysCommand(context),
|
|
1001
|
+
poolsCommand(context),
|
|
1002
|
+
jobsCommand(context),
|
|
1003
|
+
schedulesCommand(context),
|
|
1004
|
+
],
|
|
1005
|
+
}
|
|
1006
|
+
|
|
1007
|
+
/**
|
|
1008
|
+
* Every product under `gm`, in the order help lists them. A new product is one more entry here; a
|
|
1009
|
+
* name or alias already taken is refused when the tree is built.
|
|
1010
|
+
*/
|
|
1011
|
+
export const COMMAND_GROUPS: readonly ProductGroup[] = [PUBLISHING]
|
|
1012
|
+
|
|
1013
|
+
export function buildGmCommand(
|
|
1014
|
+
context: GmCommandContext = {},
|
|
1015
|
+
groups: readonly ProductGroup[] = COMMAND_GROUPS,
|
|
1016
|
+
): Command {
|
|
1017
|
+
const root = new Command('gm').description(
|
|
1018
|
+
'GreenMind on the command line: sign in once, then use each product under its own name',
|
|
1019
|
+
)
|
|
1020
|
+
const taken = new Set<string>()
|
|
1021
|
+
const claim = (name: string) => {
|
|
1022
|
+
if (taken.has(name)) throw new Error(`Two commands are both named ${name}.`)
|
|
1023
|
+
taken.add(name)
|
|
1024
|
+
}
|
|
1025
|
+
// A runner serves an org's pool with an org key rather than a sign-in, and belongs to no one
|
|
1026
|
+
// product, so it sits beside the sign-in commands.
|
|
1027
|
+
for (const command of [...signInCommands(context), runnerCommand()]) {
|
|
1028
|
+
claim(command.name())
|
|
1029
|
+
root.addCommand(command)
|
|
1030
|
+
}
|
|
1031
|
+
for (const name of Object.keys(context.productCommands ?? {})) {
|
|
1032
|
+
if (!groups.some((group) => group.name === name)) {
|
|
1033
|
+
throw new Error(`There is no product named ${name} to add commands to.`)
|
|
1034
|
+
}
|
|
1035
|
+
}
|
|
1036
|
+
for (const group of groups) {
|
|
1037
|
+
for (const name of [group.name, ...(group.aliases ?? [])]) claim(name)
|
|
1038
|
+
const product = new Command(group.name).description(group.description)
|
|
1039
|
+
for (const alias of group.aliases ?? []) product.alias(alias)
|
|
1040
|
+
const extra = (context.productCommands?.[group.name] ?? []).map((build) => build(context))
|
|
1041
|
+
const names = new Set<string>()
|
|
1042
|
+
for (const command of [...group.commands(context), ...extra]) {
|
|
1043
|
+
if (names.has(command.name())) {
|
|
1044
|
+
throw new Error(`Two commands under ${group.name} are both named ${command.name()}.`)
|
|
1045
|
+
}
|
|
1046
|
+
names.add(command.name())
|
|
1047
|
+
product.addCommand(command)
|
|
1048
|
+
}
|
|
1049
|
+
root.addCommand(product)
|
|
1050
|
+
}
|
|
1051
|
+
return root
|
|
1052
|
+
}
|
|
1053
|
+
|
|
1054
|
+
export const gmCommand = buildGmCommand()
|