Implemented count feature

This commit is contained in:
Space-Banane
2026-03-18 16:33:03 +01:00
parent fdcf3d06e0
commit 9aec36d0e8
8 changed files with 305 additions and 6 deletions
+59
View File
@@ -0,0 +1,59 @@
import chalk from "chalk";
import { getApiClient } from "../../api.js";
export const functionsCountDefinition = {
name: "functions",
description: "Counts the amount of functions the current user has.",
action: async () => {
await countFunctions();
},
};
async function countFunctions() {
const client = await getApiClient();
try {
const response = await client.get("/api/functions");
if (response.status === 200 && response.data.data) {
const functions = response.data.data;
const count = functions.length;
if (count > 0) {
console.log(chalk.blue("Functions List:"));
functions.forEach((f: any) => {
const namespaceInfo = f.namespace ? chalk.gray(` [NS: ${f.namespace.id}]`) : "";
console.log(`- ${chalk.cyan(f.name)} ${chalk.gray(`(${f.id})`)}${namespaceInfo}`);
if (f.description) {
console.log(` ${chalk.italic.gray(f.description)}`);
}
});
console.log("");
}
console.log(
`${chalk.green("✓")} You have ${chalk.bgGreen.black(` ${count} `)} functions.`,
);
} else {
console.log(
`${chalk.yellow("!")} Unexpected response from server.`,
);
console.log(`Status Code: ${chalk.blue(response.status)}`);
}
} catch (error: any) {
if (error.response) {
console.error(
`${chalk.red("✗")} Failed to fetch function count.`,
);
console.error(`Status Code: ${chalk.red(error.response.status)}`);
console.error(
`Message: ${chalk.yellow(error.response.data.message || "Unknown error from server")}`,
);
} else {
console.error(
`${chalk.red("✗")} Local error occurred.`,
);
console.error(`Error: ${chalk.yellow(error.message)}`);
}
}
}
+54
View File
@@ -0,0 +1,54 @@
import chalk from "chalk";
import { getApiClient } from "../../api.js";
export const namespacesCountDefinition = {
name: "namespaces",
description: "Counts the amount of namespaces the current user has.",
action: async () => {
await countNamespaces();
},
};
async function countNamespaces() {
const client = await getApiClient();
try {
const response = await client.get("/api/namespaces");
if (response.status === 200 && response.data.data) {
const namespaces = response.data.data;
const count = namespaces.length;
if (count > 0) {
console.log(chalk.blue("Namespaces List:"));
namespaces.forEach((ns: any) => {
console.log(`- ${chalk.cyan(ns.name)} ${chalk.gray(`(${ns.id})`)}`);
});
console.log("");
}
console.log(
`${chalk.green("✓")} You have ${chalk.bgGreen.black(` ${count} `)} namespaces.`,
);
} else {
console.log(
`${chalk.yellow("!")} Unexpected response from server.`,
);
console.log(`Status Code: ${chalk.blue(response.status)}`);
}
} catch (error: any) {
if (error.response) {
console.error(
`${chalk.red("✗")} Failed to fetch namespace count.`,
);
console.error(`Status Code: ${chalk.red(error.response.status)}`);
console.error(
`Message: ${chalk.yellow(error.response.data.message || "Unknown error from server")}`,
);
} else if (error.request) {
console.error(`${chalk.red("✗")} No response received from server.`);
} else {
console.error(`${chalk.red("✗")} Error:`, error.message);
}
}
}
+55
View File
@@ -0,0 +1,55 @@
import chalk from "chalk";
import { getApiClient } from "../../api.js";
export const storagesCountDefinition = {
name: "storages",
description: "Counts the amount of storages the current user has.",
action: async () => {
await countStorages();
},
};
async function countStorages() {
const client = await getApiClient();
try {
const response = await client.get("/api/storage");
if (response.status === 200 && response.data.data) {
const storages = response.data.data;
const count = storages.length;
if (count > 0) {
console.log(chalk.blue("Storages List:"));
storages.forEach((s: any) => {
const purposeInfo = s.purpose ? chalk.gray(` - ${s.purpose}`) : "";
console.log(`- ${chalk.cyan(s.name)}${purposeInfo}`);
});
console.log("");
}
console.log(
`${chalk.green("✓")} You have ${chalk.bgGreen.black(` ${count} `)} storages.`,
);
} else {
console.log(
`${chalk.yellow("!")} Unexpected response from server.`,
);
console.log(`Status Code: ${chalk.blue(response.status)}`);
}
} catch (error: any) {
if (error.response) {
console.error(
`${chalk.red("✗")} Failed to fetch storage count.`,
);
console.error(`Status Code: ${chalk.red(error.response.status)}`);
console.error(
`Message: ${chalk.yellow(error.response.data.message || "Unknown error from server")}`,
);
} else if (error.request) {
console.error(`${chalk.red("✗")} No response received from server.`);
} else {
console.error(`${chalk.red("✗")} Error:`, error.message);
}
}
}
+59
View File
@@ -0,0 +1,59 @@
import chalk from "chalk";
import { getApiClient } from "../../api.js";
export const triggersCountDefinition = {
name: "triggers",
description: "Counts the amount of triggers the current user has.",
action: async () => {
await countTriggers();
},
};
async function countTriggers() {
const client = await getApiClient();
try {
const response = await client.get("/api/triggers");
if (response.status === 200 && response.data.data) {
const triggers = response.data.data;
const count = triggers.length;
if (count > 0) {
console.log(chalk.blue("Triggers List:"));
triggers.forEach((t: any) => {
const functionInfo = t.function ? chalk.gray(` [Func: ${t.function.name}]`) : "";
const cronInfo = t.cron ? chalk.yellow(` (Cron: ${t.cron})`) : "";
console.log(`- ${chalk.cyan(t.name)} ${chalk.gray(`(${t.id})`)}${functionInfo}${cronInfo}`);
if (t.description) {
console.log(` ${chalk.italic.gray(t.description)}`);
}
});
console.log("");
}
console.log(
`${chalk.green("✓")} You have ${chalk.bgGreen.black(` ${count} `)} triggers.`,
);
} else {
console.log(
`${chalk.yellow("!")} Unexpected response from server.`,
);
console.log(`Status Code: ${chalk.blue(response.status)}`);
}
} catch (error: any) {
if (error.response) {
console.error(
`${chalk.red("✗")} Failed to fetch trigger count.`,
);
console.error(`Status Code: ${chalk.red(error.response.status)}`);
console.error(
`Message: ${chalk.yellow(error.response.data.message || "Unknown error from server")}`,
);
} else if (error.request) {
console.error(`${chalk.red("✗")} No response received from server.`);
} else {
console.error(`${chalk.red("✗")} Error:`, error.message);
}
}
}