@greenmind/cli 0.0.0-stage → 0.9.31
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 +39 -4
- package/src/bin.ts +15 -0
- package/src/bundle.ts +100 -0
- package/src/client.ts +515 -0
- package/src/command.ts +638 -0
- package/src/commands.ts +251 -0
- package/src/constants.ts +37 -0
- package/src/credentials.ts +159 -0
- package/src/environment-commands.ts +549 -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 +123 -0
- package/src/protocol.ts +30 -0
- package/src/session.ts +265 -0
- package/README.md +0 -3
package/src/command.ts
ADDED
|
@@ -0,0 +1,638 @@
|
|
|
1
|
+
import { Argument, Command, type CommandUnknownOpts, Option } from '@commander-js/extra-typings'
|
|
2
|
+
import { ENVIRONMENT_COLLECTION_STORAGE, ENVIRONMENT_FUNCTION_RUNTIMES } from '@greenmind/ext-schema'
|
|
3
|
+
import { PlatformError } from './client'
|
|
4
|
+
import { runCheck, runCompile, runFunction, runPush, runUpload } from './commands'
|
|
5
|
+
import { ENV_ISSUER, ENV_KEY, ENV_URL } from './constants'
|
|
6
|
+
import {
|
|
7
|
+
flowKeysOf,
|
|
8
|
+
readSeedFile,
|
|
9
|
+
runConfigSeed,
|
|
10
|
+
runConnectorCheck,
|
|
11
|
+
runDeploy,
|
|
12
|
+
runEnvironmentList,
|
|
13
|
+
runEnvironmentStatus,
|
|
14
|
+
runFlowSwitch,
|
|
15
|
+
runKeysCreate,
|
|
16
|
+
runKeysList,
|
|
17
|
+
runKeysRevoke,
|
|
18
|
+
runRuntimeGet,
|
|
19
|
+
runRuntimeSet,
|
|
20
|
+
runStorageGet,
|
|
21
|
+
runStorageSetDefault,
|
|
22
|
+
} from './environment-commands'
|
|
23
|
+
import { ExtConfigError, loadExtConfig } from './ext-config'
|
|
24
|
+
import { IdentityError, type IdentityOptions } from './identity'
|
|
25
|
+
import { LoadError } from './load'
|
|
26
|
+
import { connect, runLogin, runLogout, runWhoami, SessionError } from './session'
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* The `gm` command, GreenMind's one command line. Only what concerns the whole account is at the
|
|
30
|
+
* top (`gm login`, `gm logout`, `gm whoami`); everything else belongs to a product and sits under
|
|
31
|
+
* that product's group (`gm pub ...` for the publishing platform). A platform address and a
|
|
32
|
+
* credential come from options, the environment or a stored sign-in, never from a file in the
|
|
33
|
+
* repository.
|
|
34
|
+
*/
|
|
35
|
+
|
|
36
|
+
/** A further `gm login` switch, which asks the identity server for more scopes when given. */
|
|
37
|
+
export interface LoginScopeOption {
|
|
38
|
+
/** As commander takes it, such as `--extra`. */
|
|
39
|
+
readonly flag: string
|
|
40
|
+
readonly description: string
|
|
41
|
+
readonly scopes: readonly string[]
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** A command built for the shared context: given the sign-in options every command uses. */
|
|
45
|
+
export type CommandBuilder = (context: GmCommandContext) => CommandUnknownOpts
|
|
46
|
+
|
|
47
|
+
export interface GmCommandContext {
|
|
48
|
+
/** Replaced in tests, so sign-in polling costs no time and goes only to the fake server. */
|
|
49
|
+
readonly identity?: IdentityOptions
|
|
50
|
+
/** Switches `gm login` takes beyond its own. */
|
|
51
|
+
readonly loginScopes?: readonly LoginScopeOption[]
|
|
52
|
+
/**
|
|
53
|
+
* More commands for a product's group, by the product's name: for a build of the command line
|
|
54
|
+
* that carries more than the published one does.
|
|
55
|
+
*/
|
|
56
|
+
readonly productCommands?: Readonly<Record<string, readonly CommandBuilder[]>>
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* One product's group of commands, such as `gm pub`. A product adds its commands by writing one of
|
|
61
|
+
* these and listing it in `COMMAND_GROUPS`; the sign-in and every credential option are shared, so
|
|
62
|
+
* a new product needs no second tool and no second sign-in.
|
|
63
|
+
*/
|
|
64
|
+
export interface ProductGroup {
|
|
65
|
+
readonly name: string
|
|
66
|
+
readonly aliases?: readonly string[]
|
|
67
|
+
readonly description: string
|
|
68
|
+
readonly commands: (context: GmCommandContext) => readonly CommandUnknownOpts[]
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function print(line: string): void {
|
|
72
|
+
console.log(line)
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function finish(ok: boolean): void {
|
|
76
|
+
if (!ok) process.exitCode = 1
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Runs a command's action. Errors a person caused or can mend are a message and exit code 1, not a
|
|
81
|
+
* stack; anything else is thrown on.
|
|
82
|
+
*/
|
|
83
|
+
export async function guarded(action: () => Promise<boolean> | boolean): Promise<void> {
|
|
84
|
+
try {
|
|
85
|
+
finish(await action())
|
|
86
|
+
} catch (error) {
|
|
87
|
+
if (
|
|
88
|
+
error instanceof PlatformError ||
|
|
89
|
+
error instanceof SessionError ||
|
|
90
|
+
error instanceof IdentityError ||
|
|
91
|
+
error instanceof ExtConfigError ||
|
|
92
|
+
error instanceof LoadError
|
|
93
|
+
) {
|
|
94
|
+
console.error(error.message)
|
|
95
|
+
process.exitCode = 1
|
|
96
|
+
return
|
|
97
|
+
}
|
|
98
|
+
throw error
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function sourceFrom(options: { repository?: string; commit?: string; branch?: string }) {
|
|
103
|
+
const source = {
|
|
104
|
+
repository: options.repository ?? process.env.GITHUB_REPOSITORY,
|
|
105
|
+
commit: options.commit ?? process.env.GITHUB_SHA,
|
|
106
|
+
branch: options.branch ?? process.env.GITHUB_REF_NAME,
|
|
107
|
+
}
|
|
108
|
+
return Object.fromEntries(Object.entries(source).filter(([, value]) => value))
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const URL_HELP = `The platform API's address (or ${ENV_URL})`
|
|
112
|
+
const KEY_HELP = `A publish key for a test environment (or ${ENV_KEY}); with one, no sign-in is read`
|
|
113
|
+
const CONFIG_HELP = 'The project config (default: ext.config.ts in this directory)'
|
|
114
|
+
const APP_KEY_HELP = "The environment's app key"
|
|
115
|
+
const ENV_HELP =
|
|
116
|
+
"The environment's app key: needed when signed in as a person, and with a publish key only its own"
|
|
117
|
+
|
|
118
|
+
/** `gm login`, `gm logout` and `gm whoami`: one sign-in for every product. */
|
|
119
|
+
function signInCommands({ identity, loginScopes = [] }: GmCommandContext): CommandUnknownOpts[] {
|
|
120
|
+
const login = new Command('login')
|
|
121
|
+
.description(
|
|
122
|
+
'Sign in with your GreenMind account: the command prints a code to confirm in a browser',
|
|
123
|
+
)
|
|
124
|
+
.option('--url <url>', URL_HELP)
|
|
125
|
+
.option(
|
|
126
|
+
'--issuer <url>',
|
|
127
|
+
`The identity server's address (or ${ENV_ISSUER}); the platform names it otherwise`,
|
|
128
|
+
)
|
|
129
|
+
.option(
|
|
130
|
+
'--client-id <id>',
|
|
131
|
+
'The identity client to sign in to; the platform names it otherwise',
|
|
132
|
+
)
|
|
133
|
+
const extras = loginScopes.map((each) => {
|
|
134
|
+
const option = new Option(each.flag, each.description)
|
|
135
|
+
login.addOption(option)
|
|
136
|
+
return { name: option.attributeName(), scopes: each.scopes }
|
|
137
|
+
})
|
|
138
|
+
login.action((options, command) =>
|
|
139
|
+
guarded(() => {
|
|
140
|
+
const extraScopes = extras.flatMap((each) =>
|
|
141
|
+
command.getOptionValue(each.name) === true ? each.scopes : [],
|
|
142
|
+
)
|
|
143
|
+
return runLogin({ ...options, identity, extraScopes }, print)
|
|
144
|
+
}),
|
|
145
|
+
)
|
|
146
|
+
|
|
147
|
+
const logout = new Command('logout')
|
|
148
|
+
.description('Revoke this sign-in at the identity server and forget it')
|
|
149
|
+
.option('--url <url>', URL_HELP)
|
|
150
|
+
.action((options) => guarded(() => runLogout({ ...options, identity }, print)))
|
|
151
|
+
|
|
152
|
+
const whoami = new Command('whoami')
|
|
153
|
+
.description('Show who is signed in, and to which platform')
|
|
154
|
+
.option('--url <url>', URL_HELP)
|
|
155
|
+
.action((options) => guarded(() => runWhoami(options, print)))
|
|
156
|
+
|
|
157
|
+
return [login, logout, whoami]
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** `gm pub env`: the environments a person can read, and where one stands. */
|
|
161
|
+
const envCommand: CommandBuilder = ({ identity }) =>
|
|
162
|
+
new Command('env')
|
|
163
|
+
.description('The environments you can read')
|
|
164
|
+
.addCommand(
|
|
165
|
+
new Command('list')
|
|
166
|
+
.description('List the environments you can read')
|
|
167
|
+
.option('--url <url>', URL_HELP)
|
|
168
|
+
.action((options) =>
|
|
169
|
+
guarded(() =>
|
|
170
|
+
runEnvironmentList(
|
|
171
|
+
connect({ ...options, identity }, 'person', 'Listing environments'),
|
|
172
|
+
print,
|
|
173
|
+
),
|
|
174
|
+
),
|
|
175
|
+
),
|
|
176
|
+
)
|
|
177
|
+
.addCommand(
|
|
178
|
+
new Command('status')
|
|
179
|
+
.description(
|
|
180
|
+
'Show where an environment stands: placement, writer, runtime, definitions, flows, landing and keys',
|
|
181
|
+
)
|
|
182
|
+
.argument('<env>', APP_KEY_HELP)
|
|
183
|
+
.option('--url <url>', URL_HELP)
|
|
184
|
+
.option('--key <key>', KEY_HELP)
|
|
185
|
+
.action((appKey, options) =>
|
|
186
|
+
guarded(() =>
|
|
187
|
+
runEnvironmentStatus(
|
|
188
|
+
connect({ ...options, identity }, 'any', 'Reading a status'),
|
|
189
|
+
appKey,
|
|
190
|
+
print,
|
|
191
|
+
),
|
|
192
|
+
),
|
|
193
|
+
),
|
|
194
|
+
)
|
|
195
|
+
|
|
196
|
+
/** `gm pub keys`: an environment's server keys and publish keys. */
|
|
197
|
+
const keysCommand: CommandBuilder = ({ identity }) => {
|
|
198
|
+
const keyCommands = (kind: 'server' | 'publish') =>
|
|
199
|
+
new Command(kind)
|
|
200
|
+
.description(
|
|
201
|
+
kind === 'server'
|
|
202
|
+
? 'The keys the game backend calls the platform with (needs manage_server_keys)'
|
|
203
|
+
: 'The keys a build publishes with (needs manage_server_keys)',
|
|
204
|
+
)
|
|
205
|
+
.addCommand(
|
|
206
|
+
new Command('create')
|
|
207
|
+
.description(`Create a ${kind} key; its secret is printed once`)
|
|
208
|
+
.argument('<env>', APP_KEY_HELP)
|
|
209
|
+
.option('--name <name>', `The key's ${kind === 'server' ? 'name' : 'label'}`, 'gm')
|
|
210
|
+
.option('--url <url>', URL_HELP)
|
|
211
|
+
.action((appKey, options) =>
|
|
212
|
+
guarded(() =>
|
|
213
|
+
runKeysCreate(
|
|
214
|
+
connect({ ...options, identity }, 'person', 'Creating a key'),
|
|
215
|
+
kind,
|
|
216
|
+
appKey,
|
|
217
|
+
options.name,
|
|
218
|
+
print,
|
|
219
|
+
),
|
|
220
|
+
),
|
|
221
|
+
),
|
|
222
|
+
)
|
|
223
|
+
.addCommand(
|
|
224
|
+
new Command('list')
|
|
225
|
+
.description(`List an environment's ${kind} keys, by prefix`)
|
|
226
|
+
.argument('<env>', APP_KEY_HELP)
|
|
227
|
+
.option('--url <url>', URL_HELP)
|
|
228
|
+
.action((appKey, options) =>
|
|
229
|
+
guarded(() =>
|
|
230
|
+
runKeysList(
|
|
231
|
+
connect({ ...options, identity }, 'person', 'Listing keys'),
|
|
232
|
+
kind,
|
|
233
|
+
appKey,
|
|
234
|
+
print,
|
|
235
|
+
),
|
|
236
|
+
),
|
|
237
|
+
),
|
|
238
|
+
)
|
|
239
|
+
.addCommand(
|
|
240
|
+
new Command('revoke')
|
|
241
|
+
.description(`Revoke a ${kind} key`)
|
|
242
|
+
.argument('<env>', APP_KEY_HELP)
|
|
243
|
+
.argument('<key>', "The key's id, or its prefix")
|
|
244
|
+
.option('--url <url>', URL_HELP)
|
|
245
|
+
.action((appKey, key, options) =>
|
|
246
|
+
guarded(() =>
|
|
247
|
+
runKeysRevoke(
|
|
248
|
+
connect({ ...options, identity }, 'person', 'Revoking a key'),
|
|
249
|
+
kind,
|
|
250
|
+
appKey,
|
|
251
|
+
key,
|
|
252
|
+
print,
|
|
253
|
+
),
|
|
254
|
+
),
|
|
255
|
+
),
|
|
256
|
+
)
|
|
257
|
+
return new Command('keys')
|
|
258
|
+
.description('Server keys and publish keys')
|
|
259
|
+
.addCommand(keyCommands('server'))
|
|
260
|
+
.addCommand(keyCommands('publish'))
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* The project's definitions: flows, functions, events, actions and collections written in
|
|
265
|
+
* TypeScript. Check, compile, push and upload them, run a function on a recorded event, and deploy
|
|
266
|
+
* everything `ext.config.ts` names at once.
|
|
267
|
+
*/
|
|
268
|
+
function definitionCommands({ identity }: GmCommandContext): CommandUnknownOpts[] {
|
|
269
|
+
const check = new Command('check')
|
|
270
|
+
.description(
|
|
271
|
+
'Compile definitions, 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',
|
|
272
|
+
)
|
|
273
|
+
.argument(
|
|
274
|
+
'<paths...>',
|
|
275
|
+
'TypeScript or JavaScript modules, JSON files in the stored form, or directories of them',
|
|
276
|
+
)
|
|
277
|
+
.option('--env <env>', ENV_HELP)
|
|
278
|
+
.option('--url <url>', URL_HELP)
|
|
279
|
+
.option('--key <key>', KEY_HELP)
|
|
280
|
+
.action(async (paths, { env: appKey, ...options }) => {
|
|
281
|
+
finish(
|
|
282
|
+
await runCheck(
|
|
283
|
+
{ paths, client: connect({ ...options, identity }, 'optional', 'Checking'), appKey },
|
|
284
|
+
print,
|
|
285
|
+
),
|
|
286
|
+
)
|
|
287
|
+
})
|
|
288
|
+
|
|
289
|
+
const compile = new Command('compile')
|
|
290
|
+
.description('Print the compiled definitions as JSON, as they would be pushed')
|
|
291
|
+
.argument(
|
|
292
|
+
'<paths...>',
|
|
293
|
+
'TypeScript or JavaScript modules, JSON files in the stored form, or directories of them',
|
|
294
|
+
)
|
|
295
|
+
.action(async (paths) => {
|
|
296
|
+
await runCompile(paths, print)
|
|
297
|
+
})
|
|
298
|
+
|
|
299
|
+
const run = new Command('run')
|
|
300
|
+
.description(
|
|
301
|
+
'Run a hosted function on a recorded event, in process, with no engine and no network',
|
|
302
|
+
)
|
|
303
|
+
.argument('<file>', 'A module that exports the function')
|
|
304
|
+
.argument('<function>', "The function's key")
|
|
305
|
+
.requiredOption(
|
|
306
|
+
'--request <file>',
|
|
307
|
+
'A JSON file with the recorded input and, if the function reads, its data',
|
|
308
|
+
)
|
|
309
|
+
.option('--seed <hex>', 'The random seed, 32 bytes as 64 hex digits (default: zeros)')
|
|
310
|
+
.option('--now <time>', "The run's clock as an RFC 3339 instant (default: now)")
|
|
311
|
+
.action(async (file, functionKey, options) => {
|
|
312
|
+
const answer = await runFunction(
|
|
313
|
+
{ file, functionKey, recordedFile: options.request, seed: options.seed, now: options.now },
|
|
314
|
+
print,
|
|
315
|
+
)
|
|
316
|
+
finish(answer.outcome !== 'failed')
|
|
317
|
+
})
|
|
318
|
+
|
|
319
|
+
const push = new Command('push')
|
|
320
|
+
.description(
|
|
321
|
+
'Check definitions against the environment the credential belongs to, and if it accepts them and is a test environment, push them as new versions',
|
|
322
|
+
)
|
|
323
|
+
.argument(
|
|
324
|
+
'<paths...>',
|
|
325
|
+
'TypeScript or JavaScript modules, JSON files in the stored form, or directories of them',
|
|
326
|
+
)
|
|
327
|
+
.option('--url <url>', URL_HELP)
|
|
328
|
+
.option('--key <key>', KEY_HELP)
|
|
329
|
+
.option('--repository <name>', 'The repository the definitions are in, kept with each version')
|
|
330
|
+
.option('--commit <sha>', 'The commit, kept with each version')
|
|
331
|
+
.option('--branch <name>', 'The branch, kept with each version')
|
|
332
|
+
.option(
|
|
333
|
+
'--live',
|
|
334
|
+
'Also make each pushed version live (a test environment only; without it a push only saves versions)',
|
|
335
|
+
)
|
|
336
|
+
.option('--env <env>', ENV_HELP)
|
|
337
|
+
.action(async (paths, { env: appKey, ...options }) => {
|
|
338
|
+
const client = connect({ ...options, identity }, 'any', 'Pushing')
|
|
339
|
+
finish(
|
|
340
|
+
await runPush(
|
|
341
|
+
{
|
|
342
|
+
paths,
|
|
343
|
+
client,
|
|
344
|
+
source: sourceFrom(options),
|
|
345
|
+
live: options.live ? true : undefined,
|
|
346
|
+
appKey,
|
|
347
|
+
},
|
|
348
|
+
print,
|
|
349
|
+
),
|
|
350
|
+
)
|
|
351
|
+
})
|
|
352
|
+
|
|
353
|
+
const upload = new Command('upload')
|
|
354
|
+
.description(
|
|
355
|
+
"Build the hosted functions the modules define into one bundle and upload it to the credential's test environment, with each function saved as a version that runs it",
|
|
356
|
+
)
|
|
357
|
+
.argument('<paths...>', 'TypeScript or JavaScript modules, or directories of them')
|
|
358
|
+
.option('--url <url>', URL_HELP)
|
|
359
|
+
.option('--key <key>', KEY_HELP)
|
|
360
|
+
.option('--repository <name>', 'The repository the functions are in, kept with each version')
|
|
361
|
+
.option('--commit <sha>', 'The commit, kept with each version')
|
|
362
|
+
.option('--branch <name>', 'The branch, kept with each version')
|
|
363
|
+
.option('--env <env>', ENV_HELP)
|
|
364
|
+
.action(async (paths, { env: appKey, ...options }) => {
|
|
365
|
+
const client = connect({ ...options, identity }, 'any', 'Uploading')
|
|
366
|
+
finish(await runUpload({ paths, client, source: sourceFrom(options), appKey }, print))
|
|
367
|
+
})
|
|
368
|
+
|
|
369
|
+
const deploy = new Command('deploy')
|
|
370
|
+
.description(
|
|
371
|
+
'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',
|
|
372
|
+
)
|
|
373
|
+
.option('--env <env>', ENV_HELP)
|
|
374
|
+
.option('--url <url>', URL_HELP)
|
|
375
|
+
.option('--key <key>', KEY_HELP)
|
|
376
|
+
.option('--config <file>', CONFIG_HELP)
|
|
377
|
+
.option('--repository <name>', 'The repository the definitions are in, kept with each version')
|
|
378
|
+
.option('--commit <sha>', 'The commit, kept with each version')
|
|
379
|
+
.option('--branch <name>', 'The branch, kept with each version')
|
|
380
|
+
.action(({ env: appKey, ...options }) =>
|
|
381
|
+
guarded(async () => {
|
|
382
|
+
const loaded = await loadExtConfig(options.config)
|
|
383
|
+
const client = connect({ ...options, identity }, 'any', 'Deploying')
|
|
384
|
+
return runDeploy({ client, config: loaded, source: sourceFrom(options), appKey }, print)
|
|
385
|
+
}),
|
|
386
|
+
)
|
|
387
|
+
|
|
388
|
+
return [check, compile, run, push, upload, deploy]
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
/** `gm pub flows on|off`. */
|
|
392
|
+
const flowsCommand: CommandBuilder = ({ identity }) => {
|
|
393
|
+
const switchCommand = (name: 'on' | 'off') =>
|
|
394
|
+
new Command(name)
|
|
395
|
+
.description(`Switch flows ${name} in a test environment`)
|
|
396
|
+
.argument('[keys...]', 'The flows to switch, by key')
|
|
397
|
+
.option('--all', "Every flow the project's paths define")
|
|
398
|
+
.option('--env <env>', ENV_HELP)
|
|
399
|
+
.option('--url <url>', URL_HELP)
|
|
400
|
+
.option('--key <key>', KEY_HELP)
|
|
401
|
+
.option('--config <file>', CONFIG_HELP)
|
|
402
|
+
.action((flowKeys, { env: appKey, ...options }) =>
|
|
403
|
+
guarded(async () => {
|
|
404
|
+
if (options.all && flowKeys.length > 0) {
|
|
405
|
+
throw new SessionError('Name flows or give --all, not both.')
|
|
406
|
+
}
|
|
407
|
+
if (!options.all && flowKeys.length === 0) {
|
|
408
|
+
throw new SessionError('Name the flows to switch, or give --all.')
|
|
409
|
+
}
|
|
410
|
+
const toSwitch = options.all
|
|
411
|
+
? await flowKeysOf((await loadExtConfig(options.config)).paths)
|
|
412
|
+
: flowKeys
|
|
413
|
+
return runFlowSwitch(
|
|
414
|
+
connect({ ...options, identity }, 'any', 'Switching flows'),
|
|
415
|
+
toSwitch,
|
|
416
|
+
name === 'on',
|
|
417
|
+
print,
|
|
418
|
+
appKey,
|
|
419
|
+
)
|
|
420
|
+
}),
|
|
421
|
+
)
|
|
422
|
+
return new Command('flows')
|
|
423
|
+
.description("Switch an environment's flows")
|
|
424
|
+
.addCommand(switchCommand('on'))
|
|
425
|
+
.addCommand(switchCommand('off'))
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
/** `gm pub functions runtime get|set`: where an environment's hosted functions run. */
|
|
429
|
+
const functionsCommand: CommandBuilder = ({ identity }) =>
|
|
430
|
+
new Command('functions').description("An environment's hosted functions").addCommand(
|
|
431
|
+
new Command('runtime')
|
|
432
|
+
.description('Where an environment runs its hosted functions')
|
|
433
|
+
.addCommand(
|
|
434
|
+
new Command('get')
|
|
435
|
+
.description('Show where its functions are to run, and where they run now')
|
|
436
|
+
.argument('<env>', APP_KEY_HELP)
|
|
437
|
+
.option('--url <url>', URL_HELP)
|
|
438
|
+
.action((appKey, options) =>
|
|
439
|
+
guarded(() =>
|
|
440
|
+
runRuntimeGet(
|
|
441
|
+
connect({ ...options, identity }, 'person', 'Reading a runtime'),
|
|
442
|
+
appKey,
|
|
443
|
+
print,
|
|
444
|
+
),
|
|
445
|
+
),
|
|
446
|
+
),
|
|
447
|
+
)
|
|
448
|
+
.addCommand(
|
|
449
|
+
new Command('set')
|
|
450
|
+
.description('Choose where its functions run; `default` follows the stage default')
|
|
451
|
+
.argument('<env>', APP_KEY_HELP)
|
|
452
|
+
.addArgument(
|
|
453
|
+
new Argument('<runtime>', 'Where they run').choices([
|
|
454
|
+
...ENVIRONMENT_FUNCTION_RUNTIMES,
|
|
455
|
+
'default',
|
|
456
|
+
] as const),
|
|
457
|
+
)
|
|
458
|
+
.option('--url <url>', URL_HELP)
|
|
459
|
+
.action((appKey, chosen, options) =>
|
|
460
|
+
guarded(() =>
|
|
461
|
+
runRuntimeSet(
|
|
462
|
+
connect({ ...options, identity }, 'person', 'Choosing a runtime'),
|
|
463
|
+
appKey,
|
|
464
|
+
chosen === 'default' ? null : chosen,
|
|
465
|
+
print,
|
|
466
|
+
),
|
|
467
|
+
),
|
|
468
|
+
),
|
|
469
|
+
),
|
|
470
|
+
)
|
|
471
|
+
|
|
472
|
+
const DEFAULTABLE_STORAGE = [
|
|
473
|
+
'collection',
|
|
474
|
+
'dedicated',
|
|
475
|
+
] as const satisfies readonly (typeof ENVIRONMENT_COLLECTION_STORAGE)[number][]
|
|
476
|
+
|
|
477
|
+
/** `gm pub storage get|set-default`: where an environment's collections keep their rows. */
|
|
478
|
+
const storageCommand: CommandBuilder = ({ identity }) =>
|
|
479
|
+
new Command('storage')
|
|
480
|
+
.description("Where an environment's collections keep their rows")
|
|
481
|
+
.addCommand(
|
|
482
|
+
new Command('get')
|
|
483
|
+
.description(
|
|
484
|
+
'Show the storage modes it may use and why not, its default for new collections, and where each collection is kept',
|
|
485
|
+
)
|
|
486
|
+
.argument('<env>', APP_KEY_HELP)
|
|
487
|
+
.option('--url <url>', URL_HELP)
|
|
488
|
+
.action((appKey, options) =>
|
|
489
|
+
guarded(() =>
|
|
490
|
+
runStorageGet(
|
|
491
|
+
connect({ ...options, identity }, 'person', 'Reading collection storage'),
|
|
492
|
+
appKey,
|
|
493
|
+
print,
|
|
494
|
+
),
|
|
495
|
+
),
|
|
496
|
+
),
|
|
497
|
+
)
|
|
498
|
+
.addCommand(
|
|
499
|
+
new Command('set-default')
|
|
500
|
+
.description(
|
|
501
|
+
'Choose where new collections that name no mode are kept; `default` follows its organisation',
|
|
502
|
+
)
|
|
503
|
+
.argument('<env>', APP_KEY_HELP)
|
|
504
|
+
.addArgument(
|
|
505
|
+
// Self-hosted is never a default: it needs a data connection, which a definition names.
|
|
506
|
+
new Argument('<mode>', 'Where they are kept').choices([
|
|
507
|
+
...DEFAULTABLE_STORAGE,
|
|
508
|
+
'default',
|
|
509
|
+
] as const),
|
|
510
|
+
)
|
|
511
|
+
.option('--url <url>', URL_HELP)
|
|
512
|
+
.action((appKey, chosen, options) =>
|
|
513
|
+
guarded(() =>
|
|
514
|
+
runStorageSetDefault(
|
|
515
|
+
connect({ ...options, identity }, 'person', 'Choosing a storage default'),
|
|
516
|
+
appKey,
|
|
517
|
+
chosen === 'default' ? null : chosen,
|
|
518
|
+
print,
|
|
519
|
+
),
|
|
520
|
+
),
|
|
521
|
+
),
|
|
522
|
+
)
|
|
523
|
+
|
|
524
|
+
/** `gm pub connector check`: a data connection's connector check, a self-hosted collection's gate. */
|
|
525
|
+
const connectorCommand: CommandBuilder = ({ identity }) =>
|
|
526
|
+
new Command('connector')
|
|
527
|
+
.description("An environment's data connectors, behind self-hosted collections")
|
|
528
|
+
.addCommand(
|
|
529
|
+
new Command('check')
|
|
530
|
+
.description(
|
|
531
|
+
"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",
|
|
532
|
+
)
|
|
533
|
+
.argument('<env>', APP_KEY_HELP)
|
|
534
|
+
.argument('<connection>', 'The data connection, by its key on the Connections page')
|
|
535
|
+
.option('--url <url>', URL_HELP)
|
|
536
|
+
.action((appKey, connection, options) =>
|
|
537
|
+
guarded(() =>
|
|
538
|
+
runConnectorCheck(
|
|
539
|
+
connect({ ...options, identity }, 'person', 'Checking a connector'),
|
|
540
|
+
appKey,
|
|
541
|
+
connection,
|
|
542
|
+
print,
|
|
543
|
+
),
|
|
544
|
+
),
|
|
545
|
+
),
|
|
546
|
+
)
|
|
547
|
+
|
|
548
|
+
/** `gm pub config seed`: the config values a build's functions need. */
|
|
549
|
+
const configCommand: CommandBuilder = ({ identity }) =>
|
|
550
|
+
new Command('config').description("An environment's config").addCommand(
|
|
551
|
+
new Command('seed')
|
|
552
|
+
.description(
|
|
553
|
+
"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",
|
|
554
|
+
)
|
|
555
|
+
.argument('[file]', 'A JSON file: the list of values, or { values: [...] }')
|
|
556
|
+
.option('--env <env>', ENV_HELP)
|
|
557
|
+
.option('--url <url>', URL_HELP)
|
|
558
|
+
.option('--key <key>', KEY_HELP)
|
|
559
|
+
.option('--config <file>', CONFIG_HELP)
|
|
560
|
+
.action((file, { env: appKey, ...options }) =>
|
|
561
|
+
guarded(async () => {
|
|
562
|
+
const values = file
|
|
563
|
+
? readSeedFile(file)
|
|
564
|
+
: (await loadExtConfig(options.config)).configDefaults
|
|
565
|
+
return runConfigSeed(
|
|
566
|
+
connect({ ...options, identity }, 'any', 'Seeding config'),
|
|
567
|
+
values,
|
|
568
|
+
print,
|
|
569
|
+
appKey,
|
|
570
|
+
)
|
|
571
|
+
}),
|
|
572
|
+
),
|
|
573
|
+
)
|
|
574
|
+
|
|
575
|
+
/** `gm pub`: GreenMind's publishing platform. */
|
|
576
|
+
export const PUBLISHING: ProductGroup = {
|
|
577
|
+
name: 'pub',
|
|
578
|
+
aliases: ['publishing'],
|
|
579
|
+
description:
|
|
580
|
+
"GreenMind's publishing platform: environments, keys, and the project's flows, functions and config",
|
|
581
|
+
commands: (context) => [
|
|
582
|
+
envCommand(context),
|
|
583
|
+
keysCommand(context),
|
|
584
|
+
...definitionCommands(context),
|
|
585
|
+
flowsCommand(context),
|
|
586
|
+
functionsCommand(context),
|
|
587
|
+
storageCommand(context),
|
|
588
|
+
connectorCommand(context),
|
|
589
|
+
configCommand(context),
|
|
590
|
+
],
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
/**
|
|
594
|
+
* Every product under `gm`, in the order help lists them. A new product is one more entry here; a
|
|
595
|
+
* name or alias already taken is refused when the tree is built.
|
|
596
|
+
*/
|
|
597
|
+
export const COMMAND_GROUPS: readonly ProductGroup[] = [PUBLISHING]
|
|
598
|
+
|
|
599
|
+
export function buildGmCommand(
|
|
600
|
+
context: GmCommandContext = {},
|
|
601
|
+
groups: readonly ProductGroup[] = COMMAND_GROUPS,
|
|
602
|
+
): Command {
|
|
603
|
+
const root = new Command('gm').description(
|
|
604
|
+
'GreenMind on the command line: sign in once, then use each product under its own name',
|
|
605
|
+
)
|
|
606
|
+
const taken = new Set<string>()
|
|
607
|
+
const claim = (name: string) => {
|
|
608
|
+
if (taken.has(name)) throw new Error(`Two commands are both named ${name}.`)
|
|
609
|
+
taken.add(name)
|
|
610
|
+
}
|
|
611
|
+
for (const command of signInCommands(context)) {
|
|
612
|
+
claim(command.name())
|
|
613
|
+
root.addCommand(command)
|
|
614
|
+
}
|
|
615
|
+
for (const name of Object.keys(context.productCommands ?? {})) {
|
|
616
|
+
if (!groups.some((group) => group.name === name)) {
|
|
617
|
+
throw new Error(`There is no product named ${name} to add commands to.`)
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
for (const group of groups) {
|
|
621
|
+
for (const name of [group.name, ...(group.aliases ?? [])]) claim(name)
|
|
622
|
+
const product = new Command(group.name).description(group.description)
|
|
623
|
+
for (const alias of group.aliases ?? []) product.alias(alias)
|
|
624
|
+
const extra = (context.productCommands?.[group.name] ?? []).map((build) => build(context))
|
|
625
|
+
const names = new Set<string>()
|
|
626
|
+
for (const command of [...group.commands(context), ...extra]) {
|
|
627
|
+
if (names.has(command.name())) {
|
|
628
|
+
throw new Error(`Two commands under ${group.name} are both named ${command.name()}.`)
|
|
629
|
+
}
|
|
630
|
+
names.add(command.name())
|
|
631
|
+
product.addCommand(command)
|
|
632
|
+
}
|
|
633
|
+
root.addCommand(product)
|
|
634
|
+
}
|
|
635
|
+
return root
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
export const gmCommand = buildGmCommand()
|