/
state.go
60 lines (49 loc) · 1.25 KB
/
state.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
package op
import (
"github.com/fatih/color"
)
type state int
const (
stateComplete state = iota
stateIncomplete
stateError
stateUnknown
)
// State represents possible target or source state
type State struct {
value state
label string
}
func (st *State) String() string {
if st.IsComplete() {
return color.New(color.FgGreen).Sprint(st.label)
}
if st.IsError() {
return color.New(color.FgRed).Sprintf("err: %s", st.label)
}
return st.label
}
// IsComplete checks whether state is `stateComplete`
func (st *State) IsComplete() bool {
return st.value == stateComplete
}
// IsError checks whether state is `stateError`
func (st *State) IsError() bool {
return st.value == stateError
}
// NewCompleteState returns State of `stateComplete`
func NewCompleteState(l string) *State {
return &State{value: stateComplete, label: l}
}
// NewIncompleteState returns State of `stateIncomplete`
func NewIncompleteState(l string) *State {
return &State{value: stateIncomplete, label: l}
}
// NewErrorState returns State of `stateError`
func NewErrorState(l string) *State {
return &State{value: stateError, label: l}
}
// NewUnknownState returns State of `stateUnknown`
func NewUnknownState(l string) *State {
return &State{value: stateUnknown, label: l}
}