/
binary_handler.go
120 lines (112 loc) · 3.21 KB
/
binary_handler.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
110
111
112
113
114
115
116
117
118
119
120
// Copyright 2020 Buf Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License 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 appprotoexec
import (
"bytes"
"context"
"os/exec"
"path/filepath"
"github.com/bufbuild/buf/internal/pkg/app"
"github.com/bufbuild/buf/internal/pkg/app/appproto"
"github.com/bufbuild/buf/internal/pkg/protoencoding"
"go.opencensus.io/trace"
"go.uber.org/zap"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/pluginpb"
)
var defaultVersion = newVersion(
DefaultMajorVersion,
DefaultMinorVersion,
DefaultPatchVersion,
DefaultSuffixVersion,
)
type binaryHandler struct {
logger *zap.Logger
pluginPath string
}
func newBinaryHandler(
logger *zap.Logger,
pluginPath string,
) *binaryHandler {
return &binaryHandler{
logger: logger.Named("appprotoexec"),
pluginPath: pluginPath,
}
}
func (h *binaryHandler) Handle(
ctx context.Context,
container app.EnvStderrContainer,
responseWriter appproto.ResponseWriter,
request *pluginpb.CodeGeneratorRequest,
) error {
ctx, span := trace.StartSpan(ctx, "plugin_proxy")
span.AddAttributes(trace.StringAttribute("plugin", filepath.Base(h.pluginPath)))
defer span.End()
unsetRequestVersion := false
if request.CompilerVersion == nil {
unsetRequestVersion = true
request.CompilerVersion = defaultVersion
}
requestData, err := protoencoding.NewWireMarshaler().Marshal(request)
if unsetRequestVersion {
request.CompilerVersion = nil
}
if err != nil {
return err
}
responseBuffer := bytes.NewBuffer(nil)
cmd := exec.CommandContext(ctx, h.pluginPath)
cmd.Env = app.Environ(container)
cmd.Stdin = bytes.NewReader(requestData)
cmd.Stdout = responseBuffer
cmd.Stderr = container.Stderr()
if err := cmd.Run(); err != nil {
// TODO: strip binary path as well?
return err
}
response := &pluginpb.CodeGeneratorResponse{}
if err := protoencoding.NewWireUnmarshaler(nil).Unmarshal(responseBuffer.Bytes(), response); err != nil {
return err
}
response, err = normalizeCodeGeneratorResponse(response)
if err != nil {
return err
}
if response.GetSupportedFeatures()&uint64(pluginpb.CodeGeneratorResponse_FEATURE_PROTO3_OPTIONAL) != 0 {
responseWriter.SetFeatureProto3Optional()
}
for _, file := range response.File {
if err := responseWriter.Add(file); err != nil {
return err
}
}
// call AddError even if this is empty
if response.Error != nil {
if err := responseWriter.AddError(response.GetError()); err != nil {
return err
}
}
return nil
}
func newVersion(major int32, minor int32, patch int32, suffix string) *pluginpb.Version {
version := &pluginpb.Version{
Major: proto.Int32(major),
Minor: proto.Int32(minor),
Patch: proto.Int32(patch),
}
if suffix != "" {
version.Suffix = proto.String(suffix)
}
return version
}