-
Notifications
You must be signed in to change notification settings - Fork 1
/
executor.go
89 lines (76 loc) · 1.65 KB
/
executor.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
84
85
86
87
88
89
package executor
type Executor interface {
Run() error
Init() error
}
type ExecCreator struct {
//IsRemote bool
session execSessionBuilder
}
type CmdSample struct {
Name string
Code string
Desc string
Logging bool
}
var Sessions = struct {
Remote func(config SessionConfig) execSessionBuilder
Local func() execSessionBuilder
}{
Remote: newRemoteSession,
Local: newLocalSession,
}
func New(cmdList []Command, async bool) (Executor, error) {
h := &exor{
list: cmdList,
resultChan: make(chan *Command),
closeChan: make(chan bool, 1),
async: async,
}
if err := h.Init(); err != nil {
return nil, err
}
return h, nil
}
func Local() *ExecCreator {
creator := &ExecCreator{
//IsRemote: false,
session: Sessions.Local(),
}
return creator
}
func Remote(config SessionConfig) *ExecCreator {
creator := &ExecCreator{
//IsRemote: true,
session: Sessions.Remote(config),
}
return creator
}
func (e *ExecCreator) Default(commandLines []CmdSample) (Executor, error) {
var (
list []Command = []Command{}
async bool = false
executor *exor = &exor{
list: nil,
resultChan: make(chan *Command),
closeChan: make(chan bool, 1),
async: async,
}
err error
)
for _, sample := range commandLines {
list = append(list, Command{
Name: sample.Name,
Code: sample.Code,
Desc: sample.Desc,
Session: e.session,
Async: async,
Logging: sample.Logging,
})
}
executor.list = list
if err = executor.Init(); err != nil {
return nil, err
}
return executor, nil
}