-
Notifications
You must be signed in to change notification settings - Fork 21
/
awfmgr.go
75 lines (66 loc) · 1.55 KB
/
awfmgr.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
package core
import (
"encoding/json"
"errors"
"github.com/MG-RAST/AWE/lib/conf"
"io/ioutil"
"strings"
)
var (
AwfMgr *WorkflowMgr
)
type WorkflowMgr struct {
awfMap map[string]*Workflow
awfDirPath string
}
func NewWorkflowMgr() *WorkflowMgr {
wfm := new(WorkflowMgr)
wfm.awfMap = map[string]*Workflow{}
wfm.awfDirPath = conf.AWF_PATH
return wfm
}
func InitAwfMgr() {
AwfMgr = NewWorkflowMgr()
}
func (wfm *WorkflowMgr) GetWorkflow(name string) (awf *Workflow, err error) {
if _, ok := wfm.awfMap[name]; ok {
return wfm.awfMap[name], nil
}
return nil, errors.New("workflow not found: " + name)
}
func (wfm *WorkflowMgr) GetAllWorkflows() (workflows []*Workflow) {
for _, wf := range wfm.awfMap {
workflows = append(workflows, wf)
}
return
}
func (wfm *WorkflowMgr) AddWorkflow(name string, awf *Workflow) {
if _, ok := wfm.awfMap[name]; !ok {
wfm.awfMap[name] = awf
}
}
func (wfm *WorkflowMgr) LoadWorkflows() (err error) {
if wfm.awfDirPath == "" {
return errors.New("LoadWorkflows: awfPath not set")
}
files, err := ioutil.ReadDir(wfm.awfDirPath)
if err != nil {
return errors.New("LoadWorkflows: list dir error awfPath:" + wfm.awfDirPath)
}
for _, fileinfo := range files {
filename := fileinfo.Name()
if strings.HasSuffix(filename, ".awf") {
awfpath := wfm.awfDirPath + "/" + filename
awfjson, err := ioutil.ReadFile(awfpath)
if err != nil {
return err
}
wf := new(Workflow)
if err := json.Unmarshal(awfjson, &wf); err != nil {
return err
}
wfm.AddWorkflow(wf.WfInfo.Name, wf)
}
}
return
}