-
Notifications
You must be signed in to change notification settings - Fork 2
/
status.go
79 lines (66 loc) · 1.78 KB
/
status.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
package status
type Status struct {
Name string
Value int
}
func (e Status) Status() Status {
return e
}
type GetStatus interface {
Status() Status
}
var (
Null = Status{"", -1}
Indexed = Status{"indexed", 0}
Retrying = Status{"retrying", 0}
Queued = Status{"queued", 1}
Downloading = Status{"downloading", 2}
Uploading = Status{"uploading", 2}
Skipped = Status{"skipped", 3}
Ignored = Status{"ignored", 3}
Complete = Status{"complete", 4}
Canceled = Status{"canceled", 5}
Errored = Status{"errored", 5}
Included = []GetStatus{Indexed, Queued, Retrying, Downloading, Uploading, Complete, Canceled, Errored}
Excluded = []GetStatus{Skipped, Ignored}
Valid = []GetStatus{Indexed, Queued, Retrying, Downloading, Uploading, Complete}
Invalid = []GetStatus{Null, Canceled, Errored, Skipped, Ignored}
Running = []GetStatus{Downloading, Uploading}
Ended = []GetStatus{Complete, Canceled, Errored, Skipped, Ignored}
)
func (e Status) String() string {
return e.Name
}
func (e Status) Has(statuses ...GetStatus) bool {
return e.Any(statuses...)
}
func (e Status) Is(statuses ...GetStatus) bool {
return e.Any(statuses...)
}
func (e Status) IsNot(statuses ...GetStatus) bool {
return !e.Any(statuses...)
}
func (e Status) is(status GetStatus) bool {
return e.Name == status.Status().Name
}
func (e Status) Any(statuses ...GetStatus) bool {
if len(statuses) == 0 {
return true
}
for _, status := range statuses {
if e.is(status) {
return true
}
}
return false
}
func SetStatus(old GetStatus, new GetStatus, err error) (Status, bool) {
var setError bool
if err != nil || new.Status().Is(Retrying) {
setError = true
}
if old.Status().Is(Errored) && new.Status().Is(Running...) {
new = old
}
return new.Status(), setError
}