@pnp/cli-microsoft365
Version:
Manage Microsoft 365 and SharePoint Framework projects on any platform
97 lines • 4.09 kB
JavaScript
import { globalOptionsZod } from '../../../../Command.js';
import { z } from 'zod';
import { zod } from '../../../../utils/zod.js';
import commands from '../../commands.js';
import { validation } from '../../../../utils/validation.js';
import { spe } from '../../../../utils/spe.js';
import GraphCommand from '../../../base/GraphCommand.js';
import request from '../../../../request.js';
import { cli } from '../../../../cli/cli.js';
const options = globalOptionsZod
.extend({
id: zod.alias('i', z.string().optional()),
name: zod.alias('n', z.string().optional()),
containerTypeId: z.string()
.refine(id => validation.isValidGuid(id), id => ({
message: `'${id}' is not a valid GUID.`
})).optional(),
containerTypeName: z.string().optional(),
recycle: z.boolean().optional(),
force: zod.alias('f', z.boolean().optional())
})
.strict();
class SpeContainerRemoveCommand extends GraphCommand {
get name() {
return commands.CONTAINER_REMOVE;
}
get description() {
return 'Removes a container';
}
get schema() {
return options;
}
getRefinedSchema(schema) {
return schema
.refine((options) => [options.id, options.name].filter(o => o !== undefined).length === 1, {
message: 'Use one of the following options: id or name.'
})
.refine((options) => !options.name || [options.containerTypeId, options.containerTypeName].filter(o => o !== undefined).length === 1, {
message: 'Use one of the following options when specifying the container name: containerTypeId or containerTypeName.'
})
.refine((options) => options.name || [options.containerTypeId, options.containerTypeName].filter(o => o !== undefined).length === 0, {
message: 'Options containerTypeId and containerTypeName are only required when deleting a container by name.'
});
}
async commandAction(logger, args) {
if (!args.options.force) {
const result = await cli.promptForConfirmation({ message: `Are you sure you want to remove container '${args.options.id || args.options.name}'${!args.options.recycle ? ' permanently' : ''}?` });
if (!result) {
return;
}
}
try {
const containerId = await this.getContainerId(args.options, logger);
if (this.verbose) {
await logger.logToStderr(`Removing container with ID '${containerId}'...`);
}
const requestOptions = {
url: `${this.resource}/v1.0/storage/fileStorage/containers/${containerId}`,
headers: {
accept: 'application/json;odata.metadata=none'
},
responseType: 'json'
};
if (args.options.recycle) {
await request.delete(requestOptions);
return;
}
// Container should be permanently deleted
requestOptions.url += '/permanentDelete';
await request.post(requestOptions);
}
catch (err) {
this.handleRejectedODataJsonPromise(err);
}
}
async getContainerId(options, logger) {
if (options.id) {
return options.id;
}
const containerTypeId = await this.getContainerTypeId(options, logger);
if (this.verbose) {
await logger.logToStderr(`Getting container ID for container with name '${options.name}'...`);
}
return spe.getContainerIdByName(containerTypeId, options.name);
}
async getContainerTypeId(options, logger) {
if (options.containerTypeId) {
return options.containerTypeId;
}
if (this.verbose) {
await logger.logToStderr(`Getting container type with name '${options.containerTypeName}'...`);
}
return spe.getContainerTypeIdByName(options.containerTypeName);
}
}
export default new SpeContainerRemoveCommand();
//# sourceMappingURL=container-remove.js.map