@nestbox-ai/cli
Version:
The cli tools that helps developers to build agents
145 lines • 8.05 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.registerDeleteCommand = registerDeleteCommand;
const chalk_1 = __importDefault(require("chalk"));
const ora_1 = __importDefault(require("ora"));
const project_1 = require("../../utils/project");
const apiUtils_1 = require("./apiUtils");
const inquirer_1 = __importDefault(require("inquirer"));
const axios_1 = __importDefault(require("axios"));
function registerDeleteCommand(computeCommand) {
computeCommand
.command('delete')
.description('Delete one or more compute instances')
.option('--project <projectId>', 'Project ID or name (defaults to the current project)')
.option('--force', 'Skip confirmation prompt')
.action((options) => __awaiter(this, void 0, void 0, function* () {
var _a, _b, _c;
try {
const apis = (0, apiUtils_1.createComputeApis)();
if (!apis) {
return;
}
const { machineInstanceApi, projectsApi, authToken } = apis;
// Resolve project using the shared utility
const project = yield (0, project_1.resolveProject)(projectsApi, options);
const spinner = (0, ora_1.default)(`Fetching compute instances for project: ${project.name}`).start();
try {
// Fetch machine instances for the project
const instancesResponse = yield machineInstanceApi.machineInstancesControllerGetMachineInstanceByUserId(project.id, 0, // page
50 // limit - increased to show more instances
);
spinner.succeed('Successfully retrieved compute instances');
const instances = ((_a = instancesResponse.data) === null || _a === void 0 ? void 0 : _a.machineInstances) || [];
if (instances.length === 0) {
console.log(chalk_1.default.yellow('No compute instances found for this project.'));
return;
}
// Create choices for the selection prompt
const instanceChoices = instances.map((instance) => ({
name: `${instance.instanceName || 'Unnamed'} (${instance.id})`,
value: instance.id,
short: instance.instanceName || instance.id
}));
// Prompt user to select instances to delete
const { selectedInstances } = yield inquirer_1.default.prompt([
{
type: 'checkbox',
name: 'selectedInstances',
message: 'Select compute instances to delete:',
choices: instanceChoices,
validate: (input) => {
if (input.length === 0) {
return 'Please select at least one instance to delete';
}
return true;
}
}
]);
if (selectedInstances.length === 0) {
console.log(chalk_1.default.yellow('No instances selected for deletion.'));
return;
}
// Show selected instances
console.log(chalk_1.default.yellow('\nSelected instances for deletion:'));
const selectedInstanceDetails = instances
.filter((instance) => selectedInstances.includes(instance.id))
.map((instance) => ` - ${chalk_1.default.cyan(instance.instanceName || 'Unnamed')} (${instance.id})`);
console.log(selectedInstanceDetails.join('\n'));
// Confirm deletion if not using --force
if (!options.force) {
const { confirmDeletion } = yield inquirer_1.default.prompt([
{
type: 'confirm',
name: 'confirmDeletion',
message: chalk_1.default.red('Are you sure you want to delete these instances? This cannot be undone.'),
default: false
}
]);
if (!confirmDeletion) {
console.log(chalk_1.default.yellow('Deletion cancelled.'));
return;
}
}
// Process deletion - using single request with all selected IDs
const deleteSpinner = (0, ora_1.default)(`Deleting ${selectedInstances.length} instance(s)...`).start();
try {
yield axios_1.default.delete(`${authToken.serverUrl}/projects/${project.id}/instances`, {
data: { ids: selectedInstances },
headers: {
Authorization: authToken.token,
}
});
deleteSpinner.succeed(`Successfully deleted ${selectedInstances.length} instance(s)`);
console.log(chalk_1.default.green('\nAll selected instances have been deleted'));
console.log(chalk_1.default.gray('You can verify with: nestbox compute list'));
}
catch (error) {
deleteSpinner.fail(`Failed to delete instances`);
if (error.response && error.response.status === 401) {
console.error(chalk_1.default.red('Authentication token has expired. Please login again using "nestbox login <domain>".'));
}
else if (error.response) {
console.error(chalk_1.default.red('API Error:'), ((_b = error.response.data) === null || _b === void 0 ? void 0 : _b.message) || 'Unknown error');
}
else {
console.error(chalk_1.default.red('Error:'), error.message || 'Unknown error');
}
}
}
catch (error) {
spinner.fail('Failed to retrieve compute instances');
if (error.response && error.response.status === 401) {
console.error(chalk_1.default.red('Authentication token has expired. Please login again using "nestbox login <domain>".'));
}
else if (error.response) {
console.error(chalk_1.default.red('API Error:'), ((_c = error.response.data) === null || _c === void 0 ? void 0 : _c.message) || 'Unknown error');
}
else {
console.error(chalk_1.default.red('Error:'), error.message || 'Unknown error');
}
}
}
catch (error) {
if (error.response && error.response.status === 401) {
console.error(chalk_1.default.red('Authentication token has expired. Please login again using "nestbox login <domain>".'));
}
else {
console.error(chalk_1.default.red('Error:'), error.message || 'Unknown error');
}
}
}));
}
//# sourceMappingURL=delete.js.map