forked from bufbuild/buf
/
bufwire.go
253 lines (234 loc) · 7.1 KB
/
bufwire.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// Copyright 2020-2023 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 bufwire wires everything together.
//
// TODO: This package should be split up into individual functionality.
package bufwire
import (
"context"
"github.com/xiaoyawei/buf/private/buf/bufconvert"
"github.com/xiaoyawei/buf/private/buf/buffetch"
"github.com/xiaoyawei/buf/private/bufpkg/bufanalysis"
"github.com/xiaoyawei/buf/private/bufpkg/bufconfig"
"github.com/xiaoyawei/buf/private/bufpkg/bufimage"
"github.com/xiaoyawei/buf/private/bufpkg/bufimage/bufimagebuild"
"github.com/xiaoyawei/buf/private/bufpkg/bufmodule"
"github.com/xiaoyawei/buf/private/bufpkg/bufmodule/bufmodulebuild"
"github.com/xiaoyawei/buf/private/bufpkg/bufmodule/bufmoduleref"
"github.com/xiaoyawei/buf/private/pkg/app"
"github.com/xiaoyawei/buf/private/pkg/storage/storageos"
"go.uber.org/zap"
"google.golang.org/protobuf/proto"
)
// ImageConfig is an image and configuration.
type ImageConfig interface {
Image() bufimage.Image
Config() *bufconfig.Config
}
// ImageConfigReader is an ImageConfig reader.
type ImageConfigReader interface {
// GetImageConfigs gets the ImageConfig for the fetch value.
//
// If externalDirOrFilePaths is empty, this builds all files under Buf control.
GetImageConfigs(
ctx context.Context,
container app.EnvStdinContainer,
ref buffetch.Ref,
configOverride string,
externalDirOrFilePaths []string,
externalExcludeDirOrFilePaths []string,
externalDirOrFilePathsAllowNotExist bool,
excludeSourceCodeInfo bool,
) ([]ImageConfig, []bufanalysis.FileAnnotation, error)
}
// NewImageConfigReader returns a new ImageConfigReader.
func NewImageConfigReader(
logger *zap.Logger,
storageosProvider storageos.Provider,
fetchReader buffetch.Reader,
moduleBucketBuilder bufmodulebuild.ModuleBucketBuilder,
moduleFileSetBuilder bufmodulebuild.ModuleFileSetBuilder,
imageBuilder bufimagebuild.Builder,
) ImageConfigReader {
return newImageConfigReader(
logger,
storageosProvider,
fetchReader,
moduleBucketBuilder,
moduleFileSetBuilder,
imageBuilder,
)
}
// ModuleConfig is an module and configuration.
type ModuleConfig interface {
Module() bufmodule.Module
Config() *bufconfig.Config
Workspace() bufmodule.Workspace
}
// ModuleConfigReader is a ModuleConfig reader.
type ModuleConfigReader interface {
// GetModuleConfigs gets the ModuleConfig for the fetch value.
//
// If externalDirOrFilePaths is empty, this builds all files under Buf control.
//
// Note that as opposed to ModuleReader, this will return a Module for either
// a source or module reference, not just a module reference.
GetModuleConfigs(
ctx context.Context,
container app.EnvStdinContainer,
sourceOrModuleRef buffetch.SourceOrModuleRef,
configOverride string,
externalDirOrFilePaths []string,
externalExcludeDirOrFilePaths []string,
externalDirOrFilePathsAllowNotExist bool,
) ([]ModuleConfig, error)
}
// NewModuleConfigReader returns a new ModuleConfigReader
func NewModuleConfigReader(
logger *zap.Logger,
storageosProvider storageos.Provider,
fetchReader buffetch.Reader,
moduleBucketBuilder bufmodulebuild.ModuleBucketBuilder,
) ModuleConfigReader {
return newModuleConfigReader(
logger,
storageosProvider,
fetchReader,
moduleBucketBuilder,
)
}
// FileLister lists files.
type FileLister interface {
// ListFiles lists the files.
//
// If includeImports is set, the ref is built, which can result in FileAnnotations.
// There is no defined returned sorting order. If you need the FileInfos to
// be sorted, do so with bufmoduleref.SortFileInfos or bufmoduleref.SortFileInfosByExternalPath.
ListFiles(
ctx context.Context,
container app.EnvStdinContainer,
ref buffetch.Ref,
configOverride string,
includeImports bool,
) ([]bufmoduleref.FileInfo, []bufanalysis.FileAnnotation, error)
}
// NewFileLister returns a new FileLister.
func NewFileLister(
logger *zap.Logger,
storageosProvider storageos.Provider,
fetchReader buffetch.Reader,
moduleBucketBuilder bufmodulebuild.ModuleBucketBuilder,
moduleFileSetBuilder bufmodulebuild.ModuleFileSetBuilder,
imageBuilder bufimagebuild.Builder,
) FileLister {
return newFileLister(
logger,
storageosProvider,
fetchReader,
moduleBucketBuilder,
moduleFileSetBuilder,
imageBuilder,
)
}
// ImageReader is an image reader.
type ImageReader interface {
// GetImage reads the image from the value.
GetImage(
ctx context.Context,
container app.EnvStdinContainer,
imageRef buffetch.ImageRef,
externalDirOrFilePaths []string,
externalExcludeDirOrFilePaths []string,
externalDirOrFilePathsAllowNotExist bool,
excludeSourceCodeInfo bool,
) (bufimage.Image, error)
}
// NewImageReader returns a new ImageReader.
func NewImageReader(
logger *zap.Logger,
fetchReader buffetch.ImageReader,
) ImageReader {
return newImageReader(
logger,
fetchReader,
)
}
// ImageWriter is an image writer.
type ImageWriter interface {
// PutImage writes the image to the value.
//
// The file must be an image format.
// This is a no-np if value is the equivalent of /dev/null.
PutImage(
ctx context.Context,
container app.EnvStdoutContainer,
imageRef buffetch.ImageRef,
image bufimage.Image,
asFileDescriptorSet bool,
excludeImports bool,
) error
}
// NewImageWriter returns a new ImageWriter.
func NewImageWriter(
logger *zap.Logger,
fetchWriter buffetch.Writer,
) ImageWriter {
return newImageWriter(
logger,
fetchWriter,
)
}
// ProtoEncodingReader is a reader that reads a protobuf message in different encoding.
type ProtoEncodingReader interface {
// GetMessage reads the message by the messageRef.
//
// Currently, this support bin and JSON format.
GetMessage(
ctx context.Context,
container app.EnvStdinContainer,
image bufimage.Image,
typeName string,
messageRef bufconvert.MessageEncodingRef,
) (proto.Message, error)
}
// NewProtoEncodingReader returns a new ProtoEncodingReader.
func NewProtoEncodingReader(
logger *zap.Logger,
) ProtoEncodingReader {
return newProtoEncodingReader(
logger,
)
}
// ProtoEncodingWriter is a writer that writes a protobuf message in different encoding.
type ProtoEncodingWriter interface {
// PutMessage writes the message to the path, which can be
// a path in file system, or stdout represented by "-".
//
// Currently, this support bin and JSON format.
PutMessage(
ctx context.Context,
container app.EnvStdoutContainer,
image bufimage.Image,
message proto.Message,
messageRef bufconvert.MessageEncodingRef,
) error
}
// NewProtoEncodingWriter returns a new ProtoEncodingWriter.
func NewProtoEncodingWriter(
logger *zap.Logger,
) ProtoEncodingWriter {
return newProtoEncodingWriter(
logger,
)
}