-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipc.go
42 lines (38 loc) · 836 Bytes
/
ipc.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
package ipc
import (
"encoding/json"
"fmt"
"garden-external-networker/manager"
"io"
)
type Mux struct {
Up func(handle string, inputs manager.UpInputs) (*manager.UpOutputs, error)
Down func(handle string) error
}
func (m *Mux) Handle(action string, handle string, stdin io.Reader, stdout io.Writer) error {
if handle == "" {
return fmt.Errorf("missing handle")
}
switch action {
case "up":
var inputs manager.UpInputs
if err := json.NewDecoder(stdin).Decode(&inputs); err != nil {
return err
}
outputs, err := m.Up(handle, inputs)
if err != nil {
return err
}
if err := json.NewEncoder(stdout).Encode(outputs); err != nil {
return err
}
case "down":
err := m.Down(handle)
if err != nil {
return err
}
default:
return fmt.Errorf("unrecognized action: %s", action)
}
return nil
}