revert

package
v0.0.0-...-ff4e629 Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2024 License: AGPL-3.0, Apache-2.0 Imports: 0 Imported by: 1

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Hook

type Hook func()

Hook is a function that can be added to the revert via the Add() function. These will be run in the reverse order that they were added if the reverter's Fail() function is called.

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/canonical/lxd/shared/revert"
)

func main() {
	revert := revert.New()
	defer revert.Fail()

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

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

Example (Success)
package main

import (
	"fmt"

	"github.com/canonical/lxd/shared/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.
}
Output:

func New

func New() *Reverter

New returns a new Reverter.

func (*Reverter) Add

func (r *Reverter) Add(f Hook)

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