Skip to content

Commit 49b5de9

Browse files
committed
implement memory flag
1 parent 876e70f commit 49b5de9

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

sandbox/mod.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export const sandboxCreateCommand = new Command<SandboxContext>()
3030
.option("--cwd <path:string>", "Working directory of the command")
3131
.option("--ssh", "SSH into the sandbox")
3232
.option("--expose-http <port:number>", "Expose the specified port")
33+
.option("--memory <value:string>", "Memory limit for the sandbox")
3334
.arguments("<command...>")
3435
.example(
3536
"Create a sandbox and run a command",
@@ -39,6 +40,10 @@ export const sandboxCreateCommand = new Command<SandboxContext>()
3940
"Copying files from a local directory",
4041
"new --copy ./app",
4142
)
43+
.example(
44+
"Create a sandbox with a custom memory limit",
45+
"new --memory 2gb",
46+
)
4247
.action(async (options, ...command) => {
4348
const quiet = options.lifetime === "session";
4449
const org = await ensureOrg(options, quiet);
@@ -49,6 +54,7 @@ export const sandboxCreateCommand = new Command<SandboxContext>()
4954
token,
5055
org,
5156
lifetime: options.lifetime as `${number}s` | `${number}m` | "session",
57+
memoryMb: parseSize(options.memory),
5258
});
5359
if (options.lifetime === "session" || options.ssh) {
5460
console.log(`Created sandbox with id '${sandbox.id}'`);
@@ -603,6 +609,28 @@ export function formatDuration(ms: number): string {
603609
return str;
604610
}
605611

612+
function parseSize(size: string | undefined): number | undefined {
613+
if (size === undefined) return undefined;
614+
615+
const regex = /^(\d+)(gb|mb)$/i;
616+
const res = regex.exec(size);
617+
618+
if (res === null) {
619+
// error
620+
error(
621+
false,
622+
"Invalid size format. Examples of valid size: '2gb', '1024mb'",
623+
);
624+
}
625+
626+
switch (res[2].toLowerCase()) {
627+
case "gb":
628+
return parseInt(res[1]) * 1024;
629+
case "mb":
630+
return parseInt(res[1]);
631+
}
632+
}
633+
606634
export const sandboxCommand = new Command<GlobalOptions>()
607635
.name("deno sandbox")
608636
.description("Interact with sandboxes")

0 commit comments

Comments
 (0)