MCPcopy
hub / github.com/probelabs/goreplay / NewMiddleware

Function NewMiddleware

middleware.go:29–65  ·  view source on GitHub ↗

NewMiddleware returns new middleware

(command string)

Source from the content-addressed store, hash-verified

27
28// NewMiddleware returns new middleware
29func NewMiddleware(command string) *Middleware {
30 m := new(Middleware)
31 m.command = command
32 m.data = make(chan *Message, 1000)
33 m.stop = make(chan bool)
34
35 commands := strings.Split(command, " ")
36 ctx, cancl := context.WithCancel(context.Background())
37 m.commandCancel = cancl
38 cmd := exec.CommandContext(ctx, commands[0], commands[1:]...)
39
40 m.Stdout, _ = cmd.StdoutPipe()
41 m.Stdin, _ = cmd.StdinPipe()
42
43 cmd.Stderr = os.Stderr
44
45 go m.read(m.Stdout)
46
47 go func() {
48 defer m.Close()
49 var err error
50 if err = cmd.Start(); err == nil {
51 err = cmd.Wait()
52 }
53 if err != nil {
54 if e, ok := err.(*exec.ExitError); ok {
55 status := e.Sys().(syscall.WaitStatus)
56 if status.Signal() == syscall.SIGKILL /*killed or context canceld */ {
57 return
58 }
59 }
60 Debug(0, fmt.Sprintf("[MIDDLEWARE] command[%q] error: %q", command, err.Error()))
61 }
62 }()
63
64 return m
65}
66
67// ReadFrom start a worker to read from this plugin
68func (m *Middleware) ReadFrom(plugin PluginReader) {

Callers 1

StartMethod · 0.85

Calls 5

StartMethod · 0.80
DebugFunction · 0.70
CloseMethod · 0.65
readMethod · 0.45
ErrorMethod · 0.45

Tested by

no test coverage detected