@termix-ssh/cli 1.2.0 → 1.3.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/dist/index.js CHANGED
@@ -29307,7 +29307,7 @@ function unavailableFor(pluginId, reason) {
29307
29307
  }
29308
29308
 
29309
29309
  // src/version.ts
29310
- var CLI_VERSION = true ? "1.2.0" : "0.0.0-dev";
29310
+ var CLI_VERSION = true ? "1.3.0" : "0.0.0-dev";
29311
29311
 
29312
29312
  // src/core/http.ts
29313
29313
  var MAX_ATTEMPTS = 3;
@@ -32729,13 +32729,17 @@ function registerApiKeyCommands(program3) {
32729
32729
  });
32730
32730
  keys.command("create").description(
32731
32731
  "Create an API key for a user. The token is shown once and cannot be retrieved again."
32732
- ).requiredOption("--name <name>", "Human-readable name for the key").requiredOption("--user <userId>", "Id of the user the key acts as").option("--expires-at <date>", "Expiry as an ISO date; omit for no expiry").action(async function(opts) {
32732
+ ).requiredOption("--name <name>", "Human-readable name for the key").requiredOption("--user <userId>", "Id of the user the key acts as").option("--expires-at <date>", "Expiry as an ISO date; omit for no expiry").option(
32733
+ "--scope <scope>",
32734
+ "Limit the key to actions: actions-read or actions-run. Omit for full access"
32735
+ ).action(async function(opts) {
32733
32736
  await run(async () => {
32734
32737
  if (opts.expiresAt && Number.isNaN(Date.parse(opts.expiresAt))) {
32735
32738
  throw new UsageError(
32736
32739
  `Invalid --expires-at: "${opts.expiresAt}" (expected an ISO date).`
32737
32740
  );
32738
32741
  }
32742
+ const scopes = opts.scope ? [scopeOf(opts.scope)] : void 0;
32739
32743
  const { client } = await createContext(this);
32740
32744
  const created = await client.request({
32741
32745
  method: "POST",
@@ -32743,7 +32747,8 @@ function registerApiKeyCommands(program3) {
32743
32747
  data: {
32744
32748
  name: opts.name,
32745
32749
  userId: opts.user,
32746
- ...opts.expiresAt ? { expiresAt: opts.expiresAt } : {}
32750
+ ...opts.expiresAt ? { expiresAt: opts.expiresAt } : {},
32751
+ ...scopes ? { scopes } : {}
32747
32752
  }
32748
32753
  });
32749
32754
  if (isJsonMode()) {
@@ -32767,6 +32772,21 @@ function registerApiKeyCommands(program3) {
32767
32772
  });
32768
32773
  });
32769
32774
  }
32775
+ function scopeOf(value) {
32776
+ const scopes = {
32777
+ "actions-read": "operations:read",
32778
+ "actions-run": "operations:run",
32779
+ "operations:read": "operations:read",
32780
+ "operations:run": "operations:run"
32781
+ };
32782
+ const scope = scopes[value];
32783
+ if (!scope) {
32784
+ throw new UsageError(
32785
+ `Invalid --scope "${value}" (expected actions-read or actions-run).`
32786
+ );
32787
+ }
32788
+ return scope;
32789
+ }
32770
32790
 
32771
32791
  // src/commands/misc.ts
32772
32792
  var STATUS_COLUMNS = [
@@ -32946,6 +32966,242 @@ function registerApiCommands(program3) {
32946
32966
  });
32947
32967
  }
32948
32968
 
32969
+ // src/commands/ops.ts
32970
+ var OPS_EXIT = {
32971
+ FAILED: 1,
32972
+ NEEDS_APPROVAL: 9,
32973
+ PARTIAL: 10
32974
+ };
32975
+ function collectInput2(value, previous) {
32976
+ const index = value.indexOf("=");
32977
+ if (index <= 0) {
32978
+ throw new UsageError(`Invalid --input "${value}" (expected KEY=VALUE).`);
32979
+ }
32980
+ const raw = value.slice(index + 1);
32981
+ let parsed = raw;
32982
+ if (/^(true|false|null|-?\d+(\.\d+)?|\{.*\}|\[.*\])$/s.test(raw)) {
32983
+ try {
32984
+ parsed = JSON.parse(raw);
32985
+ } catch {
32986
+ parsed = raw;
32987
+ }
32988
+ }
32989
+ return { ...previous, [value.slice(0, index)]: parsed };
32990
+ }
32991
+ function collectHosts(value, previous) {
32992
+ return [
32993
+ ...previous,
32994
+ ...value.split(",").map((part) => parseId(part.trim(), "host id"))
32995
+ ];
32996
+ }
32997
+ function exitFor(outcomes) {
32998
+ if (outcomes.length === 0) return 0;
32999
+ const failed = outcomes.filter(
33000
+ (outcome) => outcome.status !== "success" && outcome.status !== "pending_approval"
33001
+ ).length;
33002
+ const waiting = outcomes.filter(
33003
+ (outcome) => outcome.status === "pending_approval"
33004
+ ).length;
33005
+ if (failed === outcomes.length) return OPS_EXIT.FAILED;
33006
+ if (failed > 0) return OPS_EXIT.PARTIAL;
33007
+ if (waiting > 0) return OPS_EXIT.NEEDS_APPROVAL;
33008
+ return 0;
33009
+ }
33010
+ var ACTION_COLUMNS = [
33011
+ { header: "id", value: (a) => a.id },
33012
+ { header: "title", value: (a) => a.title },
33013
+ { header: "plugin", value: (a) => a.pluginName },
33014
+ { header: "on", value: (a) => a.scope },
33015
+ { header: "risk", value: (a) => a.risk }
33016
+ ];
33017
+ var RUN_COLUMNS = [
33018
+ { header: "id", value: (r) => String(r.id).slice(0, 8) },
33019
+ { header: "action", value: (r) => r.actionId },
33020
+ { header: "host", value: (r) => r.hostName },
33021
+ { header: "status", value: (r) => r.status },
33022
+ { header: "by", value: (r) => r.username },
33023
+ { header: "when", value: (r) => r.createdAt }
33024
+ ];
33025
+ var POLL_MS = 1500;
33026
+ function registerOpsCommands(program3) {
33027
+ const ops = program3.command("ops").alias("actions").description(
33028
+ "Run and track the actions plugins offer, such as restarting a container, on one host or many."
33029
+ );
33030
+ ops.command("list").description("List the actions you can run").option("--search <text>", "Only actions whose id, title or plugin match").action(async function(opts) {
33031
+ await run(async () => {
33032
+ const { client } = await createContext(this);
33033
+ const catalog = await client.request({
33034
+ method: "GET",
33035
+ path: "/operations/catalog",
33036
+ params: { surface: "cli" }
33037
+ });
33038
+ const text = opts.search?.toLowerCase() ?? "";
33039
+ const rows = catalog.actions.filter(
33040
+ (action) => action.available && (!text || `${action.id} ${action.title} ${action.pluginName}`.toLowerCase().includes(text))
33041
+ );
33042
+ printList(
33043
+ rows,
33044
+ ACTION_COLUMNS,
33045
+ {
33046
+ quietField: "id",
33047
+ jsonWrapper: (actions) => ({ actions })
33048
+ }
33049
+ );
33050
+ });
33051
+ });
33052
+ ops.command("describe <id>").description("Show what an action needs").action(async function(id) {
33053
+ await run(async () => {
33054
+ const { client } = await createContext(this);
33055
+ const action = await client.request({
33056
+ method: "GET",
33057
+ path: `/operations/catalog/${encodeURIComponent(id)}`
33058
+ });
33059
+ printRecord(action);
33060
+ });
33061
+ });
33062
+ ops.command("run <id>").description(
33063
+ "Run an action. Pick hosts with --host. Changes may wait for approval, which exits 9."
33064
+ ).option(
33065
+ "--host <ids>",
33066
+ "Host id, or several separated by commas; repeatable",
33067
+ collectHosts,
33068
+ []
33069
+ ).option("--target <key>", "The item on each host, such as a container name").option(
33070
+ "--input <KEY=VALUE>",
33071
+ "An input value; repeatable",
33072
+ collectInput2,
33073
+ {}
33074
+ ).option("-d, --data <json>", "All inputs as a JSON object").option("--data-file <path>", "Read the inputs from a file, or - for stdin").option("--dry-run", "Report what would happen without changing anything").option("--yes", "Confirm a destructive action").action(async function(id, opts) {
33075
+ await run(async () => {
33076
+ const body = parseApiBody(opts);
33077
+ if (body !== void 0 && (typeof body !== "object" || Array.isArray(body))) {
33078
+ throw new UsageError("The inputs must be a JSON object.");
33079
+ }
33080
+ const input = {
33081
+ ...body ?? {},
33082
+ ...opts.input
33083
+ };
33084
+ const { client } = await createContext(this);
33085
+ const hosts = opts.host.length > 0 ? opts.host : [void 0];
33086
+ const outcomes = [];
33087
+ for (const hostId of hosts) {
33088
+ const outcome = await client.request({
33089
+ method: "POST",
33090
+ path: `/operations/${encodeURIComponent(id)}/run`,
33091
+ data: {
33092
+ hostId,
33093
+ resourceKey: opts.target,
33094
+ input,
33095
+ dryRun: opts.dryRun === true,
33096
+ confirmed: opts.yes === true
33097
+ }
33098
+ });
33099
+ outcomes.push({ ...outcome, hostId });
33100
+ }
33101
+ if (hosts.length === 1) {
33102
+ printRecord(outcomes[0], {
33103
+ quietField: "runId"
33104
+ });
33105
+ } else {
33106
+ printList(
33107
+ outcomes,
33108
+ [
33109
+ { header: "host", value: (o) => o.hostId },
33110
+ { header: "status", value: (o) => o.status },
33111
+ { header: "result", value: (o) => o.summary ?? o.error }
33112
+ ],
33113
+ { jsonWrapper: (results) => ({ results }) }
33114
+ );
33115
+ }
33116
+ process.exitCode = exitFor(outcomes);
33117
+ });
33118
+ });
33119
+ ops.command("runs").description("List recent runs").option("--host <id>", "Only runs on this host").option("--status <status>", "Only runs with this status").option("--limit <n>", "How many", "50").action(async function(opts) {
33120
+ await run(async () => {
33121
+ const { client } = await createContext(this);
33122
+ const { runs } = await client.request({
33123
+ method: "GET",
33124
+ path: "/operations/runs",
33125
+ params: {
33126
+ hostId: opts.host ? parseId(opts.host, "host id") : void 0,
33127
+ status: opts.status,
33128
+ limit: opts.limit
33129
+ }
33130
+ });
33131
+ printList(runs, RUN_COLUMNS, {
33132
+ quietField: "id",
33133
+ jsonWrapper: (rows) => ({ runs: rows })
33134
+ });
33135
+ });
33136
+ });
33137
+ ops.command("status <runId>").description("Show one run. --wait waits until it finishes.").option("--wait", "Wait for the run to finish").action(async function(runId, opts) {
33138
+ await run(async () => {
33139
+ const { client } = await createContext(this);
33140
+ const get = () => client.request({
33141
+ method: "GET",
33142
+ path: `/operations/runs/${encodeURIComponent(runId)}`
33143
+ });
33144
+ let current = await get();
33145
+ while (opts.wait && (current.status === "running" || current.status === "pending_approval")) {
33146
+ await new Promise((resolve) => setTimeout(resolve, POLL_MS));
33147
+ current = await get();
33148
+ }
33149
+ printRecord(current);
33150
+ process.exitCode = exitFor([current]);
33151
+ });
33152
+ });
33153
+ ops.command("approvals").description("List requests waiting for approval").action(async function() {
33154
+ await run(async () => {
33155
+ const { client } = await createContext(this);
33156
+ const { approvals } = await client.request({ method: "GET", path: "/operations/approvals" });
33157
+ printList(
33158
+ approvals.map(
33159
+ (approval) => ({ ...approval.run, approvalId: approval.id })
33160
+ ),
33161
+ [
33162
+ { header: "approval", value: (r) => r.approvalId },
33163
+ { header: "action", value: (r) => r.actionId },
33164
+ { header: "host", value: (r) => r.hostName },
33165
+ { header: "by", value: (r) => r.username }
33166
+ ],
33167
+ {
33168
+ quietField: "approvalId",
33169
+ jsonWrapper: (rows) => ({ approvals: rows })
33170
+ }
33171
+ );
33172
+ });
33173
+ });
33174
+ for (const [name, approve] of [
33175
+ ["approve", true],
33176
+ ["reject", false]
33177
+ ]) {
33178
+ ops.command(`${name} <approvalId>`).description(
33179
+ approve ? "Approve a waiting request and run it. Needs a signed in session, not an API key." : "Reject a waiting request"
33180
+ ).option("--comment <text>", "A note kept with the decision").action(async function(approvalId, opts) {
33181
+ await run(async () => {
33182
+ const { client } = await createContext(this);
33183
+ const outcome = await client.request({
33184
+ method: "POST",
33185
+ path: `/operations/approvals/${encodeURIComponent(approvalId)}`,
33186
+ data: { approve, comment: opts.comment }
33187
+ });
33188
+ printRecord(outcome);
33189
+ if (approve) process.exitCode = exitFor([outcome]);
33190
+ });
33191
+ });
33192
+ }
33193
+ ops.command("cancel <runId>").description("Stop a running action, or withdraw one waiting for approval").action(async function(runId) {
33194
+ await run(async () => {
33195
+ const { client } = await createContext(this);
33196
+ const result = await client.request({
33197
+ method: "POST",
33198
+ path: `/operations/runs/${encodeURIComponent(runId)}/cancel`
33199
+ });
33200
+ printJson(result);
33201
+ });
33202
+ });
33203
+ }
33204
+
32949
33205
  // src/index.ts
32950
33206
  var program2 = new Command();
32951
33207
  program2.name("termix").description(
@@ -32970,6 +33226,7 @@ registerAdminCommands(program2);
32970
33226
  registerApiKeyCommands(program2);
32971
33227
  registerMiscCommands(program2);
32972
33228
  registerPluginCommands(program2);
33229
+ registerOpsCommands(program2);
32973
33230
  registerApiCommands(program2);
32974
33231
  program2.exitOverride((error) => {
32975
33232
  if (error.code === "commander.help" || error.code === "commander.helpDisplayed" || error.code === "commander.version") {