From 2db10eacb4812ff32f4f2cdb7bea2d576198fd50 Mon Sep 17 00:00:00 2001 From: Marc Pomar Date: Mon, 2 Nov 2026 14:58:13 +0100 Subject: [PATCH] =?UTF-8?q?feat(deploy):=20faable=20deploy=20volumes=20rm?= =?UTF-8?q?=20=E2=80=94=20to=20the=20volume's=20trash,=20--undo=20for=207?= =?UTF-8?q?=20days=20(backlog=20=C2=A7460)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit rm moves files or directories of a volume to its trash with the app serving and says how to undo it; rm --undo brings them back (never over a path that exists again); after 7 days the platform empties the trash. Asks before going (--yes in non-interactive mode). Over the api's POST /volume/:id/remove and POST /volume-operation/:id/undo. --- src/api/FaableApi.ts | 36 +++++++- src/commands/deploy/volumes/index.ts | 6 +- src/commands/deploy/volumes/rm.ts | 129 +++++++++++++++++++++++++++ 3 files changed, 168 insertions(+), 3 deletions(-) create mode 100644 src/commands/deploy/volumes/rm.ts diff --git a/src/api/FaableApi.ts b/src/api/FaableApi.ts index fe52bcc..d308a38 100644 --- a/src/api/FaableApi.ts +++ b/src/api/FaableApi.ts @@ -344,7 +344,16 @@ export type FaableVolumeOperation = { id: string volume_id: string app_id: string - kind: 'backup' | 'restore' | 'export' | 'import' | 'put' | 'list' + kind: + | 'backup' + | 'restore' + | 'export' + | 'import' + | 'put' + | 'list' + | 'remove' + | 'untrash' + | 'purge' reason: 'manual' | 'scheduled' | 'pre_restore' | 'pre_import' state: 'awaiting_upload' | 'queued' | 'running' | 'completed' | 'failed' backup_id: string | null @@ -358,6 +367,9 @@ export type FaableVolumeOperation = { path?: string | null // A completed list: the directory's entries (at most 1000). entries?: FaableVolumeEntry[] | null + // A remove or an untrash: the paths; an untrash or a purge: its remove. + paths?: string[] | null + source_id?: string | null download_url?: string | null download_expires_at?: string | null upload_url?: string | null @@ -1015,6 +1027,28 @@ export class FaableApi { ) } + // rm: the paths go to the volume's trash (7 days), never deleted at once. + async removeVolumePaths(team: string, volume_id: string, paths: string[]) { + return data( + this.client.post( + `/volume/${volume_id}/remove`, + { paths }, + { headers: projectHeader(team) } + ) + ) + } + + // rm --undo: what a remove sent to the trash comes back. + async undoVolumeOperation(team: string, operation_id: string) { + return data( + this.client.post( + `/volume-operation/${operation_id}/undo`, + undefined, + { headers: projectHeader(team) } + ) + ) + } + async restoreVolume(team: string, volume_id: string, backup_id: string) { return data( this.client.post( diff --git a/src/commands/deploy/volumes/index.ts b/src/commands/deploy/volumes/index.ts index 772b068..da3fd22 100644 --- a/src/commands/deploy/volumes/index.ts +++ b/src/commands/deploy/volumes/index.ts @@ -5,6 +5,7 @@ import { volumes_import, volumes_put } from './import' import { volumes_list } from './list' import { volumes_ls } from './ls' import { volumes_restore } from './restore' +import { volumes_rm } from './rm' // The volumes of an app (api `/volume`, `/volume-operation`): today the ones // a blueprint defines (WordPress: `site` at /faable/app). Hidden until the @@ -12,13 +13,14 @@ import { volumes_restore } from './restore' // describe below. Arch: deploy/volumenes-cli-comparativa.md (backlog §460). export const volumes: CommandModule = { command: 'volumes ', - // 'Manage the volumes of an app: usage, files (ls, get, put), backups, restore, export and import' + // 'Manage the volumes of an app: usage, files (ls, get, put, rm), backups, restore, export and import' describe: false, builder: yargs => yargs .command(volumes_list) .command(volumes_ls) .command(volumes_get) + .command(volumes_rm) .command(volumes_backups) .command(volumes_backup) .command(volumes_restore) @@ -27,7 +29,7 @@ export const volumes: CommandModule = { .command(volumes_put) .demandCommand( 1, - 'Specify a volumes command: list, ls, get, backups, backup, restore, export, import or put' + 'Specify a volumes command: list, ls, get, rm, backups, backup, restore, export, import or put' ), handler: () => { // Unreachable: demandCommand(1) either routes to a subcommand or fails diff --git a/src/commands/deploy/volumes/rm.ts b/src/commands/deploy/volumes/rm.ts new file mode 100644 index 0000000..ed976eb --- /dev/null +++ b/src/commands/deploy/volumes/rm.ts @@ -0,0 +1,129 @@ +import { CommandModule } from 'yargs' +import { CliError } from '../../../lib/errors' +import { confirm } from '../../../lib/interactive' +import { json_option, print_json } from '../../../lib/listing' +import { log } from '../../../log' +import { resolve_app } from '../schedule/shared' +import { volume_dest } from './operation' +import { + app_option, + require_completed, + resolve_volume, + state_label, + volume_option, + wait_operation, + wait_option +} from './shared' + +interface RmArgs { + paths?: string[] + undo?: string + app?: string + volume?: string + yes?: boolean + wait?: boolean + json?: boolean +} + +// rm never deletes on the spot (backlog §460): the paths go to the volume's +// trash, `--undo ` brings them back, and they are emptied 7 days later. +export const volumes_rm: CommandModule = { + command: 'rm [paths..]', + describe: + 'Move files or directories of a volume to its trash, with the app serving: --undo brings them back for 7 days, then they are emptied', + builder: yargs => + wait_option(volume_option(app_option(json_option(yargs)))) + .positional('paths', { + type: 'string', + array: true, + description: 'Files or directories of the volume, relative to its root' + }) + .option('undo', { + type: 'string', + description: 'Bring back what an rm moved to the trash (its id)' + }) + .option('yes', { + alias: 'y', + type: 'boolean', + default: false, + description: 'Skip the confirmation prompt' + }) + .check(a => { + const n = ((a.paths as string[] | undefined) ?? []).length + if (a.undo && n) + throw new Error('Either paths to rm, or --undo ') + if (!a.undo && !n) throw new Error('Say what to rm, or --undo ') + return true + }) + .example( + '$0 deploy volumes rm wp-content/plugins/old-plugin -a my-blog', + 'A plugin directory, to the trash' + ) + .example( + '$0 deploy volumes rm --undo volumeop_… -a my-blog', + 'Back from the trash' + ) + .showHelpOnFail(false) as any, + handler: async args => { + const onState = (s: keyof typeof state_label) => { + if (!args.json && s !== 'completed' && s !== 'failed') + log.info(state_label[s]) + } + + if (args.undo) { + const { api, team, app } = await resolve_app(args.app) + const op = await api.undoVolumeOperation(team, args.undo) + if (!args.wait) { + if (args.json) return print_json(op) + log.info(`♻️ Undo ${op.id} queued.`) + return + } + if (!args.json) + log.info( + `♻️ Bringing back ${(op.paths ?? []).join(', ')} in ${app.name}` + ) + const done = await wait_operation(api, team, op, { onState }) + if (args.json) print_json(done) + require_completed(done) + if (!args.json) log.info('✅ Back where they were.') + return + } + + const paths = (args.paths ?? []).map(p => { + const d = volume_dest(p) + if (d === '.') { + throw new CliError( + 'usage', + 'rm takes files or directories, not the whole volume' + ) + } + return d + }) + const { api, team, app, volume } = await resolve_volume( + args.app, + args.volume + ) + const go = await confirm({ + message: `Move ${paths.join(', ')} of ${app.name}/${volume.name} to the trash? They can be brought back for 7 days.`, + yes: args.yes + }) + if (!go) { + log.info('Cancelled.') + return + } + const op = await api.removeVolumePaths(team, volume.id, paths) + const undo = `faable deploy volumes rm --undo ${op.id} -a ${app.name}` + if (!args.wait) { + if (args.json) return print_json(op) + log.info(`🗑 rm ${op.id} queued. To undo it: ${undo}`) + return + } + const done = await wait_operation(api, team, op, { onState }) + if (args.json) print_json(done) + require_completed(done) + if (!args.json) { + log.info(`🗑 Moved to the trash: ${paths.join(', ')}.`) + log.info(`To bring them back (7 days): ${undo}`) + } + } +}