feat: refactor file operations and add utility functions for file management
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import chalk from "chalk";
|
||||
import { getApiClient } from "../../api.js";
|
||||
import { listFiles, handleAxiosError } from "../../utils/fileops.js";
|
||||
|
||||
export const fileListDefinition = {
|
||||
name: "list",
|
||||
@@ -11,10 +12,9 @@ export const fileListDefinition = {
|
||||
const client = await getApiClient();
|
||||
|
||||
try {
|
||||
const response = await client.get(`/api/function/${options.functionId}/files`);
|
||||
const files = await listFiles(client, options.functionId);
|
||||
|
||||
if (response.status === 200 && response.data.data) {
|
||||
const files = response.data.data;
|
||||
if (files) {
|
||||
|
||||
if (!Array.isArray(files) || files.length === 0) {
|
||||
console.log(`${chalk.yellow("!")} No files found.`);
|
||||
@@ -24,23 +24,15 @@ export const fileListDefinition = {
|
||||
console.log(chalk.blue("Files:"));
|
||||
console.log(chalk.gray(`${"ID".padEnd(25)} ${"Filename"}`));
|
||||
files.forEach((file: any) => {
|
||||
console.log(`${chalk.cyan(file.id.padEnd(25))} ${chalk.white(file.filename)}`);
|
||||
const id = String(file.id ?? file.name ?? "");
|
||||
console.log(`${chalk.cyan(id.padEnd(25))} ${chalk.white(file.filename || file.name)}`);
|
||||
});
|
||||
console.log(`\n${chalk.green("✓")} Found ${chalk.bgGreen.black(` ${files.length} `)} files.`);
|
||||
} 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 list files: ${chalk.yellow(error.response.data?.message || "Unknown error")}`,
|
||||
);
|
||||
} else if (error.request) {
|
||||
console.error(`${chalk.red("✗")} No response received from server.`);
|
||||
} else {
|
||||
console.error(`${chalk.red("✗")} Error: ${error.message}`);
|
||||
}
|
||||
handleAxiosError(error);
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user