revert

package
v0.0.0-...-04e5504 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 3, 2021 License: Apache-2.0 Imports: 0 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Reverter

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

Reverter is a helper type to manage revert functions.

Example (Fail)
package main

import (
	"fmt"

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

func main() {
	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
Example (Success)
package main

import (
	"fmt"

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

func main() {
	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
}
Output:

func New

func New() *Reverter

New returns a new Reverter.

func (*Reverter) Add

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

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

func (*Reverter) Clone

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

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

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL