Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/umutbasal/rollback
Rollback package provides a simple way to manage and execute rollback operations in Go
https://github.com/umutbasal/rollback
golang rollback
Last synced: about 1 month ago
JSON representation
Rollback package provides a simple way to manage and execute rollback operations in Go
- Host: GitHub
- URL: https://github.com/umutbasal/rollback
- Owner: umutbasal
- License: mit
- Created: 2024-01-31T11:37:25.000Z (10 months ago)
- Default Branch: main
- Last Pushed: 2024-01-31T15:42:52.000Z (10 months ago)
- Last Synced: 2024-06-21T15:48:09.710Z (5 months ago)
- Topics: golang, rollback
- Language: Go
- Homepage:
- Size: 12.7 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Rollback Package
The `rollback` package provides a simple way to manage and execute rollback operations in Go.
## Usage
```go
package mainimport (
"fmt"
"github.com/umutbasal/rollback"
yourawesomeproject "github.com/your/awesome/project/lib"
)func main() {
// Create a new rollbacker
rb := rollback.New()
// Execute rollback operations on defer
defer rb.Rollback()// 1) needs rollback on its own error
if err := yourawesomeproject.DoSomething(); err != nil {
rb.Add(func() {
return yourawesomeproject.UndoSomething()
})
return err
}// 2) needs rollback on any error after this point including its own
rb.Add(func() {
return yourawesomeproject.UndoSomethingElse()
})
if err := yourawesomeproject.DoSomethingElse(); err != nil {
return err
}// 3) does not need rollback on its own error
if err := yourawesomeproject.DoSomethingMore(); err != nil {
return err
}
// but needs rollback if operations after this point fails
rb.Add(func() {
return yourawesomeproject.UndoSomethingMore()
})// No error, finalize and reset rollbacker
rb.Finalize()
}
```### Clone
This way you can pass rollback functions to upper rollbackers without executing them.```go
// Create a new Rollbacker
rollbacker := New()
rollbacker.Add(func() {
fmt.Println("Rollbacker 1 function 1")
})
rollbacker.Add(func() {
fmt.Println("Rollbacker 1 function 2")
})// Create another Rollbacker
rollbacker2 := New()
rollbacker2.Add(func() {
fmt.Println("Rollbacker 2 function 1")
})// Clone the Rollbackers
rbfn := rollbacker.Clone().Rollback
rbfn2 := rollbacker2.Clone().Rollback// Create an upper Rollbacker
upperRollbacker := New()
upperRollbacker.Add(rbfn)
upperRollbacker.Add(rbfn2)// Rollback the upper Rollbacker
upperRollbacker.Rollback()// Output:
// Rollbacker 2 function 1
// Rollbacker 1 function 2
// Rollbacker 1 function 1
```