unslopped file operations
This commit is contained in:
@@ -4,10 +4,10 @@ import { getApiClient } from "../../api.js";
|
|||||||
|
|
||||||
export const fileCreateDefinition = {
|
export const fileCreateDefinition = {
|
||||||
name: "create",
|
name: "create",
|
||||||
description: "Create a file in a storage.",
|
description: "Create or update a file in a function.",
|
||||||
options: [
|
options: [
|
||||||
{ name: "--storage-id <id>", description: "Storage ID", required: true },
|
{ name: "--function-id <id>", description: "Function ID", required: true },
|
||||||
{ name: "--path <path>", description: "Remote file path", required: true },
|
{ name: "--filename <name>", description: "Remote filename", required: true },
|
||||||
{ name: "--content <content>", description: "Inline file content" },
|
{ name: "--content <content>", description: "Inline file content" },
|
||||||
{ name: "--source <source>", description: "Local source file path" },
|
{ name: "--source <source>", description: "Local source file path" },
|
||||||
],
|
],
|
||||||
@@ -34,16 +34,16 @@ export const fileCreateDefinition = {
|
|||||||
|
|
||||||
const client = await getApiClient();
|
const client = await getApiClient();
|
||||||
const payload = {
|
const payload = {
|
||||||
path: options.path,
|
filename: options.filename,
|
||||||
content,
|
code: content,
|
||||||
};
|
};
|
||||||
|
|
||||||
try {
|
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) {
|
if (response.status === 200 || response.status === 201) {
|
||||||
console.log(
|
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 {
|
} else {
|
||||||
console.log(`${chalk.yellow("!")} Unexpected response from server: ${response.status}`);
|
console.log(`${chalk.yellow("!")} Unexpected response from server: ${response.status}`);
|
||||||
@@ -51,7 +51,7 @@ export const fileCreateDefinition = {
|
|||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
if (error.response) {
|
if (error.response) {
|
||||||
console.error(
|
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) {
|
} else if (error.request) {
|
||||||
console.error(`${chalk.red("✗")} No response received from server.`);
|
console.error(`${chalk.red("✗")} No response received from server.`);
|
||||||
|
|||||||
@@ -3,22 +3,20 @@ import { getApiClient } from "../../api.js";
|
|||||||
|
|
||||||
export const fileDeleteDefinition = {
|
export const fileDeleteDefinition = {
|
||||||
name: "delete",
|
name: "delete",
|
||||||
description: "Delete a file from a storage.",
|
description: "Delete a file from a function.",
|
||||||
options: [
|
options: [
|
||||||
{ name: "--storage-id <id>", description: "Storage ID", required: true },
|
{ name: "--function-id <id>", description: "Function ID", required: true },
|
||||||
{ name: "--path <path>", description: "Remote file path", required: true },
|
{ name: "--file-id <id>", description: "File ID (from 'file list')", required: true },
|
||||||
],
|
],
|
||||||
action: async (options: any) => {
|
action: async (options: any) => {
|
||||||
const client = await getApiClient();
|
const client = await getApiClient();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await client.delete(`/api/storage/${options.storageId}/files`, {
|
const response = await client.delete(`/api/function/${options.functionId}/file/${options.fileId}`);
|
||||||
data: { path: options.path },
|
|
||||||
});
|
|
||||||
|
|
||||||
if (response.status === 200) {
|
if (response.status === 200) {
|
||||||
console.log(
|
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 {
|
} else {
|
||||||
console.log(`${chalk.yellow("!")} Unexpected response from server: ${response.status}`);
|
console.log(`${chalk.yellow("!")} Unexpected response from server: ${response.status}`);
|
||||||
@@ -26,7 +24,7 @@ export const fileDeleteDefinition = {
|
|||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
if (error.response) {
|
if (error.response) {
|
||||||
if (error.response.status === 404) {
|
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 {
|
} else {
|
||||||
console.error(
|
console.error(
|
||||||
`${chalk.red("✗")} Failed to delete file: ${chalk.yellow(error.response.data?.message || "Unknown error")}`,
|
`${chalk.red("✗")} Failed to delete file: ${chalk.yellow(error.response.data?.message || "Unknown error")}`,
|
||||||
|
|||||||
@@ -3,18 +3,15 @@ import { getApiClient } from "../../api.js";
|
|||||||
|
|
||||||
export const fileListDefinition = {
|
export const fileListDefinition = {
|
||||||
name: "list",
|
name: "list",
|
||||||
description: "List files in a storage.",
|
description: "List files in a function.",
|
||||||
options: [
|
options: [
|
||||||
{ name: "--storage-id <id>", description: "Storage ID", required: true },
|
{ name: "--function-id <id>", description: "Function ID", required: true },
|
||||||
{ name: "--prefix <prefix>", description: "Optional path prefix filter" },
|
|
||||||
],
|
],
|
||||||
action: async (options: any) => {
|
action: async (options: any) => {
|
||||||
const client = await getApiClient();
|
const client = await getApiClient();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await client.get(`/api/storage/${options.storageId}/files`, {
|
const response = await client.get(`/api/function/${options.functionId}/files`);
|
||||||
params: options.prefix ? { prefix: options.prefix } : undefined,
|
|
||||||
});
|
|
||||||
|
|
||||||
if (response.status === 200 && response.data.data) {
|
if (response.status === 200 && response.data.data) {
|
||||||
const files = response.data.data;
|
const files = response.data.data;
|
||||||
@@ -25,9 +22,9 @@ export const fileListDefinition = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
console.log(chalk.blue("Files:"));
|
console.log(chalk.blue("Files:"));
|
||||||
|
console.log(chalk.gray(`${"ID".padEnd(25)} ${"Filename"}`));
|
||||||
files.forEach((file: any) => {
|
files.forEach((file: any) => {
|
||||||
const path = file.path || file.name || "(unknown)";
|
console.log(`${chalk.cyan(file.id.padEnd(25))} ${chalk.white(file.filename)}`);
|
||||||
console.log(`- ${chalk.cyan(path)}`);
|
|
||||||
});
|
});
|
||||||
console.log(`\n${chalk.green("✓")} Found ${chalk.bgGreen.black(` ${files.length} `)} files.`);
|
console.log(`\n${chalk.green("✓")} Found ${chalk.bgGreen.black(` ${files.length} `)} files.`);
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -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 <id>", description: "Storage ID", required: true },
|
|
||||||
{ name: "--path <path>", description: "Remote file path", required: true },
|
|
||||||
{ name: "--content <content>", description: "Inline file content" },
|
|
||||||
{ name: "--source <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}`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
};
|
|
||||||
@@ -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 <id>", description: "Function ID", required: true },
|
||||||
|
{ name: "--file-id <id>", description: "File ID", required: true },
|
||||||
|
{ name: "--new-filename <name>", 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}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user