diff --git a/src/commands/file/create.ts b/src/commands/file/create.ts index 86b22ef..de32c99 100644 --- a/src/commands/file/create.ts +++ b/src/commands/file/create.ts @@ -4,10 +4,10 @@ import { getApiClient } from "../../api.js"; export const fileCreateDefinition = { name: "create", - description: "Create a file in a storage.", + description: "Create or update a file in a function.", options: [ - { name: "--storage-id ", description: "Storage ID", required: true }, - { name: "--path ", description: "Remote file path", required: true }, + { name: "--function-id ", description: "Function ID", required: true }, + { name: "--filename ", description: "Remote filename", required: true }, { name: "--content ", description: "Inline file content" }, { name: "--source ", description: "Local source file path" }, ], @@ -34,16 +34,16 @@ export const fileCreateDefinition = { const client = await getApiClient(); const payload = { - path: options.path, - content, + filename: options.filename, + code: content, }; try { - const response = await client.post(`/api/storage/${options.storageId}/files`, payload); + const response = await client.put(`/api/function/${options.functionId}/file`, payload); if (response.status === 200 || response.status === 201) { console.log( - `${chalk.green("✓")} File ${chalk.cyan(options.path)} created successfully in storage ${chalk.cyan(options.storageId)}.`, + `${chalk.green("✓")} File ${chalk.cyan(options.filename)} created/updated successfully in function ${chalk.cyan(options.functionId)}.`, ); } else { console.log(`${chalk.yellow("!")} Unexpected response from server: ${response.status}`); @@ -51,7 +51,7 @@ export const fileCreateDefinition = { } catch (error: any) { if (error.response) { console.error( - `${chalk.red("✗")} Failed to create file: ${chalk.yellow(error.response.data?.message || "Unknown error")}`, + `${chalk.red("✗")} Failed to create/update file: ${chalk.yellow(error.response.data?.message || "Unknown error")}`, ); } else if (error.request) { console.error(`${chalk.red("✗")} No response received from server.`); diff --git a/src/commands/file/delete.ts b/src/commands/file/delete.ts index cbb143c..b302551 100644 --- a/src/commands/file/delete.ts +++ b/src/commands/file/delete.ts @@ -3,22 +3,20 @@ import { getApiClient } from "../../api.js"; export const fileDeleteDefinition = { name: "delete", - description: "Delete a file from a storage.", + description: "Delete a file from a function.", options: [ - { name: "--storage-id ", description: "Storage ID", required: true }, - { name: "--path ", description: "Remote file path", required: true }, + { name: "--function-id ", description: "Function ID", required: true }, + { name: "--file-id ", description: "File ID (from 'file list')", required: true }, ], action: async (options: any) => { const client = await getApiClient(); try { - const response = await client.delete(`/api/storage/${options.storageId}/files`, { - data: { path: options.path }, - }); + const response = await client.delete(`/api/function/${options.functionId}/file/${options.fileId}`); if (response.status === 200) { console.log( - `${chalk.green("✓")} File ${chalk.cyan(options.path)} deleted from storage ${chalk.cyan(options.storageId)}.`, + `${chalk.green("✓")} File ${chalk.cyan(options.fileId)} deleted from function ${chalk.cyan(options.functionId)}.`, ); } else { console.log(`${chalk.yellow("!")} Unexpected response from server: ${response.status}`); @@ -26,7 +24,7 @@ export const fileDeleteDefinition = { } catch (error: any) { if (error.response) { if (error.response.status === 404) { - console.error(`${chalk.red("✗")} File ${chalk.yellow(options.path)} not found.`); + console.error(`${chalk.red("✗")} File or Function not found.`); } else { console.error( `${chalk.red("✗")} Failed to delete file: ${chalk.yellow(error.response.data?.message || "Unknown error")}`, diff --git a/src/commands/file/list.ts b/src/commands/file/list.ts index 3172c6b..2a9bb77 100644 --- a/src/commands/file/list.ts +++ b/src/commands/file/list.ts @@ -3,18 +3,15 @@ import { getApiClient } from "../../api.js"; export const fileListDefinition = { name: "list", - description: "List files in a storage.", + description: "List files in a function.", options: [ - { name: "--storage-id ", description: "Storage ID", required: true }, - { name: "--prefix ", description: "Optional path prefix filter" }, + { name: "--function-id ", description: "Function ID", required: true }, ], action: async (options: any) => { const client = await getApiClient(); try { - const response = await client.get(`/api/storage/${options.storageId}/files`, { - params: options.prefix ? { prefix: options.prefix } : undefined, - }); + const response = await client.get(`/api/function/${options.functionId}/files`); if (response.status === 200 && response.data.data) { const files = response.data.data; @@ -25,9 +22,9 @@ export const fileListDefinition = { } console.log(chalk.blue("Files:")); + console.log(chalk.gray(`${"ID".padEnd(25)} ${"Filename"}`)); files.forEach((file: any) => { - const path = file.path || file.name || "(unknown)"; - console.log(`- ${chalk.cyan(path)}`); + console.log(`${chalk.cyan(file.id.padEnd(25))} ${chalk.white(file.filename)}`); }); console.log(`\n${chalk.green("✓")} Found ${chalk.bgGreen.black(` ${files.length} `)} files.`); } else { diff --git a/src/commands/file/overwrite.ts b/src/commands/file/overwrite.ts deleted file mode 100644 index cb1513a..0000000 --- a/src/commands/file/overwrite.ts +++ /dev/null @@ -1,67 +0,0 @@ -import chalk from "chalk"; -import fs from "fs/promises"; -import { getApiClient } from "../../api.js"; - -export const fileOverwriteDefinition = { - name: "overwrite", - description: "Overwrite an existing file in a storage.", - options: [ - { name: "--storage-id ", description: "Storage ID", required: true }, - { name: "--path ", description: "Remote file path", required: true }, - { name: "--content ", description: "Inline file content" }, - { name: "--source ", description: "Local source file path" }, - ], - action: async (options: any) => { - if (!options.content && !options.source) { - console.error(`${chalk.red("✗")} You must provide either --content or --source.`); - return; - } - - if (options.content && options.source) { - console.error(`${chalk.red("✗")} Please provide only one of --content or --source.`); - return; - } - - let content = options.content; - if (options.source) { - try { - content = await fs.readFile(options.source, "utf8"); - } catch (error: any) { - console.error(`${chalk.red("✗")} Failed to read source file: ${chalk.yellow(error.message)}`); - return; - } - } - - const client = await getApiClient(); - const payload = { - path: options.path, - content, - }; - - try { - const response = await client.put(`/api/storage/${options.storageId}/files`, payload); - - if (response.status === 200) { - console.log( - `${chalk.green("✓")} File ${chalk.cyan(options.path)} overwritten successfully in storage ${chalk.cyan(options.storageId)}.`, - ); - } else { - console.log(`${chalk.yellow("!")} Unexpected response from server: ${response.status}`); - } - } catch (error: any) { - if (error.response) { - if (error.response.status === 404) { - console.error(`${chalk.red("✗")} File ${chalk.yellow(options.path)} not found.`); - } else { - console.error( - `${chalk.red("✗")} Failed to overwrite file: ${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}`); - } - } - }, -}; diff --git a/src/commands/file/rename.ts b/src/commands/file/rename.ts new file mode 100644 index 0000000..1bec309 --- /dev/null +++ b/src/commands/file/rename.ts @@ -0,0 +1,39 @@ +import chalk from "chalk"; +import { getApiClient } from "../../api.js"; + +export const fileRenameDefinition = { + name: "rename", + description: "Rename a file in a function.", + options: [ + { name: "--function-id ", description: "Function ID", required: true }, + { name: "--file-id ", description: "File ID", required: true }, + { name: "--new-filename ", description: "New filename", required: true }, + ], + action: async (options: any) => { + const client = await getApiClient(); + + try { + const response = await client.patch(`/api/function/${options.functionId}/file/${options.fileId}/rename`, { + newFilename: options.newFilename, + }); + + if (response.status === 200) { + console.log( + `${chalk.green("✓")} File ${chalk.cyan(options.fileId)} renamed to ${chalk.cyan(options.newFilename)} successfully.`, + ); + } else { + console.log(`${chalk.yellow("!")} Unexpected response from server: ${response.status}`); + } + } catch (error: any) { + if (error.response) { + console.error( + `${chalk.red("✗")} Failed to rename file: ${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}`); + } + } + }, +};