Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: function inspect all #68

Merged
merged 1 commit into from
Sep 12, 2024
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
90 changes: 68 additions & 22 deletions src/subcommands/function/inspect/action.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,79 @@
import { InspectOptions } from "./option.ts";
import { api, PROJECT_DB_PATH } from "@/api/mod.ts";
import {
api,
Deployment,
DeploymentState,
PROJECT_DB_PATH,
ProjectEnvironmentName,
} from "@/api/mod.ts";
import { buildEnvironmentName } from "@/subcommands/admin/var/utilities.ts";
import { Table } from "@cliffy/table";
import { colors } from "@cliffy/ansi";

export default async function (options: InspectOptions, functionName: 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.red,
"UNCAUGHT_EXCEPTION": colors.red,
"MEMORY_LIMIT_EXCEEDED": colors.red,
};

function coloredDeploymentState(state: DeploymentState): string {
return stateColors[state](state);
}

function buildDeploymentTable(
deployment: Deployment,
functionName: string,
environmentName: ProjectEnvironmentName,
) {
return Table.from([
["Environment", environmentName],
["Function Name", functionName],
["Deployment State", coloredDeploymentState(deployment.state)],
["Deployment Endpoint", deployment.endpoint ?? "N/A"],
]);
}

async function inspectAndBuildTable(
projectId: string,
environmentName: ProjectEnvironmentName,
functionName: string,
): Promise<string | undefined> {
const deployment = await api.jet.inspectFunction({
projectId,
environmentName,
functionName,
});

if (!deployment) {
api.console.log(`Function ${functionName} not found.`);
return undefined;
}

return buildDeploymentTable(deployment, functionName, environmentName)
.toString();
}

export default async function (options: InspectOptions, functionName?: string) {
const db = await api.db.connect(PROJECT_DB_PATH);
const environmentName = buildEnvironmentName(options);

try {
const [[projectId]] = db.query<[string]>("SELECT project_id FROM metadata");
const environmentName = buildEnvironmentName(options);

const deployment = await api.jet.inspectFunction({
projectId,
environmentName,
functionName,
});

if (deployment) {
const info: Table = new Table(
["Environment", environmentName],
["Function Name", functionName],
["Deployment State", deployment.state],
["Deployment Endpoint", deployment.endpoint ?? "N/A"],
);

api.console.log(info.toString());
} else {
api.console.log(`Function ${functionName} not found.`);
}
const functionNames = functionName ? [functionName] : db.query<[string]>(
`SELECT name FROM functions WHERE name GLOB '[^_]*' ORDER BY name`,
).flat();

const deploymentTables = await Promise.all(
functionNames.map((name) =>
inspectAndBuildTable(projectId, environmentName, name)
),
);
api.console.log(deploymentTables.filter(Boolean).join("\n\n"));
} finally {
db.close();
}
Expand Down
2 changes: 1 addition & 1 deletion src/subcommands/function/inspect/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import action from "./action.ts";

const command = new Command<InspectOptions>()
.description("Inspect given function")
.arguments("<functionName:string>")
.arguments("[functionName:string]")
.option("--prod", "Print production logs")
.error(errorHandler)
.action(action);
Expand Down
41 changes: 39 additions & 2 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,11 @@ 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/api");
await api.fs.mkdir("functions/_core");
await pushFunctionaction({ onlyFunctions: true });
});

afterEach(() => {
Expand All @@ -45,7 +52,9 @@ describe("inspect", () => {
assertEquals(api.console.logs.length, 1);
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 @@ -57,7 +66,35 @@ describe("inspect", () => {
assertEquals(api.console.logs.length, 2);
assertEquals(
api.console.logs[1],
"Environment PRODUCTION\nFunction Name main \nDeployment State BOOTING \nDeployment Endpoint N/A ",
`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", "api", {
state: "BOOTING",
});

await action({});

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

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