/
action.go
100 lines (83 loc) · 1.9 KB
/
action.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package confirm
import (
"bytes"
"io"
"strconv"
"strings"
)
type Type int
const (
TypeCreate = iota + 1
TypeUpdate
TypeDelete
)
func NewUpdate(title string, diff []byte, f ActionFunc) *Action {
return New(TypeUpdate, title, diff, f)
}
func NewDelete(title string, diff []byte, f ActionFunc) *Action {
return New(TypeDelete, title, diff, f)
}
func NewCreate(title string, diff []byte, f ActionFunc) *Action {
return New(TypeCreate, title, diff, f)
}
func New(t Type, title string, diff []byte, f ActionFunc) *Action {
if t == 0 {
panic("Type " + strconv.Itoa(int(t)) + " not valid")
}
if title == "" {
panic("title must be present")
}
if f == nil {
panic("ActionFunc must not be nil")
}
return &Action{Type: t, Title: title, Diff: diff, Call: f}
}
type Action struct {
Title string
Diff []byte
Type Type
Call ActionFunc
}
var types = map[Type]string{
TypeCreate: "CREATE",
TypeUpdate: "UPDATE",
TypeDelete: "DELETE",
}
func (s *Action) String() string {
out := []string{}
a, ok := types[s.Type]
if !ok {
panic("type " + strconv.Itoa(int(s.Type)) + " not mapped")
}
out = append(out, a, s.Title)
return strings.Join(out, " ")
}
type ActionFunc func() error
type Actions []*Action
func (list Actions) Exec() error {
for _, a := range list {
if err := a.Call(); err != nil {
return err
}
}
return nil
}
func (list *Actions) Create(title string, diff []byte, f ActionFunc) {
*list = append(*list, NewCreate(title, diff, f))
}
func (list *Actions) Delete(title string, diff []byte, f ActionFunc) {
*list = append(*list, NewDelete(title, diff, f))
}
func (list *Actions) Update(title string, diff []byte, f ActionFunc) {
*list = append(*list, NewUpdate(title, diff, f))
}
func (list Actions) String() string {
buf := &bytes.Buffer{}
for i, a := range list {
io.WriteString(buf, a.String())
if i != len(list)-1 {
io.WriteString(buf, "\n")
}
}
return buf.String()
}