Skip to content

Commit

Permalink
199 add purge to cli (#206)
Browse files Browse the repository at this point in the history
* Update DeWeb SC bytecode in CLI

* Add purge website SC tasks

* Add purge option to delete command for Smart Contract removal
  • Loading branch information
thomas-senechal authored Jan 7, 2025
1 parent 76bfe94 commit 2c88648
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 2 deletions.
12 changes: 12 additions & 0 deletions cli/src/commands/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import { Listr } from 'listr2'

import {
confirmDeleteWebsiteTask,
confirmPurgeWebsiteTask,
deleteWebsiteTask,
prepareDeleteWebsiteTask,
purgeWebsiteTask,
} from '../tasks/delete'
import { DeleteCtx } from '../tasks/tasks'

Expand All @@ -16,6 +18,11 @@ export const deleteCommand = new Command('delete')
.description('Delete the given website from Massa blockchain')
.argument('<address>', 'Address of the website to delete')
.option('-y, --yes', 'Skip confirmation prompt', false)
.option(
'--purge',
'Also delete the Smart Contract from the blockchain',
false
)
.action(async (address, options, command) => {
const globalOptions = command.optsWithGlobals()

Expand All @@ -30,6 +37,7 @@ export const deleteCommand = new Command('delete')
globalMetadatas: [],

skipConfirm: options.yes,
purge: options.purge,
}

const tasksArray = [
Expand All @@ -38,6 +46,10 @@ export const deleteCommand = new Command('delete')
deleteWebsiteTask(),
]

if (options.purge) {
tasksArray.push(confirmPurgeWebsiteTask(), purgeWebsiteTask())
}

const tasks = new Listr(tasksArray, {
concurrent: false,
})
Expand Down
2 changes: 1 addition & 1 deletion cli/src/lib/website/sc/deweb-sc-bytecode.ts

Large diffs are not rendered by default.

62 changes: 61 additions & 1 deletion cli/src/tasks/delete.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ListrEnquirerPromptAdapter } from '@listr2/prompt-adapter-enquirer'
import { OperationStatus } from '@massalabs/massa-web3'
import { ListrTask } from 'listr2'

import { deleteWebsite, prepareDeleteWebsite } from '../lib/website/delete'
Expand Down Expand Up @@ -65,7 +66,7 @@ export function deleteWebsiteTask(): ListrTask {
return
}

deleteWebsite(ctx.sc, ctx.fileDeletes, ctx.globalMetadatas)
await deleteWebsite(ctx.sc, ctx.fileDeletes, ctx.globalMetadatas)
.then(() => {
task.output = 'Website deleted successfully'
})
Expand All @@ -80,3 +81,62 @@ export function deleteWebsiteTask(): ListrTask {
},
}
}

export function confirmPurgeWebsiteTask(): ListrTask {
return {
title: 'Confirm purge operation',
task: async (ctx: DeleteCtx, task) => {
if (!ctx.purge) {
task.skip('No purge operation requested')
return
}

task.output = 'Purge operation requested'
if (ctx.skipConfirm) {
task.skip('Skipping confirmation')
return
}
const answer = await task
.prompt(ListrEnquirerPromptAdapter)
.run<boolean>({
type: 'Toggle',
message: 'Do you want to proceed with the purge operation?',
})

if (answer === false) {
throw new Error('Aborted')
}
},
rendererOptions: {
outputBar: Infinity,
persistentOutput: true,
},
}
}

export function purgeWebsiteTask(): ListrTask {
return {
title: 'Purging website',
task: async (ctx: DeleteCtx, task) => {
task.output = 'Purging website'

const op = await ctx.sc.call('purge')
const status = await op.waitSpeculativeExecution()

if (
status !== OperationStatus.SpeculativeSuccess &&
status !== OperationStatus.Success
) {
throw new Error(
`Failed to execute purge (status: ${status.toString()})`
)
}

task.output = 'Website purged successfully'
},
rendererOptions: {
outputBar: Infinity,
persistentOutput: true,
},
}
}
1 change: 1 addition & 0 deletions cli/src/tasks/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export interface UploadCtx {

export interface DeleteCtx {
sc: SmartContract
purge: boolean

fileDeletes: FileDelete[]
globalMetadatas: Metadata[]
Expand Down

0 comments on commit 2c88648

Please sign in to comment.