Implemented count feature
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user