lxd: github.com/lxc/lxd/lxd/revert Index | Examples | Files

package revert

import "github.com/lxc/lxd/lxd/revert"

Index

Examples

Package Files

revert.go

type Reverter Uses

type Reverter struct {
    // contains filtered or unexported fields
}

Reverter is a helper type to manage revert functions.

Code:

revert := revert.New()
defer revert.Fail()

revert.Add(func() { fmt.Println("1st step") })
revert.Add(func() { fmt.Println("2nd step") })

return // Revert functions are run in reverse order on return.

Output:

2nd step
1st step

Code:

revert := revert.New()
defer revert.Fail()

revert.Add(func() { fmt.Println("1st step") })
revert.Add(func() { fmt.Println("2nd step") })

revert.Success() // Revert functions added are not run on return.
return

func New Uses

func New() *Reverter

New returns a new Reverter.

func (*Reverter) Add Uses

func (r *Reverter) Add(f func())

Add adds a revert function to the list to be run when Revert() is called.

func (*Reverter) Clone Uses

func (r *Reverter) Clone() *Reverter

Clone returns a copy of the reverter with the current set of revert functions added. This can be used if you want to return a reverting function to an external caller but do not want to actually execute the previously deferred reverter.Fail() function.

func (*Reverter) Fail Uses

func (r *Reverter) Fail()

Fail runs any revert functions in the reverse order they were added. Should be used with defer or when a task has encountered an error and needs to be reverted.

func (*Reverter) Success Uses

func (r *Reverter) Success()

Success clears the revert functions previously added. Should be called on successful completion of a task to prevent revert functions from being run.

Package revert is imported by 17 packages. Updated 2020-03-22. Refresh now. Tools for package owners.