@munitoai/cli 0.2.0 → 0.4.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/cli.js CHANGED
@@ -10,28 +10,36 @@ import {
10
10
  DEFAULT_MODEL,
11
11
  DEFAULT_SMALL_MODEL,
12
12
  ENV_KEY,
13
+ ENV_REGISTRY_TOKEN,
13
14
  MissingKeyError,
14
15
  PACKAGES,
15
16
  PLUGINS,
16
17
  PROVIDERS,
18
+ TemplateError,
17
19
  WiringError,
18
20
  authPath,
19
21
  clear,
20
22
  createAgentSandbox,
23
+ deployBody,
21
24
  drop,
25
+ endpoints,
22
26
  findBase,
27
+ findProxy,
23
28
  job,
24
29
  mask,
30
+ opencodeMajor,
31
+ registryAuth,
25
32
  require as require2,
26
33
  resolveHost,
27
34
  runAgent,
28
35
  save,
36
+ waitForBuild,
29
37
  warm,
30
38
  wire
31
- } from "./chunk-7OXZJKC2.js";
39
+ } from "./chunk-7XUGUENM.js";
32
40
 
33
41
  // src/cli.ts
34
- import { runMain } from "citty";
42
+ import { runCommand, runMain } from "citty";
35
43
 
36
44
  // src/core/workspace.ts
37
45
  import { execFile } from "child_process";
@@ -83,8 +91,8 @@ function isBinary(buffer) {
83
91
  return buffer.subarray(0, 8e3).includes(0);
84
92
  }
85
93
  function describeUpload(files) {
86
- const bytes = files.reduce((sum, file) => sum + Buffer.byteLength(file.content), 0);
87
- const size = bytes > 1 << 20 ? `${(bytes / (1 << 20)).toFixed(1)} MiB` : `${Math.ceil(bytes / 1024)} KiB`;
94
+ const bytes2 = files.reduce((sum, file) => sum + Buffer.byteLength(file.content), 0);
95
+ const size = bytes2 > 1 << 20 ? `${(bytes2 / (1 << 20)).toFixed(1)} MiB` : `${Math.ceil(bytes2 / 1024)} KiB`;
88
96
  return `${files.length} files, ${size}`;
89
97
  }
90
98
  async function applyPatch(patch, dir) {
@@ -105,7 +113,7 @@ async function applyPatch(patch, dir) {
105
113
  }
106
114
 
107
115
  // src/cli/main.ts
108
- import { defineCommand as defineCommand8 } from "citty";
116
+ import { defineCommand as defineCommand10 } from "citty";
109
117
 
110
118
  // src/cli/commands/agent.ts
111
119
  import { defineCommand } from "citty";
@@ -230,11 +238,16 @@ function duration(ms) {
230
238
  const minutes = Math.floor(ms / 6e4);
231
239
  return `${minutes} min ${Math.round(ms % 6e4 / 1e3)} s`;
232
240
  }
241
+ function bytes(value) {
242
+ if (value === null || value === void 0) return "-";
243
+ if (value >= 1024 ** 3) return `${(value / 1024 ** 3).toFixed(1)} GiB`;
244
+ return `${Math.max(1, Math.round(value / 1024 ** 2))} MiB`;
245
+ }
233
246
  function table(rows, p, heading = true) {
234
247
  if (!rows.length) return "";
235
- const widths = rows[0].map((_, column) => Math.max(...rows.map((row2) => (row2[column] ?? "").length)));
236
- return rows.map((row2, index) => {
237
- const line = row2.map((cell, column) => (cell ?? "").padEnd(widths[column])).join(" ").trimEnd();
248
+ const widths = rows[0].map((_, column) => Math.max(...rows.map((row3) => (row3[column] ?? "").length)));
249
+ return rows.map((row3, index) => {
250
+ const line = row3.map((cell, column) => (cell ?? "").padEnd(widths[column])).join(" ").trimEnd();
238
251
  return heading && index === 0 ? p.dim(line) : line;
239
252
  }).join("\n");
240
253
  }
@@ -671,10 +684,20 @@ var herdr = defineCommand4({
671
684
  });
672
685
 
673
686
  // src/cli/commands/init.ts
687
+ import { execFile as execFile2 } from "child_process";
674
688
  import { mkdir, writeFile as writeFile2 } from "fs/promises";
675
689
  import { homedir } from "os";
676
690
  import { dirname } from "path";
691
+ import { promisify as promisify2 } from "util";
677
692
  import { defineCommand as defineCommand5 } from "citty";
693
+ async function installedOpencode() {
694
+ try {
695
+ const { stdout } = await promisify2(execFile2)("opencode", ["--version"], { timeout: 1e4 });
696
+ return opencodeMajor(stdout);
697
+ } catch {
698
+ return null;
699
+ }
700
+ }
678
701
  var init = defineCommand5({
679
702
  meta: {
680
703
  name: "init",
@@ -708,6 +731,11 @@ var init = defineCommand5({
708
731
  out2.result("", { agent: name, plugin, guide });
709
732
  return;
710
733
  }
734
+ if (name === "opencode" && (await installedOpencode() ?? 2) < 2) {
735
+ throw new Error(
736
+ "munito init needs OpenCode 2, and this machine has OpenCode 1. See https://opencode.ai/docs/#cli"
737
+ );
738
+ }
711
739
  const wiring = wire(
712
740
  name,
713
741
  args.provider,
@@ -739,6 +767,9 @@ var init = defineCommand5({
739
767
  out2.line(` export ${key}=${shown}`);
740
768
  }
741
769
  }
770
+ if (name === "opencode" && exports.length) {
771
+ out2.line(out2.p.dim(" Then restart the server of OpenCode: opencode service restart"));
772
+ }
742
773
  if (wiring.unset.length && !args["dry-run"]) {
743
774
  out2.line();
744
775
  out2.line(out2.p.dim(` Clear these if your shell sets them: ${wiring.unset.join(", ")}`));
@@ -819,7 +850,7 @@ var mcp = defineCommand6({
819
850
  description: "Run one shell command on a fresh Linux microVM and return its output. The sandbox is deleted afterwards. Use this for code you do not trust, or for a build that must start clean.",
820
851
  inputSchema: {
821
852
  command: z.string().describe("The command, which runs in bash."),
822
- template: z.string().default("base").describe("base, ubuntu, agent or desktop."),
853
+ template: z.string().default("base").describe("base, ubuntu, agent, desktop, or the name of a custom template."),
823
854
  internet: z.boolean().default(false).describe("Allow outbound connections."),
824
855
  timeout: z.number().int().positive().default(120).describe("Seconds the command may take.")
825
856
  }
@@ -860,9 +891,148 @@ var mcp = defineCommand6({
860
891
  }
861
892
  });
862
893
 
894
+ // src/cli/commands/proxies.ts
895
+ import { defineCommand as defineCommand7 } from "citty";
896
+ var proxyArg = {
897
+ proxy: { type: "positional", required: true, description: "The name or the id of the proxy." }
898
+ };
899
+ var create = defineCommand7({
900
+ meta: { name: "create", description: "Create a proxy: fixed public addresses for your sandboxes." },
901
+ args: {
902
+ ...commonArgs,
903
+ name: { type: "positional", required: true, description: "The name: lower case letters, digits and hyphens." },
904
+ dedicated: { type: "boolean", description: "IPv4 addresses that no other customer uses, at a price." }
905
+ },
906
+ async run({ args }) {
907
+ const { client, out: out2 } = await context(args);
908
+ const proxy = await client.createProxy({ name: String(args.name), ipv4: args.dedicated ? "dedicated" : "shared" });
909
+ show(out2, proxy);
910
+ out2.line(out2.p.dim(` munito create --proxy ${proxy.name}`));
911
+ }
912
+ });
913
+ var ls = defineCommand7({
914
+ meta: { name: "ls", description: "List the proxies of your workspace." },
915
+ args: commonArgs,
916
+ async run({ args }) {
917
+ const { client, out: out2 } = await context(args);
918
+ const proxies2 = await client.listProxies();
919
+ for (const proxy of proxies2) out2.event({ type: "proxy", ...proxy });
920
+ if (out2.mode === "json") return;
921
+ if (!proxies2.length) {
922
+ out2.line(out2.p.dim("No proxies. Create one with `munito proxies create <name>`."));
923
+ return;
924
+ }
925
+ const rows = [
926
+ ["NAME", "IPV4", "ADDRESSES", "IPV6 BLOCK", "OPEN PORTS"],
927
+ ...proxies2.map((proxy) => [
928
+ proxy.name,
929
+ proxy.ipv4.mode,
930
+ proxy.ipv4.addresses.join(" ") || "-",
931
+ proxy.ipv6 ?? "-",
932
+ String(proxy.ports.length)
933
+ ])
934
+ ];
935
+ out2.line(table(rows, out2.p));
936
+ }
937
+ });
938
+ var get = defineCommand7({
939
+ meta: { name: "get", description: "Show a proxy, its addresses and its open ports." },
940
+ args: { ...commonArgs, ...proxyArg },
941
+ async run({ args }) {
942
+ const { client, out: out2 } = await context(args);
943
+ show(out2, await findProxy(client, String(args.proxy)));
944
+ }
945
+ });
946
+ var rm2 = defineCommand7({
947
+ meta: { name: "rm", description: "Delete proxies that no running or paused sandbox uses." },
948
+ args: { ...commonArgs, proxies: { type: "positional", required: true, description: "One or more proxies." } },
949
+ async run({ args }) {
950
+ const { client, out: out2 } = await context(args);
951
+ const refs = args._.length ? args._ : [String(args.proxies)];
952
+ for (const ref of refs) {
953
+ const proxy = await findProxy(client, ref);
954
+ await client.deleteProxy(proxy.id);
955
+ out2.event({ type: "proxy.deleted", id: proxy.id, name: proxy.name });
956
+ out2.step("deleted", proxy.name);
957
+ }
958
+ }
959
+ });
960
+ var open = defineCommand7({
961
+ meta: { name: "open", description: "Let the internet reach one port of a sandbox of the proxy." },
962
+ args: {
963
+ ...commonArgs,
964
+ ...proxyArg,
965
+ sandbox: { type: "positional", required: true, description: "The sandbox id." },
966
+ port: { type: "positional", required: true, description: "The port that a server in the sandbox listens on." },
967
+ udp: { type: "boolean", description: "A UDP port. The default is TCP." },
968
+ "public-port": { type: "string", description: "The port outside, for a dedicated proxy. The default is the port." }
969
+ },
970
+ async run({ args }) {
971
+ const { client, out: out2 } = await context(args);
972
+ const proxy = await findProxy(client, String(args.proxy));
973
+ const sandbox = String(args.sandbox);
974
+ const opened = await client.openProxyPort(proxy.id, {
975
+ sandbox_id: sandbox,
976
+ port: Number(args.port),
977
+ protocol: args.udp ? "udp" : "tcp",
978
+ public_port: args["public-port"] ? Number(args["public-port"]) : void 0
979
+ });
980
+ const ipv6 = (await client.getSandbox(sandbox).catch(() => void 0))?.proxy?.ipv6;
981
+ const reach = endpoints(proxy, opened, ipv6);
982
+ out2.event({ type: "proxy.port", ...opened, endpoints: reach });
983
+ out2.step("open", `${opened.protocol} ${opened.port} of ${sandbox}`);
984
+ for (const endpoint of reach) out2.line(` ${endpoint}`);
985
+ out2.result("", { port: opened.id, endpoints: reach });
986
+ }
987
+ });
988
+ var close = defineCommand7({
989
+ meta: { name: "close", description: "Close an open port of a proxy." },
990
+ args: {
991
+ ...commonArgs,
992
+ ...proxyArg,
993
+ port: { type: "positional", required: true, description: "The id of the open port, from `munito proxies get`." }
994
+ },
995
+ async run({ args }) {
996
+ const { client, out: out2 } = await context(args);
997
+ const proxy = await findProxy(client, String(args.proxy));
998
+ await client.closeProxyPort(proxy.id, String(args.port));
999
+ out2.event({ type: "proxy.port.closed", id: String(args.port) });
1000
+ out2.step("closed", String(args.port));
1001
+ }
1002
+ });
1003
+ function show(out2, proxy) {
1004
+ out2.event({ type: "proxy", ...proxy });
1005
+ out2.result("", { proxy: proxy.name, id: proxy.id, ipv4: proxy.ipv4.addresses, ipv6: proxy.ipv6 ?? null });
1006
+ if (out2.mode === "json") return;
1007
+ out2.step(proxy.name, proxy.ipv4.mode);
1008
+ const facts = [
1009
+ ["id", proxy.id],
1010
+ ["ipv4", proxy.ipv4.addresses.join(" ") || "-"],
1011
+ ["ipv6", proxy.ipv6 ?? "-"]
1012
+ ];
1013
+ const lines = table(facts, out2.p, false).split("\n");
1014
+ for (const line of lines) out2.line(` ${line}`);
1015
+ if (!proxy.ports.length) return;
1016
+ const rows = [
1017
+ ["PORT ID", "SANDBOX", "PROTOCOL", "PORT", "PUBLIC PORT"],
1018
+ ...proxy.ports.map((port2) => [
1019
+ port2.id,
1020
+ port2.sandbox_id,
1021
+ port2.protocol,
1022
+ String(port2.port),
1023
+ String(port2.public_port)
1024
+ ])
1025
+ ];
1026
+ for (const line of table(rows, out2.p).split("\n")) out2.line(` ${line}`);
1027
+ }
1028
+ var proxies = defineCommand7({
1029
+ meta: { name: "proxies", description: "Give sandboxes fixed public addresses. A preview." },
1030
+ subCommands: { create, ls, get, rm: rm2, open, close }
1031
+ });
1032
+
863
1033
  // src/cli/commands/sandboxes.ts
864
1034
  import { spawn as spawn3 } from "child_process";
865
- import { defineCommand as defineCommand7 } from "citty";
1035
+ import { defineCommand as defineCommand8 } from "citty";
866
1036
 
867
1037
  // src/core/ssh.ts
868
1038
  var LOOPBACK = "127.0.0.1";
@@ -898,7 +1068,7 @@ function sshArgv(user, host, forwards = []) {
898
1068
  }
899
1069
 
900
1070
  // src/cli/commands/sandboxes.ts
901
- var ls = defineCommand7({
1071
+ var ls2 = defineCommand8({
902
1072
  meta: { name: "ls", description: "List your sandboxes." },
903
1073
  args: { ...commonArgs, all: { type: "boolean", alias: "a", description: "Stopped ones too." } },
904
1074
  async run({ args }) {
@@ -915,28 +1085,36 @@ var ls = defineCommand7({
915
1085
  out2.line(table(rows, out2.p));
916
1086
  }
917
1087
  });
1088
+ var PROXY_FLAG = "The name or the id of a proxy: the sandbox gets its fixed public addresses. A preview.";
918
1089
  function row(box) {
919
1090
  const age = box.created_at ? duration(Date.now() - box.created_at * 1e3) : "";
920
1091
  return [box.id, box.status, box.template, `${box.vcpus}c ${Math.round(box.memory_mib / 1024)}g`, age];
921
1092
  }
922
- var run2 = defineCommand7({
1093
+ var run2 = defineCommand8({
923
1094
  meta: { name: "run", description: "Run one command in a throwaway sandbox." },
924
1095
  args: {
925
1096
  ...commonArgs,
926
1097
  command: { type: "positional", required: true, description: "The command to run." },
927
- template: { type: "string", description: "A template. The default is base.", default: "base" },
1098
+ template: {
1099
+ type: "string",
1100
+ description: "base, ubuntu, agent, desktop, or the name of a custom template. The default is base.",
1101
+ default: "base"
1102
+ },
928
1103
  size: { type: "string", description: "tiny, small, default or large.", default: "small" },
929
1104
  internet: { type: "boolean", description: "Allow outbound connections." },
1105
+ proxy: { type: "string", description: PROXY_FLAG },
930
1106
  timeout: { type: "string", description: "Seconds the command may take." }
931
1107
  },
932
1108
  async run({ args }) {
933
1109
  const { client, out: out2 } = await context(args);
1110
+ const proxy = args.proxy ? (await findProxy(client, args.proxy)).id : void 0;
934
1111
  const box = await client.createSandbox({
935
1112
  template: args.template,
936
1113
  size: args.size,
937
1114
  internet: Boolean(args.internet),
938
1115
  timeout: 600,
939
- metadata: { "munito.cli": "run" }
1116
+ metadata: { "munito.cli": "run" },
1117
+ proxy
940
1118
  });
941
1119
  try {
942
1120
  const exit = await stream(client, box.id, args.command, out2, args.timeout);
@@ -947,7 +1125,7 @@ var run2 = defineCommand7({
947
1125
  }
948
1126
  }
949
1127
  });
950
- var exec = defineCommand7({
1128
+ var exec = defineCommand8({
951
1129
  meta: { name: "exec", description: "Run a command in a sandbox you already have." },
952
1130
  args: {
953
1131
  ...commonArgs,
@@ -977,19 +1155,19 @@ async function stream(client, id, command, out2, timeout, cwd) {
977
1155
  }
978
1156
  return exit;
979
1157
  }
980
- var rm2 = defineCommand7({
1158
+ var rm3 = defineCommand8({
981
1159
  meta: { name: "rm", description: "Delete sandboxes." },
982
1160
  args: { ...commonArgs, ids: { type: "positional", required: true, description: "One or more sandbox ids." } },
983
- async run({ args, rawArgs }) {
1161
+ async run({ args, rawArgs: rawArgs2 }) {
984
1162
  const { client, out: out2 } = await context(args);
985
- const ids = rawArgs.filter((value) => value.startsWith("sbx_"));
1163
+ const ids = rawArgs2.filter((value) => value.startsWith("sbx_"));
986
1164
  for (const id of ids.length ? ids : [args.ids]) {
987
1165
  await client.deleteSandbox(id);
988
1166
  out2.step("deleted", id);
989
1167
  }
990
1168
  }
991
1169
  });
992
- var pause = defineCommand7({
1170
+ var pause = defineCommand8({
993
1171
  meta: { name: "pause", description: "Pause a persistent sandbox." },
994
1172
  args: { ...commonArgs, id: { type: "positional", required: true, description: "The sandbox id." } },
995
1173
  async run({ args }) {
@@ -998,7 +1176,7 @@ var pause = defineCommand7({
998
1176
  out2.step("paused", box.id);
999
1177
  }
1000
1178
  });
1001
- var resume = defineCommand7({
1179
+ var resume = defineCommand8({
1002
1180
  meta: { name: "resume", description: "Resume a paused sandbox." },
1003
1181
  args: { ...commonArgs, id: { type: "positional", required: true, description: "The sandbox id." } },
1004
1182
  async run({ args }) {
@@ -1007,7 +1185,7 @@ var resume = defineCommand7({
1007
1185
  out2.step("resumed", box.id);
1008
1186
  }
1009
1187
  });
1010
- var fork = defineCommand7({
1188
+ var fork = defineCommand8({
1011
1189
  meta: { name: "fork", description: "Start new sandboxes from a persistent one, as it is now." },
1012
1190
  args: {
1013
1191
  ...commonArgs,
@@ -1025,7 +1203,7 @@ var fork = defineCommand7({
1025
1203
  out2.result("", { forks: forks.map((box) => box.id) });
1026
1204
  }
1027
1205
  });
1028
- var ssh = defineCommand7({
1206
+ var ssh = defineCommand8({
1029
1207
  meta: { name: "ssh", description: "Open a shell in a sandbox." },
1030
1208
  args: {
1031
1209
  ...commonArgs,
@@ -1059,7 +1237,7 @@ var ssh = defineCommand7({
1059
1237
  );
1060
1238
  }
1061
1239
  });
1062
- var open = defineCommand7({
1240
+ var open2 = defineCommand8({
1063
1241
  meta: { name: "open", description: "Open a sandbox port, or its desktop, in your browser." },
1064
1242
  args: {
1065
1243
  ...commonArgs,
@@ -1082,11 +1260,15 @@ var open = defineCommand7({
1082
1260
  openInBrowser(signed.url);
1083
1261
  }
1084
1262
  });
1085
- var create = defineCommand7({
1263
+ var create2 = defineCommand8({
1086
1264
  meta: { name: "create", description: "Create a sandbox and leave it running." },
1087
1265
  args: {
1088
1266
  ...commonArgs,
1089
- template: { type: "string", description: "base, ubuntu, agent or desktop.", default: AGENT_TEMPLATE },
1267
+ template: {
1268
+ type: "string",
1269
+ description: "base, ubuntu, agent, desktop, or the name of a custom template.",
1270
+ default: AGENT_TEMPLATE
1271
+ },
1090
1272
  agent: { type: "string", description: `Point one harness at a provider: ${AGENTS.join(", ")}.` },
1091
1273
  provider: { type: "string", description: `Where the model comes from: ${PROVIDERS.join(", ")}.` },
1092
1274
  auth: { type: "string", description: "token or subscription." },
@@ -1094,10 +1276,13 @@ var create = defineCommand7({
1094
1276
  size: { type: "string", description: "tiny, small, default or large.", default: "default" },
1095
1277
  internet: { type: "boolean", description: "Allow outbound connections.", default: true },
1096
1278
  persistent: { type: "boolean", description: "Keep files and processes across a pause." },
1279
+ proxy: { type: "string", description: PROXY_FLAG },
1097
1280
  timeout: { type: "string", description: "Seconds the sandbox may live." }
1098
1281
  },
1099
1282
  async run({ args }) {
1100
1283
  const { client, out: out2 } = await context(args);
1284
+ if (args.proxy && args.agent) throw new Error("A sandbox with --agent takes no --proxy yet.");
1285
+ const proxy = args.proxy ? (await findProxy(client, args.proxy)).id : void 0;
1101
1286
  const common = {
1102
1287
  template: args.template,
1103
1288
  size: args.size,
@@ -1112,16 +1297,172 @@ var create = defineCommand7({
1112
1297
  auth: args.auth,
1113
1298
  model: args.model,
1114
1299
  metadata: { "munito.cli": "create", "munito.agent": args.agent }
1115
- }) : await client.createSandbox({ ...common, metadata: { "munito.cli": "create" } });
1300
+ }) : await client.createSandbox({ ...common, metadata: { "munito.cli": "create" }, proxy });
1116
1301
  out2.step("created", box.id);
1302
+ if (box.proxy?.ipv6) out2.line(out2.p.dim(` ipv6 ${box.proxy.ipv6}`));
1117
1303
  out2.line(out2.p.dim(` munito ssh ${box.id}`));
1118
- out2.result("", { sandbox_id: box.id });
1304
+ out2.result("", { sandbox_id: box.id, ...box.proxy ? { proxy: box.proxy } : {} });
1305
+ }
1306
+ });
1307
+
1308
+ // src/cli/commands/templates.ts
1309
+ import { defineCommand as defineCommand9 } from "citty";
1310
+ var str2 = (value) => typeof value === "string" && value ? value : void 0;
1311
+ var nameArg2 = {
1312
+ name: { type: "positional", required: true, description: "The name or the id of the template." }
1313
+ };
1314
+ async function readStdin() {
1315
+ if (process.stdin.isTTY) throw new TemplateError(`Pipe the token to --password-stdin, or set ${ENV_REGISTRY_TOKEN}.`);
1316
+ const chunks = [];
1317
+ for await (const chunk of process.stdin) chunks.push(Buffer.from(chunk));
1318
+ return Buffer.concat(chunks).toString("utf8");
1319
+ }
1320
+ var deploy = defineCommand9({
1321
+ meta: {
1322
+ name: "deploy",
1323
+ description: "Build a container image into a template. A name that exists gets a new version."
1324
+ },
1325
+ args: {
1326
+ ...commonArgs,
1327
+ name: {
1328
+ type: "positional",
1329
+ required: true,
1330
+ description: "The template name: lower case letters, digits and hyphens."
1331
+ },
1332
+ image: { type: "positional", required: true, description: "The image, such as ghcr.io/acme/agent:1.4." },
1333
+ username: { type: "string", description: "The user name of a private registry." },
1334
+ "password-stdin": {
1335
+ type: "boolean",
1336
+ description: `Read the registry password or token from standard input. Or set ${ENV_REGISTRY_TOKEN}.`
1337
+ },
1338
+ wait: { type: "boolean", description: "Wait until the build ends. Exit 1 when it fails." }
1339
+ },
1340
+ async run({ args }) {
1341
+ if (args.password !== void 0) {
1342
+ throw new TemplateError(`There is no --password. Use --password-stdin, or set ${ENV_REGISTRY_TOKEN}.`);
1343
+ }
1344
+ const { auth, ignored } = registryAuth({
1345
+ username: str2(args.username),
1346
+ stdin: args["password-stdin"] ? await readStdin() : void 0,
1347
+ env: process.env[ENV_REGISTRY_TOKEN]
1348
+ });
1349
+ const body = deployBody(String(args.name), String(args.image), auth);
1350
+ const { client, out: out2 } = await context(args);
1351
+ if (ignored) out2.warn(`${ENV_REGISTRY_TOKEN} is set without --username, so the deploy sends no credential.`);
1352
+ const template = await client.deployTemplate(body);
1353
+ out2.event({ type: "template", ...template });
1354
+ out2.step("deploying", `${template.name} v${template.build.version} ${template.build.image}`);
1355
+ if (args.wait) {
1356
+ await follow(client, out2, template.id);
1357
+ return;
1358
+ }
1359
+ out2.line(out2.p.dim(` munito templates get ${template.name} --wait`));
1360
+ out2.result("", { template: template.name, id: template.id, version: template.build.version });
1361
+ }
1362
+ });
1363
+ var ls3 = defineCommand9({
1364
+ meta: { name: "ls", description: "List the templates of your workspace." },
1365
+ args: commonArgs,
1366
+ async run({ args }) {
1367
+ const { client, out: out2 } = await context(args);
1368
+ const templates2 = await client.listTemplates();
1369
+ for (const template of templates2) out2.event({ type: "template", ...template });
1370
+ if (out2.mode === "json") return;
1371
+ if (!templates2.length) {
1372
+ out2.line(out2.p.dim("No templates. Deploy one with `munito templates deploy <name> <image>`."));
1373
+ return;
1374
+ }
1375
+ const rows = [["NAME", "STATUS", "VERSION", "IMAGE", "SIZE", "NEWEST BUILD"], ...templates2.map(row2)];
1376
+ out2.line(table(rows, out2.p));
1377
+ }
1378
+ });
1379
+ function row2(template) {
1380
+ const serving = template.version ? `v${template.version}` : "-";
1381
+ const { build } = template;
1382
+ return [
1383
+ template.name,
1384
+ template.status,
1385
+ serving,
1386
+ template.image ?? "-",
1387
+ bytes(template.size_bytes),
1388
+ `v${build.version} ${build.status}`
1389
+ ];
1390
+ }
1391
+ var get2 = defineCommand9({
1392
+ meta: { name: "get", description: "Show a template and its newest build." },
1393
+ args: {
1394
+ ...commonArgs,
1395
+ ...nameArg2,
1396
+ wait: { type: "boolean", description: "Wait until the newest build ends. Exit 1 when it fails." }
1397
+ },
1398
+ async run({ args }) {
1399
+ const { client, out: out2 } = await context(args);
1400
+ if (args.wait) {
1401
+ await follow(client, out2, String(args.name));
1402
+ return;
1403
+ }
1404
+ show2(out2, await client.getTemplate(String(args.name)));
1119
1405
  }
1120
1406
  });
1407
+ var rm4 = defineCommand9({
1408
+ meta: { name: "rm", description: "Delete templates. Their running sandboxes keep running." },
1409
+ args: { ...commonArgs, names: { type: "positional", required: true, description: "One or more template names." } },
1410
+ async run({ args }) {
1411
+ const { client, out: out2 } = await context(args);
1412
+ const names = args._.length ? args._ : [String(args.names)];
1413
+ for (const name of names) {
1414
+ await client.deleteTemplate(name);
1415
+ out2.event({ type: "template.deleted", name });
1416
+ out2.step("deleted", name);
1417
+ }
1418
+ }
1419
+ });
1420
+ async function follow(client, out2, ref) {
1421
+ const template = await waitForBuild(client, ref, {
1422
+ onEvent: (event) => {
1423
+ out2.event({ ...event });
1424
+ out2.step(event.status, `${event.name} v${event.version}`);
1425
+ }
1426
+ });
1427
+ show2(out2, template);
1428
+ if (template.build.status === "failed") process.exitCode = 1;
1429
+ }
1430
+ function show2(out2, template) {
1431
+ out2.event({ type: "template", ...template });
1432
+ const { build } = template;
1433
+ out2.result("", {
1434
+ template: template.name,
1435
+ id: template.id,
1436
+ status: template.status,
1437
+ version: template.version ?? null,
1438
+ build: { version: build.version, status: build.status }
1439
+ });
1440
+ if (out2.mode === "json") return;
1441
+ out2.step(template.name, template.status);
1442
+ const facts = [
1443
+ ["id", template.id],
1444
+ ["serving", template.version ? `v${template.version} ${template.image ?? ""}` : "none yet"],
1445
+ ["size", bytes(template.size_bytes)],
1446
+ ["digest", template.digest ?? "-"],
1447
+ ["build", `v${build.version} ${build.status} ${build.image}`]
1448
+ ];
1449
+ if (build.error) facts.push(["error", build.error.message]);
1450
+ out2.line(
1451
+ table(facts, out2.p, false).split("\n").map((line) => ` ${line}`).join("\n")
1452
+ );
1453
+ if (build.status === "failed" && build.log) {
1454
+ out2.line(out2.p.dim(" the end of the build output:"));
1455
+ for (const line of build.log.replace(/\n$/, "").split("\n")) out2.line(out2.p.dim(` ${line}`));
1456
+ }
1457
+ }
1458
+ var templates = defineCommand9({
1459
+ meta: { name: "templates", description: "Start sandboxes from your own container image." },
1460
+ subCommands: { deploy, ls: ls3, get: get2, rm: rm4 }
1461
+ });
1121
1462
 
1122
1463
  // src/cli/main.ts
1123
- var VERSION = true ? "0.2.0" : "0.0.0-dev";
1124
- var main = defineCommand8({
1464
+ var VERSION = true ? "0.4.0" : "0.0.0-dev";
1465
+ var main = defineCommand10({
1125
1466
  meta: {
1126
1467
  name: "munito",
1127
1468
  // Without this, `munito --version` exits with "No version specified".
@@ -1137,16 +1478,18 @@ var main = defineCommand8({
1137
1478
  login,
1138
1479
  logout,
1139
1480
  whoami,
1140
- ls,
1141
- create,
1481
+ ls: ls2,
1482
+ create: create2,
1142
1483
  run: run2,
1143
1484
  exec,
1144
1485
  ssh,
1145
- open,
1146
- rm: rm2,
1486
+ open: open2,
1487
+ rm: rm3,
1147
1488
  pause,
1148
1489
  resume,
1149
- fork
1490
+ fork,
1491
+ templates,
1492
+ proxies
1150
1493
  }
1151
1494
  });
1152
1495
 
@@ -1158,7 +1501,7 @@ function report2(error) {
1158
1501
  channel2.error(error.message);
1159
1502
  return 2;
1160
1503
  }
1161
- if (error instanceof WiringError || error instanceof WorkspaceError) {
1504
+ if (error instanceof WiringError || error instanceof WorkspaceError || error instanceof TemplateError) {
1162
1505
  channel2.error(error.message);
1163
1506
  return 2;
1164
1507
  }
@@ -1176,7 +1519,12 @@ function report2(error) {
1176
1519
  process.on("unhandledRejection", (reason) => {
1177
1520
  process.exit(report2(reason));
1178
1521
  });
1179
- await runMain(main).catch((error) => {
1180
- process.exit(report2(error));
1181
- });
1522
+ var rawArgs = process.argv.slice(2);
1523
+ var own = rawArgs.some((arg) => arg === "--help" || arg === "-h") || rawArgs.length === 1 && (rawArgs[0] === "--version" || rawArgs[0] === "-v");
1524
+ if (own) await runMain(main);
1525
+ else
1526
+ await runCommand(main, { rawArgs }).catch(async (error) => {
1527
+ if (error instanceof Error && error.name === "CLIError") await runMain(main);
1528
+ process.exit(report2(error));
1529
+ });
1182
1530
  //# sourceMappingURL=cli.js.map