SHARE
TWEET

Untitled

a guest Jan 21st, 2020 82 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5.     "errors"
  6. )
  7.  
  8. type InfrastructureAction interface {
  9.     Apply()
  10.     Recovery()
  11. }
  12.  
  13. type RecoveryStack []InfrastructureAction
  14.  
  15. type Step1 struct {
  16.     id string
  17. }
  18.  
  19. type Step2 struct {
  20.     id string
  21. }
  22.  
  23. func (s Step1) Apply() {
  24.     fmt.Println("Step1 apply", s.id)
  25. }
  26.  
  27.  
  28. func (s Step1) Recovery() {
  29.     fmt.Println("Step1 recovery", s.id)
  30. }
  31.  
  32. func (s Step2) Apply() {
  33.     fmt.Println("Step2 apply", s.id)
  34. }
  35.  
  36.  
  37. func (s Step2) Recovery() {
  38.     fmt.Println("Step2 recovery", s.id)
  39. }
  40.  
  41.  
  42. func Perform() (err error) {
  43.    defer func() {
  44.       if r := recover(); r != nil {
  45.          err = r.(error)
  46.       }
  47.    
  48.    }()
  49.    GoesWrong()
  50.    return
  51. }
  52. func GoesWrong() {
  53.    panic(errors.New("Fail"))
  54. }
  55. func main() {
  56.    var s1 InfrastructureAction = &Step1{"some_unique_id_for_step1"}
  57.    var s2 InfrastructureAction = &Step2{"some_unique_id_for_step2"}
  58.  
  59.    var rStack RecoveryStack
  60.  
  61.    rStack = append(rStack, s1)
  62.    rStack = append(rStack, s2)
  63.  
  64.    for idx, _ := range rStack {
  65.     rStack[idx].Apply()
  66.    }
  67. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top