Skip to content

Commit 616e0a3

Browse files
committed
fix: better byte size parsing
1 parent 4af409e commit 616e0a3

3 files changed

Lines changed: 23 additions & 12 deletions

File tree

sandbox/mod.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { join } from "@std/path";
77
import { Spinner } from "@std/cli/unstable-spinner";
88

99
import { getAppFromConfig, readConfig, writeConfig } from "../config.ts";
10-
import { error, parseSize, renderTemporalTimestamp, withApp } from "../util.ts";
10+
import { error, parseSizeToMib, renderTemporalTimestamp, withApp } from "../util.ts";
1111
import { createTrpcClient, getAuth } from "../auth.ts";
1212
import type { GlobalOptions } from "../main.ts";
1313
import token_storage from "../token_storage.ts";
@@ -54,7 +54,7 @@ export const sandboxCreateCommand = new Command<SandboxContext>()
5454
token,
5555
org,
5656
lifetime: options.lifetime as `${number}s` | `${number}m` | "session",
57-
memoryMb: parseSize(options.memory),
57+
memoryMb: parseSizeToMib(options.memory),
5858
});
5959
if (options.lifetime === "session" || options.ssh) {
6060
console.log(`Created sandbox with id '${sandbox.id}'`);

sandbox/volumes.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Command } from "@cliffy/command";
22
import { ensureOrg, type SandboxContext } from "./mod.ts";
33
import { getAuth } from "../auth.ts";
44
import { Client } from "@deno/sandbox";
5-
import { formatSize, parseSize, tablePrinter } from "../util.ts";
5+
import { formatSize, parseSizeToMib, tablePrinter } from "../util.ts";
66

77
export const volumesCreateCommand = new Command<SandboxContext>()
88
.description("Create a volume")
@@ -23,7 +23,7 @@ export const volumesCreateCommand = new Command<SandboxContext>()
2323

2424
const volume = await client.volumes.create({
2525
slug: name,
26-
capacity: parseSize(options.capacity)!,
26+
capacity: parseSizeToMib(options.capacity)!,
2727
region: options.region,
2828
});
2929

util.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ export function formatSize(bytes: number): string {
194194
return `${bytes} Bytes`;
195195
}
196196

197-
export function parseSize(size: string | undefined): number | undefined {
197+
export function parseSizeToMib(size: string | undefined): number | undefined {
198198
if (size === undefined) return undefined;
199199

200200
const match = size.match(/^(\d+)(GB|MB|KB|GiB|MiB|KiB)$/i);
@@ -205,21 +205,32 @@ export function parseSize(size: string | undefined): number | undefined {
205205
);
206206
}
207207
const [, numStr, unit] = match;
208-
const num = parseInt(numStr, 10);
208+
const num = parseFloat(numStr);
209+
210+
let bytes = 0;
211+
209212
switch (unit.toLowerCase()) {
210213
case "gb":
211-
return num * GIGABYTE;
214+
bytes = num * GIGABYTE;
215+
break;
212216
case "mb":
213-
return num * MEGABYTE;
217+
bytes = num * MEGABYTE;
218+
break;
214219
case "kb":
215-
return num * KILOBYTE;
220+
bytes = num * KILOBYTE;
221+
break;
216222
case "gib":
217-
return num * GIBIBYTE;
223+
bytes = num * GIBIBYTE;
224+
break;
218225
case "mib":
219-
return num * MEBIBYTE;
226+
bytes = num * MEBIBYTE;
227+
break;
220228
case "kib":
221-
return num * KIBIBYTE;
229+
bytes = num * KIBIBYTE;
230+
break;
222231
}
232+
233+
return Math.floor(bytes / MEBIBYTE);
223234
}
224235

225236
export function tablePrinter<T>(

0 commit comments

Comments
 (0)