/
action.go
76 lines (68 loc) · 1.66 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
package action
import "strconv"
// Action the action of file change
type Action int
const (
// UnknownAction the unknown file operation
UnknownAction Action = iota
// CreateAction the action of create a file
CreateAction
// WriteAction the action of write data to the file
WriteAction
// RemoveAction the action of remove the file
RemoveAction
// RenameAction the action of rename the file
RenameAction
// ChmodAction the action of change the file mode
ChmodAction
// SymlinkAction the action of create a symbolic link
SymlinkAction
// maxAction the max boundary value of Action, it is an invalid value
maxAction
)
// String return the action description name
func (action Action) String() string {
desc := ""
switch action {
case CreateAction:
desc = "Create"
case WriteAction:
desc = "Write"
case RemoveAction:
desc = "Remove"
case RenameAction:
desc = "Rename"
case ChmodAction:
desc = "Chmod"
case SymlinkAction:
desc = "Symlink"
case UnknownAction:
desc = "Unknown"
default:
desc = "Invalid"
}
return desc
}
// Int return the int value of Action
func (action Action) Int() int {
return int(action)
}
// Valid if the current Action is an invalid int value, return the UnknownAction
func (action Action) Valid() Action {
if action >= maxAction || action <= UnknownAction {
return UnknownAction
}
return action
}
// ParseActionFromString parse the string value to Action
func ParseActionFromString(action string) Action {
i, err := strconv.Atoi(action)
if err != nil {
return UnknownAction
}
return ParseAction(i)
}
// ParseAction parse the int value to Action
func ParseAction(action int) Action {
return Action(action).Valid()
}