forked from endojs/endo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathform.js
More file actions
47 lines (45 loc) · 1.24 KB
/
Copy pathform.js
File metadata and controls
47 lines (45 loc) · 1.24 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
/* global process */
import os from 'os';
import { E } from '@endo/far';
import { withEndoAgent } from '../context.js';
import { parseOptionalPetNamePath } from '../pet-name.js';
/**
* Parse --field arguments into a fields record.
* Each field is "fieldName:label" e.g. "name:Your name"
*
* @param {string[]} fieldArgs
* @returns {Record<string, { label: string }>}
*/
const parseFields = fieldArgs => {
/** @type {Record<string, { label: string }>} */
const fields = Object.create(null);
for (const arg of fieldArgs) {
const colonIndex = arg.indexOf(':');
if (colonIndex === -1) {
throw new Error(
`Invalid field format ${JSON.stringify(arg)}, expected "fieldName:label"`,
);
}
const fieldName = arg.slice(0, colonIndex);
const label = arg.slice(colonIndex + 1);
fields[fieldName] = { label };
}
return fields;
};
export const formCommand = async ({
toName,
description,
fieldArgs,
resultName,
agentNames,
}) =>
withEndoAgent(agentNames, { os, process }, async ({ agent }) => {
const fields = parseFields(fieldArgs);
const result = await E(agent).form(
toName,
description,
fields,
parseOptionalPetNamePath(resultName),
);
console.log(result);
});