forked from twitchtv/twirp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.go
109 lines (93 loc) · 3.13 KB
/
run.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Copyright 2018 Twitch Interactive, Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may not
// use this file except in compliance with the License. A copy of the License is
// located at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// or in the "license" file accompanying this file. This file is distributed on
// an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing
// permissions and limitations under the License.
package main
import (
"bytes"
"log"
"os/exec"
"github.com/pkg/errors"
"github.com/twitchtv/twirp/clientcompat/internal/clientcompat"
"google.golang.org/protobuf/proto"
)
func runClient(clientBin string, msg *clientcompat.ClientCompatMessage) (resp []byte, errCode string, err error) {
cmd := exec.Command(clientBin)
msgBytes, err := proto.Marshal(msg)
if err != nil {
return nil, "", errors.Wrap(err, "unable to marshal ClientCompatMessage message")
}
cmd.Stdin = bytes.NewReader(msgBytes)
stdout, stderr := bytes.NewBuffer(nil), bytes.NewBuffer(nil)
cmd.Stdout = stdout
cmd.Stderr = stderr
err = cmd.Run()
if err != nil {
err = errors.Wrap(err, "error running client binary")
log.Printf("client stdout: %s", stdout.String())
log.Printf("client stderr: %s", stderr.String())
return nil, "", err
}
if stdout.Len() > 0 && stderr.Len() > 0 {
return nil, "", errors.Errorf("client bin should write to either stdout or stderr, but never both in one invocation")
}
if stderr.Len() > 0 {
return nil, stderr.String(), err
}
return stdout.Bytes(), "", nil
}
func runClientNoop(serverURL string, clientBin string) (resp *clientcompat.Empty, twirpErrCode string, err error) {
req := &clientcompat.Empty{}
reqBytes, err := proto.Marshal(req)
if err != nil {
return nil, "", errors.Wrap(err, "unable to marshal Empty message")
}
msg := &clientcompat.ClientCompatMessage{
ServiceAddress: serverURL,
Method: clientcompat.ClientCompatMessage_NOOP,
Request: reqBytes,
}
respBytes, code, err := runClient(clientBin, msg)
if err != nil {
return nil, "", err
}
if respBytes != nil {
resp = new(clientcompat.Empty)
err = proto.Unmarshal(respBytes, resp)
if err != nil {
return nil, "", errors.Wrap(err, "unable to unmarshal stdout from client bin as an Empty response")
}
}
return resp, code, nil
}
func runClientMethod(serverURL string, clientBin string, req *clientcompat.Req) (resp *clientcompat.Resp, twirpErrCode string, err error) {
reqBytes, err := proto.Marshal(req)
if err != nil {
return nil, "", errors.Wrap(err, "unable to marshal Req")
}
msg := &clientcompat.ClientCompatMessage{
ServiceAddress: serverURL,
Method: clientcompat.ClientCompatMessage_METHOD,
Request: reqBytes,
}
respBytes, code, err := runClient(clientBin, msg)
if err != nil {
return nil, "", err
}
if respBytes != nil {
resp = new(clientcompat.Resp)
err = proto.Unmarshal(respBytes, resp)
if err != nil {
return nil, "", errors.Wrap(err, "unable to unmarshal stdout from client bin as a Resp")
}
}
return resp, code, nil
}