-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathaction.go
More file actions
76 lines (65 loc) · 1.65 KB
/
action.go
File metadata and controls
76 lines (65 loc) · 1.65 KB
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 fsagent
import (
"encoding/json"
"log"
"time"
"github.com/SimonWaldherr/golibs/cachedfile"
gfile "github.com/SimonWaldherr/golibs/file"
"simonwaldherr.de/go/fsagent/modules"
)
func init() {
cachedfile.Init(15*time.Minute, 1*time.Minute)
}
// Action is something that should be performed.
type Action []struct {
Do string `json:"do"`
Config json.RawMessage `json:"config"`
Onsuccess Action `json:"onSuccess"`
Onfailure Action `json:"onFailure"`
}
// Actionable defines a common set of methods each action should have.
type Actionable interface {
Name() string
EmptyConfig() interface{}
Perform(config interface{}, fileName string) error
}
// Actions is a list of Actionable types that can be added to.
var Actions = []Actionable{
&modules.Mail{},
&modules.HTTP{},
&modules.Sleep{},
&modules.Delete{},
&modules.Move{},
&modules.Decompress{},
&modules.Compress{},
&modules.IsFile{},
&modules.CheckSize{},
}
func do(act Action, file string) {
for _, a := range act {
var err error
log.Printf("Do \"%v\" on file \"%v\"\n", a.Do, file)
for _, action := range Actions {
if a.Do == action.Name() {
config := action.EmptyConfig()
if gfile.IsFile(string(a.Config)) {
str, _ := cachedfile.Read(string(a.Config))
json.Unmarshal([]byte(str), &config)
} else {
json.Unmarshal(a.Config, &config)
}
err = action.Perform(config, file)
if err != nil {
log.Printf("Error \"%v\" for file \"%v\"", err, file)
}
break
}
}
if err == nil {
do(a.Onsuccess, file)
} else {
log.Printf("Error \"%v\" for file \"%v\"", err, file)
do(a.Onfailure, file)
}
}
}