Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 1 addition & 23 deletions sandbox/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { join } from "@std/path";
import { Spinner } from "@std/cli/unstable-spinner";

import { getAppFromConfig, readConfig, writeConfig } from "../config.ts";
import { error, renderTemporalTimestamp, withApp } from "../util.ts";
import { error, parseSize, renderTemporalTimestamp, withApp } from "../util.ts";
import { createTrpcClient, getAuth } from "../auth.ts";
import type { GlobalOptions } from "../main.ts";
import token_storage from "../token_storage.ts";
Expand Down Expand Up @@ -609,28 +609,6 @@ export function formatDuration(ms: number): string {
return str;
}

function parseSize(size: string | undefined): number | undefined {
if (size === undefined) return undefined;

const regex = /^(\d+)(gb|mb)$/i;
const res = regex.exec(size);

if (res === null) {
// error
error(
false,
"Invalid size format. Examples of valid size: '2gb', '1024mb'",
);
}

switch (res[2].toLowerCase()) {
case "gb":
return parseInt(res[1]) * 1024;
case "mb":
return parseInt(res[1]);
}
}

export const sandboxCommand = new Command<GlobalOptions>()
.name("deno sandbox")
.description("Interact with sandboxes")
Expand Down
13 changes: 5 additions & 8 deletions sandbox/volumes.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Command } from "@cliffy/command";
import { ensureOrg, type SandboxContext } from "./mod.ts";
import { getAuth } from "../auth.ts";
import { Client, type VolumeInit } from "@deno/sandbox";
import { tablePrinter } from "../util.ts";
import { Client } from "@deno/sandbox";
import { formatSize, parseSize, tablePrinter } from "../util.ts";

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

const volume = await client.volumes.create({
slug: name,
capacity: options.capacity as VolumeInit["capacity"],
capacity: parseSize(options.capacity)!,
region: options.region,
});

Expand Down Expand Up @@ -52,15 +52,12 @@ export const volumesListCommand = new Command<SandboxContext>()
["ID", "SLUG", "REGION", "USED", "TOTAL"],
list.items,
(volume) => {
const used = volume.used.toFixed(2).toString();
const total = volume.capacity.toFixed(2).toString();

return [
volume.id,
volume.slug,
volume.region,
used,
total,
formatSize(volume.used),
formatSize(volume.capacity),
];
},
);
Expand Down
54 changes: 54 additions & 0 deletions util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,60 @@ export function renderTemporalTimestamp(timestamp: string, hideDate = false) {
return `${date.year}-${months}-${days} ${time}`;
}

export const KIBIBYTE = 1024;
export const MEBIBYTE = KIBIBYTE * 1024;
export const GIBIBYTE = MEBIBYTE * 1024;

export const KILOBYTE = 1000;
export const MEGABYTE = KILOBYTE * 1000;
export const GIGABYTE = MEGABYTE * 1000;

export function formatSize(bytes: number): string {
if (bytes === 0) return "0 Bytes";

if (bytes >= GIBIBYTE) {
return `${parseFloat((bytes / GIBIBYTE).toFixed(2))} GiB`;
}

if (bytes >= MEBIBYTE) {
return `${parseFloat((bytes / MEBIBYTE).toFixed(2))} MiB`;
}

if (bytes >= KIBIBYTE) {
return `${parseFloat((bytes / KIBIBYTE).toFixed(2))} KiB`;
}

return `${bytes} Bytes`;
}

export function parseSize(size: string | undefined): number | undefined {
if (size === undefined) return undefined;

const match = size.match(/^(\d+)(GB|MB|KB|GiB|MiB|KiB)$/i);
if (!match) {
error(
false,
"Invalid size format. Examples of valid size: '2gb', '1024mb'",
);
}
const [, numStr, unit] = match;
const num = parseInt(numStr, 10);
switch (unit.toLowerCase()) {
case "gb":
return num * GIGABYTE;
case "mb":
return num * MEGABYTE;
case "kb":
return num * KILOBYTE;
case "gib":
return num * GIBIBYTE;
case "mib":
return num * MEBIBYTE;
case "kib":
return num * KIBIBYTE;
}
}

export function tablePrinter<T>(
headers: string[],
values: T[],
Expand Down