Skip to content

Commit

Permalink
fixup! feat: function inspect all
Browse files Browse the repository at this point in the history
  • Loading branch information
danewalters committed Sep 11, 2024
1 parent 1164e20 commit be3e333
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 11 deletions.
30 changes: 24 additions & 6 deletions src/subcommands/function/inspect/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { InspectOptions } from "./option.ts";
import {
api,
Deployment,
DeploymentState,
PROJECT_DB_PATH,
ProjectEnvironmentName,
} from "@/api/mod.ts";
Expand All @@ -17,7 +18,9 @@ export default async function (options: InspectOptions, functionName?: string) {
const [[projectId]] = db.query<[string]>("SELECT project_id FROM metadata");
const functionNames = functionName
? [functionName]
: db.query<[string]>("SELECT name FROM functions").flat();
: db.query<[string]>("SELECT name FROM functions").flat().filter((str) =>
!str.startsWith("_")
).sort();

const deploymentPromises = functionNames.map((name) =>
api.jet.inspectFunction({
Expand All @@ -32,7 +35,7 @@ export default async function (options: InspectOptions, functionName?: string) {
const deploymentTables = await Promise.all(deploymentPromises);

for (const table of deploymentTables) {
table.render();
api.console.log(table.toString());
api.console.log("\n");
}
} finally {
Expand All @@ -46,9 +49,24 @@ function buildDeploymentTable(
environmentName: ProjectEnvironmentName,
) {
return Table.from([
[colors.green("Environment"), environmentName],
[colors.green("Function Name"), functionName],
[colors.green("Deployment State"), deployment.state],
[colors.green("Deployment Endpoint"), deployment.endpoint ?? "N/A"],
["Environment", environmentName],
["Function Name", functionName],
["Deployment State", coloredDeploymentState(deployment.state)],
["Deployment Endpoint", deployment.endpoint ?? "N/A"],
]);
}

function coloredDeploymentState(state: DeploymentState): string {
const stateColors: Record<DeploymentState, (text: string) => string> = {
"UNSPECIFIED": colors.gray,
"BOOTING": colors.cyan,
"RUNNING": colors.green,
"BOOT_FAILED": colors.red,
"DESTROYED": colors.magenta,
"EARLY_EXITED": colors.yellow,
"UNCAUGHT_EXCEPTION": colors.red,
"MEMORY_LIMIT_EXCEEDED": colors.red,
};

return stateColors[state](state);
}
50 changes: 45 additions & 5 deletions test/subcommands/function/inspect/action_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import { APIClientTest, makeAPIClient } from "@test/api/mod.ts";

import createProject from "@/subcommands/admin/projects/create/action.ts";
import action from "@/subcommands/function/inspect/action.ts";
import pushFunctionaction from "@/subcommands/push/action.ts";
import { colors } from "@cliffy/ansi";

describe("inspect", () => {
let api: APIClientTest;
Expand All @@ -26,6 +28,10 @@ describe("inspect", () => {
projectId = api.jet.getProject({ projectName: "my_proj" })!.id;

api.chdir("my_proj");

await api.fs.mkdir("functions/main");
await api.fs.mkdir("functions/my_func");
await pushFunctionaction({ onlyFunctions: true });
});

afterEach(() => {
Expand All @@ -42,10 +48,12 @@ describe("inspect", () => {

await action({}, "main");

assertEquals(api.console.logs.length, 1);
assertEquals(api.console.logs.length, 2);
assertEquals(
api.console.logs[0],
"Environment DEVELOPMENT \nFunction Name main \nDeployment State RUNNING \nDeployment Endpoint https://breeze.rt/main",
`Environment DEVELOPMENT \nFunction Name main \nDeployment State ${
colors.green("RUNNING")
} \nDeployment Endpoint https://breeze.rt/main`,
);

api.jet.setDeployment(projectId, "PRODUCTION", "main", {
Expand All @@ -54,10 +62,42 @@ describe("inspect", () => {

await action({ prod: true }, "main");

assertEquals(api.console.logs.length, 2);
assertEquals(api.console.logs.length, 4);
assertEquals(
api.console.logs[2],
`Environment PRODUCTION\nFunction Name main \nDeployment State ${
colors.cyan("BOOTING")
} \nDeployment Endpoint N/A `,
);
});

it("print function inspection all", async () => {
api.console.configure({ capture: true });

api.jet.setDeployment(projectId, "DEVELOPMENT", "main", {
state: "RUNNING",
endpoint: "https://breeze.rt/main",
});

api.jet.setDeployment(projectId, "DEVELOPMENT", "my_func", {
state: "BOOTING",
});

await action({});

assertEquals(api.console.logs.length, 4);

assertEquals(
api.console.logs[0],
`Environment DEVELOPMENT \nFunction Name main \nDeployment State ${
colors.green("RUNNING")
} \nDeployment Endpoint https://breeze.rt/main`,
);
assertEquals(
api.console.logs[1],
"Environment PRODUCTION\nFunction Name main \nDeployment State BOOTING \nDeployment Endpoint N/A ",
api.console.logs[2],
`Environment DEVELOPMENT\nFunction Name my_func \nDeployment State ${
colors.cyan("BOOTING")
} \nDeployment Endpoint N/A `,
);
});
});

0 comments on commit be3e333

Please sign in to comment.