Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion src/api/FaableApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -1015,6 +1027,28 @@ export class FaableApi<T = any> {
)
}

// 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<FaableVolumeOperation>(
`/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<FaableVolumeOperation>(
`/volume-operation/${operation_id}/undo`,
undefined,
{ headers: projectHeader(team) }
)
)
}

async restoreVolume(team: string, volume_id: string, backup_id: string) {
return data(
this.client.post<FaableVolumeOperation>(
Expand Down
6 changes: 4 additions & 2 deletions src/commands/deploy/volumes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,22 @@ 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
// blueprints are published, like the api endpoints (x-internal); then the
// describe below. Arch: deploy/volumenes-cli-comparativa.md (backlog §460).
export const volumes: CommandModule = {
command: 'volumes <command>',
// '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)
Expand All @@ -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
Expand Down
129 changes: 129 additions & 0 deletions src/commands/deploy/volumes/rm.ts
Original file line number Diff line number Diff line change
@@ -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 <rm id>` brings them back, and they are emptied 7 days later.
export const volumes_rm: CommandModule<unknown, RmArgs> = {
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 <rm id>')
if (!a.undo && !n) throw new Error('Say what to rm, or --undo <rm id>')
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}`)
}
}
}
Loading