diff --git a/src/subcommands/function/inspect/action.ts b/src/subcommands/function/inspect/action.ts index 0b9bfc4..f7e726b 100644 --- a/src/subcommands/function/inspect/action.ts +++ b/src/subcommands/function/inspect/action.ts @@ -2,6 +2,7 @@ import { InspectOptions } from "./option.ts"; import { api, Deployment, + DeploymentState, PROJECT_DB_PATH, ProjectEnvironmentName, } from "@/api/mod.ts"; @@ -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({ @@ -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 { @@ -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 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); +} diff --git a/test/subcommands/function/inspect/action_test.ts b/test/subcommands/function/inspect/action_test.ts index 61fb094..e45677b 100644 --- a/test/subcommands/function/inspect/action_test.ts +++ b/test/subcommands/function/inspect/action_test.ts @@ -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; @@ -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(() => { @@ -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", { @@ -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 `, ); }); });