@assemblyline/al-orc 0.9.0 → 0.11.0

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/CHANGELOG.md CHANGED
@@ -7,6 +7,36 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [0.11.0] - 2026-12-04
11
+
12
+ Simplify stage 7, contract: the Orchestrator is the only copy of a definition.
13
+
14
+ ### Removed (breaking)
15
+ - `al-orc stations pull|push` and `al-orc lines pull|push`, and the station and line files
16
+ they read and wrote in `.orchestrator/`. Change a definition with `stations create|edit`,
17
+ `lines create|edit`, or copy a line with `lines export|import`.
18
+ - `watch_enabled` and `dependencies` from the station type; `stations show` reads only
19
+ `auto_drain_enabled`.
20
+
21
+ ## [0.10.0] - 2026-12-04
22
+
23
+ The Orchestrator holds every station and line definition; these commands change it there
24
+ (lib-factory simplify stage 7).
25
+
26
+ ### Added
27
+ - `stations create <code> --line --name [--from] [--config] [--set] [--schedule] …`.
28
+ - `stations edit` changes every definition field: `--set`/`--unset` a config key, `--config`,
29
+ `--from`, `--schedule`, `--timezone`, `--enabled`, `--schedule-enabled`,
30
+ `--auto-drain-enabled`, `--name`, `--description`, `--line`, besides `--short-code` and tags.
31
+ - `lines create`, `lines edit`, `lines archive`, `lines unarchive`.
32
+ - `lines export` and `lines import [--as CODE] [--location]`: copy a line and its stations.
33
+ - Restart warnings: a change the factory session reads only at start prints
34
+ `⚠ Restart the factory session at <location> …`. The client keeps them as `warnings` on the
35
+ returned record.
36
+
37
+ ### Changed
38
+ - `stations show` prints From, Enabled, Schedule and Auto-drain instead of Dependencies.
39
+
10
40
  ## [0.9.0] - 2026-12-03
11
41
 
12
42
  ### Changed
package/README.md CHANGED
@@ -1,14 +1,17 @@
1
1
  # @assemblyline/al-orc
2
2
 
3
3
  The [Assembly Line](https://assemblyline.sh) Orchestrator's client + `al-orc` CLI — a **2-in-1**:
4
- import the client in code, or use the CLI to push lines and stations, run them, and read work
5
- records.
4
+ import the client in code, or use the CLI to create and change lines and stations, run them, and
5
+ read work records. The Orchestrator holds every line and station definition; you change it here
6
+ or in its UI, and the factory session reads it when it starts.
6
7
 
7
8
  ```bash
8
9
  npm install -g @assemblyline/al-orc
9
10
  al-orc login # pick orgs in the browser; one profile per org
10
11
  al-orc status # org and key the profile belongs to
11
- al-orc lines push --all && al-orc stations push --all # from a factory repo root
12
+ al-orc lines create EX --location my-laptop --name "Example"
13
+ al-orc stations create EX1 --line EX --name "Check" --from EX0/output --auto-drain-enabled
14
+ al-orc stations edit EX1 --set batch_size=20 # one config key; the value is read as JSON
12
15
  al-orc stations run EX0
13
16
  al-orc stations run EX1 --set dry_run=true # change a config key for this run
14
17
  al-orc work-records list --station EX0
@@ -19,6 +22,41 @@ printed link on any device). Orgs you already have a profile for show as connect
19
22
  profile changes only when there was none. To add a key by hand instead:
20
23
  `al-orc config profiles add acme --api-key <key> --api-secret <secret>`.
21
24
 
25
+ ## Changing a definition
26
+
27
+ `stations edit` changes one field at a time, never a whole definition from a file:
28
+
29
+ ```bash
30
+ al-orc stations edit EM1 --set ignore_senders='["a.com","b.com"]' # a top-level config key
31
+ al-orc stations edit EM1 --unset model
32
+ al-orc stations edit EM1 --config '{"batch_size": 20}' # replace the whole config
33
+ al-orc stations edit EM1 --from EM0/output # --from "" removes the conveyor
34
+ al-orc stations edit EM1 --schedule "0 * * * *" --timezone Asia/Kolkata
35
+ al-orc stations edit EM1 --enabled false | --schedule-enabled false | --auto-drain-enabled true
36
+ al-orc stations edit EM1 --name "Classify email" --short-code EM2 --add-tag email
37
+ al-orc lines edit EM --location alex-linux-pc
38
+ ```
39
+
40
+ The factory session reads definitions when it starts. A change it acts on (`from`,
41
+ `auto_drain_enabled`, a line-head's `watch_path`, a code, a line's location, creating or archiving)
42
+ prints a warning to restart the session at the line's location:
43
+
44
+ ```
45
+ $ al-orc stations edit EM1 --from EM0/out2
46
+ Updated EM1: conveyor.
47
+ ⚠ Restart the factory session at alex-linux-pc for this to take effect (from).
48
+ ```
49
+
50
+ Config, cron, `schedule_enabled`, `is_enabled`, names and tags take effect at once.
51
+
52
+ To copy a line to another org, or under a new code:
53
+
54
+ ```bash
55
+ al-orc lines export BK-SR > bk-sr.json # the line and its stations, without ids
56
+ al-orc lines import bk-sr.json --profile chisel # create them in another org
57
+ al-orc lines import bk-sr.json --as BK-SR2 # or a copy: BK-SR1 becomes BK-SR21
58
+ ```
59
+
22
60
  Profiles live in `~/.al/al-orc/config.yml` (mode 0600; override the folder with
23
61
  `AL_ORC_CONFIG_DIR`). The CLI never reads `.env`.
24
62
 
@@ -40,7 +78,7 @@ Credentials are **explicit** — the client never reads `process.env`. Code serv
40
78
 
41
79
  | Namespace | Methods |
42
80
  |---|---|
43
- | `orc.lines` | `list · getAll · get · create · update · delete` — a line owns the location its stations run at |
81
+ | `orc.lines` | `list · getAll · get · create · update · delete · archive · unarchive` — a line owns the location its stations run at |
44
82
  | `orc.stations` | `list · getAll · get · create · update · delete · archive · unarchive · run · getTags · setTags` |
45
83
  | `orc.workRecords` | `list · getAll · get · activity · cancel · getTags · setTags` |
46
84
  | `orc.tags` | `list · create · update · delete · resolveNames · ensure` |
@@ -49,7 +87,8 @@ Credentials are **explicit** — the client never reads `process.env`. Code serv
49
87
  | `orc.me()` | the org and API key the credentials belong to |
50
88
  | `orc.checkConnection()` | credentials check via `me`; never throws and never claims work |
51
89
 
52
- Return shapes: single-record ops return the unwrapped record; `list` returns `{ data, page_context }`;
90
+ Return shapes: single-record ops return the unwrapped record (a write adds `warnings` when the
91
+ factory session must restart for it); `list` returns `{ data, page_context }`;
53
92
  `getAll` returns a flat array.
54
93
 
55
94
  ## Endpoint contract
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@assemblyline/al-orc",
3
- "version": "0.9.0",
3
+ "version": "0.11.0",
4
4
  "description": "Assembly Line Orchestrator client + al-orc CLI (2-in-1): push lines and stations, run them, read work records.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -0,0 +1,96 @@
1
+ // @ts-check
2
+ /**
3
+ * `al-orc lines export` and `import`: copy a line and its stations, to another org or under a
4
+ * new code. The export file is made once and imported once, like a database dump; it is never
5
+ * kept or edited as the definition, which lives in the orchestrator.
6
+ */
7
+
8
+ import fs from 'node:fs';
9
+ import { clientFor } from '../_helpers.js';
10
+ import { printWarnings } from '../shared/print-warnings.js';
11
+
12
+ /** The station fields an export carries: the definition, without ids or orchestrator state. */
13
+ const STATION_FIELDS = [
14
+ 'short_code', 'name', 'description', 'config', 'conveyor', 'is_enabled',
15
+ 'schedule_cron', 'schedule_timezone', 'schedule_enabled', 'auto_drain_enabled',
16
+ ];
17
+
18
+ /**
19
+ * @typedef {Object} LineExport
20
+ * @property {{ code: string, name: string, description: string|null, location: string }} line
21
+ * @property {Array<Record<string, any>>} stations
22
+ */
23
+
24
+ /**
25
+ * The export of a line and its stations, ordered by code.
26
+ *
27
+ * @param {Record<string, any>} line - As the API returns it
28
+ * @param {Array<Record<string, any>>} stations - The line's stations, as the API returns them
29
+ * @returns {LineExport}
30
+ */
31
+ export function buildExport(line, stations) {
32
+ return {
33
+ line: { code: line.code, name: line.name, description: line.description ?? null, location: line.location },
34
+ stations: [...stations]
35
+ .sort((a, b) => String(a.short_code).localeCompare(String(b.short_code)))
36
+ .map((s) => Object.fromEntries(STATION_FIELDS.filter((f) => s[f] !== undefined).map((f) => [f, s[f]]))),
37
+ };
38
+ }
39
+
40
+ /**
41
+ * Rename a line on import: the line's code, every station code that starts with it, and the
42
+ * bins in `conveyor.from` that name those stations. `BK-SR` → `BK-SR2` turns `BK-SR1` into
43
+ * `BK-SR21` and `BK-SR0/output` into `BK-SR20/output`.
44
+ *
45
+ * @param {LineExport} data
46
+ * @param {string} new_code
47
+ * @returns {LineExport}
48
+ */
49
+ export function renameLine(data, new_code) {
50
+ const old_code = data.line.code;
51
+ const rename = (code) => (typeof code === 'string' && code.startsWith(old_code) ? new_code + code.slice(old_code.length) : code);
52
+ const renameBin = (bin) => {
53
+ const [code, ...rest] = bin.split('/');
54
+ return [rename(code), ...rest].join('/');
55
+ };
56
+ return {
57
+ line: { ...data.line, code: new_code, name: data.line.name === old_code ? new_code : data.line.name },
58
+ stations: data.stations.map((s) => {
59
+ const from = s.conveyor?.from;
60
+ return {
61
+ ...s,
62
+ short_code: rename(s.short_code),
63
+ ...(from ? { conveyor: { from: Array.isArray(from) ? from.map(renameBin) : renameBin(from) } } : {}),
64
+ };
65
+ }),
66
+ };
67
+ }
68
+
69
+ /** `al-orc lines export <code>` — the line and its stations as JSON, on stdout. */
70
+ export async function exportLineHandler(argv) {
71
+ const orc = clientFor();
72
+ const line = await orc.lines.get(argv.id);
73
+ if (!line) throw new Error(`Line not found: ${argv.id}`);
74
+ const stations = await orc.stations.getAll({ line: line.code });
75
+ console.log(JSON.stringify(buildExport(line, stations), null, 2));
76
+ }
77
+
78
+ /** `al-orc lines import <file> [--as CODE] [--location LOC]` — create the line and its stations. */
79
+ export async function importLineHandler(argv) {
80
+ let data = /** @type {LineExport} */ (JSON.parse(fs.readFileSync(argv.file, 'utf8')));
81
+ if (!data?.line?.code || !Array.isArray(data.stations)) {
82
+ throw new Error(`${argv.file} is not a line export: it needs { line: { code, … }, stations: [ … ] }`);
83
+ }
84
+ if (argv.as) data = renameLine(data, argv.as);
85
+ if (argv.location) data = { ...data, line: { ...data.line, location: argv.location } };
86
+
87
+ const orc = clientFor();
88
+ const line = await orc.lines.create(data.line);
89
+ console.log(`Created line ${line?.code ?? data.line.code} at ${data.line.location}.`);
90
+ let last = null;
91
+ for (const station of data.stations) {
92
+ last = await orc.stations.create({ ...station, line: data.line.code });
93
+ console.log(`Created ${last?.short_code ?? station.short_code}.`);
94
+ }
95
+ printWarnings(last ?? line);
96
+ }
@@ -4,15 +4,15 @@
4
4
  *
5
5
  * A line is a sequence of stations that runs at exactly one location.
6
6
  * The line owns `location`; stations name their line by `code` and inherit it.
7
- * Local files: `.orchestrator/lines/<CODE>.json`.
7
+ * The orchestrator holds the definition; create and edit change it there.
8
8
  */
9
9
 
10
10
  import { safe, paginationOptions, localOptions } from '../_helpers.js';
11
11
 
12
12
  import { listLinesHandler } from './list.js';
13
13
  import { showLineHandler } from './show.js';
14
- import { pullLinesHandler } from './pull.js';
15
- import { pushLinesHandler } from './push.js';
14
+ import { createLineHandler, editLineHandler, archiveLineHandler, unarchiveLineHandler } from './write.js';
15
+ import { exportLineHandler, importLineHandler } from './copy.js';
16
16
 
17
17
  export function buildLinesSubcommands(yargs) {
18
18
  return yargs
@@ -35,20 +35,54 @@ export function buildLinesSubcommands(yargs) {
35
35
  safe(showLineHandler),
36
36
  )
37
37
  .command(
38
- 'pull [id]',
39
- 'Pull line definitions to .orchestrator/lines/ (--all for every line)',
38
+ 'create <code>',
39
+ 'Create a line: al-orc lines create EM --location alex-linux-pc --name "Email"',
40
40
  (y) =>
41
- localOptions(y.positional('id', { describe: 'Line code or ID (omit with --all)', type: 'string' }))
42
- .option('all', { describe: 'Pull every line', type: 'boolean' }),
43
- safe(pullLinesHandler),
41
+ localOptions(y.positional('code', { describe: 'Line code, e.g. EM', type: 'string' }))
42
+ .option('location', { describe: 'Where the line runs (FACTORY_LOCATION of its session)', type: 'string', demandOption: true })
43
+ .option('name', { describe: 'Display name (defaults to the code)', type: 'string' })
44
+ .option('description', { describe: 'What the line does', type: 'string' })
45
+ .option('json', { describe: 'Output raw JSON', type: 'boolean' }),
46
+ safe(createLineHandler),
47
+ )
48
+ .command(
49
+ 'edit <id>',
50
+ 'Change a line\'s code, name, description or location',
51
+ (y) =>
52
+ localOptions(y.positional('id', { describe: 'Line code or ID', type: 'string' }))
53
+ .option('code', { describe: 'New code', type: 'string' })
54
+ .option('name', { describe: 'Display name', type: 'string' })
55
+ .option('description', { describe: 'What the line does ("" clears it)', type: 'string' })
56
+ .option('location', { describe: 'Where the line runs', type: 'string' })
57
+ .option('json', { describe: 'Output raw JSON', type: 'boolean' }),
58
+ safe(editLineHandler),
59
+ )
60
+ .command(
61
+ 'archive <id>',
62
+ 'Archive a line (its stations must be archived first)',
63
+ (y) => localOptions(y.positional('id', { describe: 'Line code or ID', type: 'string' })),
64
+ safe(archiveLineHandler),
65
+ )
66
+ .command(
67
+ 'unarchive <id>',
68
+ 'Unarchive a line (its stations stay archived)',
69
+ (y) => localOptions(y.positional('id', { describe: 'Line code or ID', type: 'string' })),
70
+ safe(unarchiveLineHandler),
71
+ )
72
+ .command(
73
+ 'export <id>',
74
+ 'Print a line and its stations as JSON, without ids, to import elsewhere',
75
+ (y) => localOptions(y.positional('id', { describe: 'Line code or ID', type: 'string' })),
76
+ safe(exportLineHandler),
44
77
  )
45
78
  .command(
46
- 'push [id]',
47
- 'Push local line files to the orchestrator (create/update; --all for every file)',
79
+ 'import <file>',
80
+ 'Create a line and its stations from an export (into the --profile org)',
48
81
  (y) =>
49
- localOptions(y.positional('id', { describe: 'Filename, line code, or ID (omit with --all)', type: 'string' }))
50
- .option('all', { describe: 'Push every local line file', type: 'boolean' }),
51
- safe(pushLinesHandler),
82
+ localOptions(y.positional('file', { describe: 'A file from al-orc lines export', type: 'string' }))
83
+ .option('as', { describe: 'Import under a new line code; station codes and from bins follow', type: 'string' })
84
+ .option('location', { describe: 'Run the line at another location', type: 'string' }),
85
+ safe(importLineHandler),
52
86
  )
53
- .demandCommand(1, 'Specify an action: list, show, pull, push');
87
+ .demandCommand(1, 'Specify an action: list, show, create, edit, archive, unarchive, export, import');
54
88
  }
@@ -30,7 +30,7 @@ export async function showLineHandler(argv) {
30
30
  const stations = line.stations || [];
31
31
  console.log(formatSection(`Stations (${stations.length})`));
32
32
  if (stations.length === 0) {
33
- console.log(' (none — push station files with "line": "' + line.code + '")');
33
+ console.log(' (none — al-orc stations create <CODE> --line ' + line.code + ')');
34
34
  return;
35
35
  }
36
36
  console.log(formatTable(['CODE', 'ID', 'NAME'], stations.map((s) => [s.short_code || '—', s.id, s.name || '—'])));
@@ -0,0 +1,51 @@
1
+ // @ts-check
2
+ import { clientFor } from '../_helpers.js';
3
+ import { printWarnings } from '../shared/print-warnings.js';
4
+
5
+ /** `al-orc lines create <code> --location <loc> [--name] [--description]` */
6
+ export async function createLineHandler(argv) {
7
+ const { code, location, name, description, json } = argv;
8
+ const line = await clientFor().lines.create({ code, location, name, description });
9
+ if (json) {
10
+ console.log(JSON.stringify(line, null, 2));
11
+ return;
12
+ }
13
+ console.log(`Created line ${line?.code ?? code} at ${line?.location ?? location}.`);
14
+ printWarnings(line);
15
+ }
16
+
17
+ /** `al-orc lines edit <id> [--code] [--name] [--description] [--location]` */
18
+ export async function editLineHandler(argv) {
19
+ const { id, code, name, description, location, json } = argv;
20
+ /** @type {Record<string, any>} */
21
+ const body = {};
22
+ if (code !== undefined) body.code = code;
23
+ if (name !== undefined) body.name = name;
24
+ if (description !== undefined) body.description = description || null;
25
+ if (location !== undefined) body.location = location;
26
+ if (Object.keys(body).length === 0) {
27
+ throw new Error('Nothing to change: give --code, --name, --description or --location');
28
+ }
29
+
30
+ const line = await clientFor().lines.update(id, body);
31
+ if (json) {
32
+ console.log(JSON.stringify(line, null, 2));
33
+ return;
34
+ }
35
+ console.log(`Updated line ${line?.code ?? id}: ${Object.keys(body).join(', ')}.`);
36
+ printWarnings(line);
37
+ }
38
+
39
+ /** `al-orc lines archive <id>` */
40
+ export async function archiveLineHandler(argv) {
41
+ const line = await clientFor().lines.archive(argv.id);
42
+ console.log(`Archived line ${argv.id}.`);
43
+ printWarnings(line);
44
+ }
45
+
46
+ /** `al-orc lines unarchive <id>` */
47
+ export async function unarchiveLineHandler(argv) {
48
+ const line = await clientFor().lines.unarchive(argv.id);
49
+ console.log(`Unarchived line ${argv.id}.`);
50
+ printWarnings(line);
51
+ }
@@ -0,0 +1,11 @@
1
+ // @ts-check
2
+
3
+ /**
4
+ * Print the restart warnings an orchestrator write returned: changes the factory session reads
5
+ * only when it starts. To stderr, like every diagnostic.
6
+ *
7
+ * @param {{ warnings?: string[] } | null | undefined} record
8
+ */
9
+ export function printWarnings(record) {
10
+ for (const warning of record?.warnings ?? []) console.error(`⚠ ${warning}`);
11
+ }
@@ -1,5 +1,6 @@
1
1
  // @ts-check
2
2
  import { clientFor } from '../_helpers.js';
3
+ import { printWarnings } from '../shared/print-warnings.js';
3
4
 
4
5
  /** `al-orc stations archive <id>` — hide from default list, preserve history, block execution. */
5
6
  export async function archiveStationHandler(argv) {
@@ -11,6 +12,7 @@ export async function archiveStationHandler(argv) {
11
12
  }
12
13
  // The API response carries short_code; `id` is only what the user typed.
13
14
  console.log(`Archived station ${station?.short_code || id}.`);
15
+ printWarnings(station);
14
16
  }
15
17
 
16
18
  /** `al-orc stations unarchive <id>` — undo archive. */
@@ -22,4 +24,5 @@ export async function unarchiveStationHandler(argv) {
22
24
  return;
23
25
  }
24
26
  console.log(`Unarchived station ${station?.short_code || id}.`);
27
+ printWarnings(station);
25
28
  }
@@ -0,0 +1,21 @@
1
+ // @ts-check
2
+ import { clientFor } from '../_helpers.js';
3
+ import { printWarnings } from '../shared/print-warnings.js';
4
+ import { definitionBody } from './definition.js';
5
+
6
+ /** `al-orc stations create <code> --line <code> --name "…"` — create a station in the orchestrator. */
7
+ export async function createStationHandler(argv) {
8
+ const { code, json } = argv;
9
+ /** @type {Record<string, any>} */
10
+ const body = { short_code: code, ...definitionBody(argv) };
11
+ if (!body.line) throw new Error('--line is required: every station is on a line');
12
+ if (!body.name) throw new Error('--name is required');
13
+
14
+ const station = await clientFor().stations.create(body);
15
+ if (json) {
16
+ console.log(JSON.stringify(station, null, 2));
17
+ return;
18
+ }
19
+ console.log(`Created ${station?.short_code ?? code} (${station?.id}).`);
20
+ printWarnings(station);
21
+ }
@@ -0,0 +1,83 @@
1
+ // @ts-check
2
+ /**
3
+ * The definition options `stations create` and `stations edit` share, and the request body
4
+ * they build. A definition is changed one field at a time, never sent whole from a file.
5
+ */
6
+
7
+ import { parseSets } from './run.js';
8
+
9
+ /**
10
+ * Register the definition options on a command.
11
+ * @param {any} yargs
12
+ */
13
+ export function definitionOptions(yargs) {
14
+ return yargs
15
+ .option('name', { describe: 'Display name', type: 'string' })
16
+ .option('description', { describe: 'What the station does', type: 'string' })
17
+ .option('line', { describe: 'Line code (moves the station when editing)', type: 'string' })
18
+ .option('config', { describe: 'The whole config, as a JSON object (replaces it)', type: 'string' })
19
+ .option('set', {
20
+ describe: 'Set one top-level config key; repeatable. The value is read as JSON (true, 2, "x", [..]), else text',
21
+ type: 'string',
22
+ array: true,
23
+ })
24
+ .option('unset', { describe: 'Remove a top-level config key; repeatable', type: 'string', array: true })
25
+ .option('from', {
26
+ describe: 'Bin(s) the conveyor moves work from, e.g. EM0/output; repeatable. --from "" removes the conveyor',
27
+ type: 'string',
28
+ array: true,
29
+ })
30
+ .option('schedule', { describe: 'Cron expression ("" clears it)', type: 'string' })
31
+ .option('timezone', { describe: 'IANA timezone for the schedule, e.g. Asia/Kolkata', type: 'string' })
32
+ .option('enabled', { describe: 'Station on or off (off: nothing runs, not even by hand)', type: 'boolean' })
33
+ .option('schedule-enabled', { describe: 'Cron on or off, keeping its expression', type: 'boolean' })
34
+ .option('auto-drain-enabled', {
35
+ describe: 'Run automatically whenever work is waiting (input/, or a line-head\'s watch_path)',
36
+ type: 'boolean',
37
+ });
38
+ }
39
+
40
+ /**
41
+ * The API body for the definition options given.
42
+ *
43
+ * @param {Record<string, any>} argv
44
+ * @param {Record<string, any>} [current_config] - The station's config now; --set and --unset change a copy of it
45
+ * @returns {Record<string, any>}
46
+ */
47
+ export function definitionBody(argv, current_config = {}) {
48
+ /** @type {Record<string, any>} */
49
+ const body = {};
50
+ if (argv.name !== undefined) body.name = argv.name;
51
+ if (argv.description !== undefined) body.description = argv.description || null;
52
+ if (argv.line !== undefined) body.line = argv.line;
53
+
54
+ const sets = argv.set ?? [];
55
+ const unsets = argv.unset ?? [];
56
+ if (argv.config !== undefined || sets.length > 0 || unsets.length > 0) {
57
+ let config = { ...current_config };
58
+ if (argv.config !== undefined) {
59
+ try {
60
+ config = JSON.parse(argv.config);
61
+ } catch (err) {
62
+ throw new Error(`--config is not valid JSON: ${err.message}`);
63
+ }
64
+ if (config === null || typeof config !== 'object' || Array.isArray(config)) {
65
+ throw new Error('--config must be a JSON object');
66
+ }
67
+ }
68
+ Object.assign(config, parseSets(sets));
69
+ for (const key of unsets) delete config[key];
70
+ body.config = config;
71
+ }
72
+
73
+ if (argv.from !== undefined) {
74
+ const bins = argv.from.filter((bin) => bin !== '');
75
+ body.conveyor = bins.length === 0 ? null : { from: bins.length === 1 ? bins[0] : bins };
76
+ }
77
+ if (argv.schedule !== undefined) body.schedule_cron = argv.schedule || null;
78
+ if (argv.timezone !== undefined) body.schedule_timezone = argv.timezone;
79
+ if (argv.enabled !== undefined) body.is_enabled = argv.enabled;
80
+ if (argv.scheduleEnabled !== undefined) body.schedule_enabled = argv.scheduleEnabled;
81
+ if (argv.autoDrainEnabled !== undefined) body.auto_drain_enabled = argv.autoDrainEnabled;
82
+ return body;
83
+ }
@@ -1,31 +1,44 @@
1
1
  // @ts-check
2
2
  import { clientFor } from '../_helpers.js';
3
3
  import { editEntityTags } from '../shared/edit-tags.js';
4
+ import { printWarnings } from '../shared/print-warnings.js';
5
+ import { definitionBody } from './definition.js';
4
6
 
5
- /** `al-orc stations edit <id>` — set short_code and/or add/remove tags. */
7
+ /**
8
+ * `al-orc stations edit <id> [options]` — change a station's definition in the orchestrator,
9
+ * field by field, and its tags.
10
+ */
6
11
  export async function editStationHandler(argv) {
7
- const { id, shortCode: short_code, addTag: add_tag, removeTag: remove_tag } = argv;
12
+ const { id, shortCode: short_code, addTag: add_tag, removeTag: remove_tag, json } = argv;
8
13
 
9
14
  const add_tags = add_tag ? [].concat(add_tag) : [];
10
15
  const remove_tags = remove_tag ? [].concat(remove_tag) : [];
11
- const has_tag_changes = add_tags.length > 0 || remove_tags.length > 0;
16
+ const orc = clientFor();
12
17
 
13
- if (!has_tag_changes && short_code === undefined) {
14
- console.error('Usage: al-orc stations edit <id|short_code> --short-code P1 --add-tag <name> [--remove-tag <name>]');
15
- console.error('At least one of --short-code, --add-tag, or --remove-tag is required');
16
- process.exit(1);
17
- }
18
+ const changes_config = (argv.set ?? []).length > 0 || (argv.unset ?? []).length > 0;
19
+ const current = changes_config ? await orc.stations.get(id) : null;
20
+ if (changes_config && !current) throw new Error(`Station not found: ${id}`);
18
21
 
19
- console.log(`Station: ${id}`);
20
- const orc = clientFor();
22
+ const body = definitionBody(argv, current?.config ?? {});
23
+ if (short_code !== undefined) body.short_code = short_code || null;
21
24
 
22
- if (short_code !== undefined) {
23
- await orc.stations.update(id, { short_code: short_code || null });
24
- console.log(`Short code set: ${short_code || '(cleared)'}`);
25
+ if (Object.keys(body).length === 0 && add_tags.length === 0 && remove_tags.length === 0) {
26
+ throw new Error('Nothing to change: give at least one option (see al-orc stations edit --help)');
25
27
  }
26
28
 
27
- if (has_tag_changes) {
29
+ let station = null;
30
+ if (Object.keys(body).length > 0) {
31
+ station = await orc.stations.update(id, body);
32
+ }
33
+ if (add_tags.length > 0 || remove_tags.length > 0) {
28
34
  await editEntityTags(orc.stations, orc.tags, id, add_tags, remove_tags);
29
- console.log('Tags updated');
30
35
  }
36
+
37
+ if (json) {
38
+ console.log(JSON.stringify(station ?? (await orc.stations.get(id)), null, 2));
39
+ return;
40
+ }
41
+ const changed = [...Object.keys(body), ...(add_tags.length || remove_tags.length ? ['tags'] : [])];
42
+ console.log(`Updated ${station?.short_code ?? id}: ${changed.join(', ')}.`);
43
+ printWarnings(station);
31
44
  }
@@ -13,8 +13,8 @@ import { runStationHandler } from './run.js';
13
13
  import { archiveStationHandler, unarchiveStationHandler } from './archive.js';
14
14
  import { deleteStationHandler } from './delete.js';
15
15
  import { editStationHandler } from './edit.js';
16
- import { pullStationsHandler } from './pull.js';
17
- import { pushStationsHandler } from './push.js';
16
+ import { createStationHandler } from './create.js';
17
+ import { definitionOptions } from './definition.js';
18
18
 
19
19
  export function buildStationsSubcommands(yargs) {
20
20
  return yargs
@@ -45,40 +45,32 @@ export function buildStationsSubcommands(yargs) {
45
45
  (y) =>
46
46
  localOptions(y.positional('id', { describe: 'Station ID or short_code', type: 'string' }))
47
47
  .option('set', {
48
- describe: 'Change a config key in every step for this run; repeatable. The value is read as JSON (true, 2, "x"), else text',
48
+ describe: 'Change a config key for this run; repeatable. The value is read as JSON (true, 2, "x"), else text',
49
49
  type: 'string',
50
50
  array: true,
51
51
  })
52
52
  .option('json', { describe: 'Output raw JSON', type: 'boolean' }),
53
53
  safe(runStationHandler),
54
54
  )
55
+ .command(
56
+ 'create <code>',
57
+ 'Create a station: al-orc stations create EM5 --line EM --name "…" --from EM4/output',
58
+ (y) =>
59
+ definitionOptions(localOptions(y.positional('code', { describe: 'Station code, e.g. EM5', type: 'string' })))
60
+ .option('json', { describe: 'Output raw JSON', type: 'boolean' }),
61
+ safe(createStationHandler),
62
+ )
55
63
  .command(
56
64
  'edit <id>',
57
- 'Set a station short_code and/or add/remove tags',
65
+ 'Change a station\'s definition, field by field: config keys, from, schedule, switches, name, code, tags',
58
66
  (y) =>
59
- localOptions(y.positional('id', { describe: 'Station ID or short_code', type: 'string' }))
67
+ definitionOptions(localOptions(y.positional('id', { describe: 'Station ID or short_code', type: 'string' })))
60
68
  .option('short-code', { describe: 'Set the short_code (empty string clears it)', type: 'string' })
61
69
  .option('add-tag', { describe: 'Tag name to add (repeatable)', type: 'string', array: true })
62
- .option('remove-tag', { describe: 'Tag name to remove (repeatable)', type: 'string', array: true }),
70
+ .option('remove-tag', { describe: 'Tag name to remove (repeatable)', type: 'string', array: true })
71
+ .option('json', { describe: 'Output raw JSON', type: 'boolean' }),
63
72
  safe(editStationHandler),
64
73
  )
65
- .command(
66
- 'pull [id]',
67
- 'Pull station definitions to .orchestrator/stations/ (--all for every station)',
68
- (y) =>
69
- localOptions(y.positional('id', { describe: 'Station ID or short_code (omit with --all)', type: 'string' }))
70
- .option('all', { describe: 'Pull every station', type: 'boolean' }),
71
- safe(pullStationsHandler),
72
- )
73
- .command(
74
- 'push [id]',
75
- 'Push local station files to the orchestrator (create/update; --all for every file)',
76
- (y) =>
77
- localOptions(y.positional('id', { describe: 'Filename, station ID, or short_code (omit with --all)', type: 'string' }))
78
- .option('all', { describe: 'Push every local station file', type: 'boolean' })
79
- .option('force', { describe: 'On a 404 update, create remotely with the same id', type: 'boolean' }),
80
- safe(pushStationsHandler),
81
- )
82
74
  .command(
83
75
  'archive <id>',
84
76
  'Archive a station (preserve history, hide from default list, block execution)',
@@ -103,5 +95,5 @@ export function buildStationsSubcommands(yargs) {
103
95
  .option('cascade', { describe: 'Also delete its work records (otherwise refused while any exist)', type: 'boolean' }),
104
96
  safe(deleteStationHandler),
105
97
  )
106
- .demandCommand(1, 'Specify an action: list, show, run, edit, pull, push, archive, unarchive, delete');
98
+ .demandCommand(1, 'Specify an action: list, show, run, create, edit, archive, unarchive, delete');
107
99
  }