forked from thecodeteam/libstorage
/
executors.go
83 lines (71 loc) · 1.66 KB
/
executors.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
77
78
79
80
81
82
83
package executors
import (
"encoding/json"
"regexp"
// depend upon this tool with a nil import in order to preserve it
// in the dependency list
_ "github.com/jteeuwen/go-bindata"
"github.com/emccode/libstorage/api/types"
"github.com/emccode/libstorage/api/utils"
)
var (
executors = map[string]*ExecutorInfoEx{}
pathRX = regexp.MustCompile(`^lsx-(.+?)(?:.exe)?$`)
)
func init() {
for path, bdFunc := range _bindata {
bd, err := bdFunc()
if err != nil {
panic(err)
}
executors[path] = &ExecutorInfoEx{
ExecutorInfo: types.ExecutorInfo{
Name: path,
MD5Checksum: bd.info.MD5Checksum(),
Size: bd.info.Size(),
LastModified: bd.info.ModTime().Unix(),
},
}
}
}
// ExecutorInfos returns a channel on which all executor information can be
// received.
func ExecutorInfos() <-chan *ExecutorInfoEx {
c := make(chan *ExecutorInfoEx)
go func() {
for _, v := range executors {
c <- v
}
close(c)
}()
return c
}
// ExecutorInfoInspect returns the executor info for the provided name.
func ExecutorInfoInspect(name string, data bool) (*ExecutorInfoEx, error) {
ei, ok := executors[name]
if !ok {
return nil, utils.NewNotFoundError(name)
}
if !data {
return ei, nil
}
bd, ok := _bindata[ei.Name]
if !ok {
return nil, utils.NewNotFoundError(name)
}
a, err := bd()
if err != nil {
return nil, err
}
ei.Data = a.bytes
return ei, nil
}
// ExecutorInfoEx is an extension of ExecutorInfo
type ExecutorInfoEx struct {
types.ExecutorInfo
Data []byte `json:"-"`
}
// MarshalJSON marshals the ExecutorInfoEx to JSON.
func (i *ExecutorInfoEx) MarshalJSON() ([]byte, error) {
return json.Marshal(&i.ExecutorInfo)
}