forked from gaulliath/operative-framework
/
source.go
59 lines (51 loc) · 1.11 KB
/
source.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
package session
import (
"encoding/json"
"errors"
"io/ioutil"
"os"
"strings"
"time"
)
type Source struct {
Type string `json:"type"`
Content interface{} `json:"content"`
}
func (s *Session) FromSourceFile() error {
var sources []Source
file, err := os.Open(s.SourceFile)
if err != nil {
return errors.New(err.Error())
}
source, err := ioutil.ReadAll(file)
if err != nil {
return errors.New(err.Error())
}
_ = json.Unmarshal(source, &sources)
if len(sources) > 0 {
for _, source := range sources {
content, _ := json.Marshal(source.Content)
switch strings.ToLower(source.Type) {
case "interval":
var interval Interval
_ = json.Unmarshal(content, &interval)
interval.SetId()
interval.SetSession(s)
interval.SetTimeType("minute")
s.Interval = append(s.Interval, &interval)
interval.Up()
break
case "monitor":
var monitor Monitor
_ = json.Unmarshal(content, &monitor)
monitor.SetId()
monitor.SetSession(s)
monitor.CreatedAt = time.Now()
s.Monitors = append(s.Monitors, &monitor)
monitor.Up()
break
}
}
}
return nil
}