Skip to content

Commit 9af062e

Browse files
authored
Refactor bufcheck client options for plugin configuration (#3762)
1 parent 8d45601 commit 9af062e

File tree

15 files changed

+290
-253
lines changed

15 files changed

+290
-253
lines changed

private/buf/bufctl/controller.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -487,15 +487,14 @@ func (c *controller) GetTargetImageWithConfigsAndCheckClient(
487487
pluginConfigs,
488488
),
489489
}
490-
pluginRunnerProvider := bufcheck.NewLocalRunnerProvider(
491-
wasmRuntime,
492-
pluginKeyProvider,
493-
c.pluginDataProvider,
494-
)
495490
checkClient, err := bufcheck.NewClient(
496491
c.logger,
497-
pluginRunnerProvider,
498492
bufcheck.ClientWithStderr(c.container.Stderr()),
493+
bufcheck.ClientWithRunnerProvider(
494+
bufcheck.NewLocalRunnerProvider(wasmRuntime),
495+
),
496+
bufcheck.ClientWithLocalWasmPluginsFromOS(),
497+
bufcheck.ClientWithRemoteWasmPlugins(pluginKeyProvider, c.pluginDataProvider),
499498
)
500499
if err != nil {
501500
return nil, nil, err
@@ -802,15 +801,17 @@ func (c *controller) GetCheckClientForWorkspace(
802801
if err != nil {
803802
return nil, err
804803
}
805-
pluginRunnerProvider := bufcheck.NewLocalRunnerProvider(
806-
wasmRuntime,
807-
pluginKeyProvider,
808-
c.pluginDataProvider,
809-
)
810804
return bufcheck.NewClient(
811805
c.logger,
812-
pluginRunnerProvider,
813806
bufcheck.ClientWithStderr(c.container.Stderr()),
807+
bufcheck.ClientWithRunnerProvider(
808+
bufcheck.NewLocalRunnerProvider(wasmRuntime),
809+
),
810+
bufcheck.ClientWithLocalWasmPluginsFromOS(),
811+
bufcheck.ClientWithRemoteWasmPlugins(
812+
pluginKeyProvider,
813+
c.pluginDataProvider,
814+
),
814815
)
815816
}
816817

private/buf/bufmigrate/migrator.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,10 @@ import (
3030
"github.com/bufbuild/buf/private/bufpkg/bufconfig"
3131
"github.com/bufbuild/buf/private/bufpkg/bufmodule"
3232
"github.com/bufbuild/buf/private/bufpkg/bufparse"
33-
"github.com/bufbuild/buf/private/bufpkg/bufplugin"
3433
"github.com/bufbuild/buf/private/pkg/normalpath"
3534
"github.com/bufbuild/buf/private/pkg/slicesext"
3635
"github.com/bufbuild/buf/private/pkg/storage"
3736
"github.com/bufbuild/buf/private/pkg/storage/storagemem"
38-
"github.com/bufbuild/buf/private/pkg/wasm"
3937
"github.com/google/uuid"
4038
)
4139

@@ -697,11 +695,7 @@ func equivalentCheckConfigInV2(
697695
) (bufconfig.CheckConfig, error) {
698696
// No need for custom lint/breaking plugins since there's no plugins to migrate from <=v1.
699697
// TODO: If we ever need v3, then we will have to deal with this.
700-
client, err := bufcheck.NewClient(logger, bufcheck.NewLocalRunnerProvider(
701-
wasm.UnimplementedRuntime,
702-
bufplugin.NopPluginKeyProvider,
703-
bufplugin.NopPluginDataProvider,
704-
))
698+
client, err := bufcheck.NewClient(logger)
705699
if err != nil {
706700
return nil, err
707701
}

private/buf/cmd/buf/buf_test.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ import (
3636
"github.com/bufbuild/buf/private/bufpkg/bufcheck"
3737
"github.com/bufbuild/buf/private/bufpkg/bufconfig"
3838
"github.com/bufbuild/buf/private/bufpkg/bufimage"
39-
"github.com/bufbuild/buf/private/bufpkg/bufplugin"
4039
imagev1 "github.com/bufbuild/buf/private/gen/proto/go/buf/alpha/image/v1"
4140
"github.com/bufbuild/buf/private/pkg/app/appcmd"
4241
"github.com/bufbuild/buf/private/pkg/app/appcmd/appcmdtesting"
@@ -46,7 +45,6 @@ import (
4645
"github.com/bufbuild/buf/private/pkg/slogtestext"
4746
"github.com/bufbuild/buf/private/pkg/storage/storageos"
4847
"github.com/bufbuild/buf/private/pkg/storage/storagetesting"
49-
"github.com/bufbuild/buf/private/pkg/wasm"
5048
"github.com/stretchr/testify/assert"
5149
"github.com/stretchr/testify/require"
5250
)
@@ -1345,14 +1343,7 @@ func TestCheckLsBreakingRulesFromConfigExceptDeprecated(t *testing.T) {
13451343
t.Run(version.String(), func(t *testing.T) {
13461344
t.Parallel()
13471345
// Do not need any custom lint/breaking plugins here.
1348-
client, err := bufcheck.NewClient(
1349-
slogtestext.NewLogger(t),
1350-
bufcheck.NewLocalRunnerProvider(
1351-
wasm.UnimplementedRuntime,
1352-
bufplugin.NopPluginKeyProvider,
1353-
bufplugin.NopPluginDataProvider,
1354-
),
1355-
)
1346+
client, err := bufcheck.NewClient(slogtestext.NewLogger(t))
13561347
require.NoError(t, err)
13571348
allRules, err := client.AllRules(context.Background(), check.RuleTypeBreaking, version)
13581349
require.NoError(t, err)

private/buf/cmd/buf/command/mod/internal/internal.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,11 @@ import (
2424
"github.com/bufbuild/buf/private/buf/bufcli"
2525
"github.com/bufbuild/buf/private/bufpkg/bufcheck"
2626
"github.com/bufbuild/buf/private/bufpkg/bufconfig"
27-
"github.com/bufbuild/buf/private/bufpkg/bufplugin"
2827
"github.com/bufbuild/buf/private/pkg/app/appcmd"
2928
"github.com/bufbuild/buf/private/pkg/app/appext"
3029
"github.com/bufbuild/buf/private/pkg/slicesext"
3130
"github.com/bufbuild/buf/private/pkg/stringutil"
3231
"github.com/bufbuild/buf/private/pkg/syserror"
33-
"github.com/bufbuild/buf/private/pkg/wasm"
3432
"github.com/spf13/pflag"
3533
)
3634

@@ -176,11 +174,6 @@ func lsRun(
176174
// BufYAMLFiles <=v1 never had plugins.
177175
client, err := bufcheck.NewClient(
178176
container.Logger(),
179-
bufcheck.NewLocalRunnerProvider(
180-
wasm.UnimplementedRuntime,
181-
bufplugin.NopPluginKeyProvider,
182-
bufplugin.NopPluginDataProvider,
183-
),
184177
bufcheck.ClientWithStderr(container.Stderr()),
185178
)
186179
if err != nil {

private/buf/cmd/buf/command/plugin/pluginpush/pluginpush.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ func upload(
153153
var err error
154154
plugin, err = bufplugin.NewLocalWasmPlugin(
155155
pluginFullName,
156+
flags.Binary,
156157
nil, // args
157158
func() ([]byte, error) {
158159
wasmBinary, err := os.ReadFile(flags.Binary)

private/buf/cmd/protoc-gen-buf-breaking/breaking.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,9 @@ import (
2828
"github.com/bufbuild/buf/private/bufpkg/bufanalysis"
2929
"github.com/bufbuild/buf/private/bufpkg/bufcheck"
3030
"github.com/bufbuild/buf/private/bufpkg/bufimage"
31-
"github.com/bufbuild/buf/private/bufpkg/bufplugin"
3231
"github.com/bufbuild/buf/private/pkg/encoding"
3332
"github.com/bufbuild/buf/private/pkg/protodescriptor"
3433
"github.com/bufbuild/buf/private/pkg/protoencoding"
35-
"github.com/bufbuild/buf/private/pkg/wasm"
3634
"github.com/bufbuild/protoplugin"
3735
)
3836

@@ -126,11 +124,6 @@ func handle(
126124
// The protoc plugins do not support custom lint/breaking change plugins for now.
127125
client, err := bufcheck.NewClient(
128126
container.Logger(),
129-
bufcheck.NewLocalRunnerProvider(
130-
wasm.UnimplementedRuntime,
131-
bufplugin.NopPluginKeyProvider,
132-
bufplugin.NopPluginDataProvider,
133-
),
134127
bufcheck.ClientWithStderr(pluginEnv.Stderr),
135128
)
136129
if err != nil {

private/buf/cmd/protoc-gen-buf-lint/lint.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,9 @@ import (
2727
"github.com/bufbuild/buf/private/bufpkg/bufanalysis"
2828
"github.com/bufbuild/buf/private/bufpkg/bufcheck"
2929
"github.com/bufbuild/buf/private/bufpkg/bufimage"
30-
"github.com/bufbuild/buf/private/bufpkg/bufplugin"
3130
"github.com/bufbuild/buf/private/pkg/encoding"
3231
"github.com/bufbuild/buf/private/pkg/protodescriptor"
3332
"github.com/bufbuild/buf/private/pkg/protoencoding"
34-
"github.com/bufbuild/buf/private/pkg/wasm"
3533
"github.com/bufbuild/protoplugin"
3634
)
3735

@@ -101,11 +99,6 @@ func handle(
10199
// The protoc plugins do not support custom lint/breaking change plugins for now.
102100
client, err := bufcheck.NewClient(
103101
container.Logger(),
104-
bufcheck.NewLocalRunnerProvider(
105-
wasm.UnimplementedRuntime,
106-
bufplugin.NopPluginKeyProvider,
107-
bufplugin.NopPluginDataProvider,
108-
),
109102
bufcheck.ClientWithStderr(pluginEnv.Stderr),
110103
)
111104
if err != nil {

private/bufpkg/bufcheck/breaking_test.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1346,11 +1346,10 @@ func testBreaking(
13461346
require.NoError(t, err)
13471347
breakingConfig := workspace.GetBreakingConfigForOpaqueID(opaqueID)
13481348
require.NotNil(t, breakingConfig)
1349-
client, err := bufcheck.NewClient(logger, bufcheck.NewLocalRunnerProvider(
1350-
wasm.UnimplementedRuntime,
1351-
bufplugin.NopPluginKeyProvider,
1352-
bufplugin.NopPluginDataProvider,
1353-
))
1349+
client, err := bufcheck.NewClient(
1350+
logger,
1351+
bufcheck.ClientWithRunnerProvider(bufcheck.NewLocalRunnerProvider(wasm.UnimplementedRuntime)),
1352+
)
13541353
require.NoError(t, err)
13551354
err = client.Breaking(
13561355
ctx,

private/bufpkg/bufcheck/bufcheck.go

Lines changed: 55 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/bufbuild/buf/private/bufpkg/bufconfig"
2424
"github.com/bufbuild/buf/private/bufpkg/bufimage"
2525
"github.com/bufbuild/buf/private/bufpkg/bufplugin"
26+
"github.com/bufbuild/buf/private/pkg/pluginrpcutil"
2627
"github.com/bufbuild/buf/private/pkg/slicesext"
2728
"github.com/bufbuild/buf/private/pkg/syserror"
2829
"github.com/bufbuild/buf/private/pkg/wasm"
@@ -176,55 +177,41 @@ func WithPluginConfigs(pluginConfigs ...bufconfig.PluginConfig) ClientFunctionOp
176177

177178
// RunnerProvider provides pluginrpc.Runners for a given plugin config.
178179
type RunnerProvider interface {
179-
NewRunner(pluginConfig bufconfig.PluginConfig) (pluginrpc.Runner, error)
180+
NewRunner(plugin bufplugin.Plugin) (pluginrpc.Runner, error)
180181
}
181182

182183
// RunnerProviderFunc is a function that implements RunnerProvider.
183-
type RunnerProviderFunc func(pluginConfig bufconfig.PluginConfig) (pluginrpc.Runner, error)
184+
type RunnerProviderFunc func(plugin bufplugin.Plugin) (pluginrpc.Runner, error)
184185

185186
// NewRunner implements RunnerProvider.
186187
//
187188
// RunnerProvider selects the correct Runner based on the type of pluginConfig.
188-
func (r RunnerProviderFunc) NewRunner(pluginConfig bufconfig.PluginConfig) (pluginrpc.Runner, error) {
189-
return r(pluginConfig)
189+
func (r RunnerProviderFunc) NewRunner(plugin bufplugin.Plugin) (pluginrpc.Runner, error) {
190+
return r(plugin)
190191
}
191192

192-
// NewLocalRunnerProvider returns a new RunnerProvider for the wasm.Runtime and
193-
// the given plugin providers.
193+
// NewLocalRunnerProvider returns a new RunnerProvider to invoke plugins locally.
194194
//
195195
// This implementation should only be used for local applications. It is safe to
196196
// use concurrently.
197197
//
198-
// The RunnerProvider selects the correct Runner based on the PluginConfigType.
199-
// The supported types are:
200-
// - bufconfig.PluginConfigTypeLocal
201-
// - bufconfig.PluginConfigTypeLocalWasm
202-
// - bufconfig.PluginConfigTypeRemoteWasm
198+
// The RunnerProvider selects the correct Runner based on the Plugin:
199+
// - Local plugins will be run with pluginrpcutil.NewLocalRunner.
200+
// - Local Wasm plugins will be run with pluginrpcutil.NewWasmRunner.
201+
// - Remote Wasm plugins will be run with pluginrpcutil.NewWasmRunner.
203202
//
204-
// If the PluginConfigType is not supported, an error is returned.
203+
// If the plugin type is not supported, an error is returned.
205204
// To disable support for Wasm plugins, set wasmRuntime to wasm.UnimplementedRuntime.
206-
// To disable support for bufconfig.PluginConfigTypeRemoteWasm Plugins, set
207-
// pluginKeyProvider and pluginDataProvider to bufplugin.NopPluginKeyProvider
208-
// and bufplugin.NopPluginDataProvider.
209-
func NewLocalRunnerProvider(
210-
wasmRuntime wasm.Runtime,
211-
pluginKeyProvider bufplugin.PluginKeyProvider,
212-
pluginDataProvider bufplugin.PluginDataProvider,
213-
) RunnerProvider {
214-
return newRunnerProvider(
215-
wasmRuntime,
216-
pluginKeyProvider,
217-
pluginDataProvider,
218-
)
205+
func NewLocalRunnerProvider(wasmRuntime wasm.Runtime) RunnerProvider {
206+
return newLocalRunnerProvider(wasmRuntime)
219207
}
220208

221209
// NewClient returns a new Client.
222210
func NewClient(
223211
logger *slog.Logger,
224-
runnerProvider RunnerProvider,
225212
options ...ClientOption,
226213
) (Client, error) {
227-
return newClient(logger, runnerProvider, options...)
214+
return newClient(logger, options...)
228215
}
229216

230217
// ClientOption is an option for a new Client.
@@ -239,6 +226,47 @@ func ClientWithStderr(stderr io.Writer) ClientOption {
239226
}
240227
}
241228

229+
// ClientWithRunnerProvider returns a new ClientOption that specifies a RunnerProvider.
230+
//
231+
// The runnerProvider is used to create pluginrpc.Runners for the plugins.
232+
// By default, only builtin plugins are used.
233+
func ClientWithRunnerProvider(runnerProvider RunnerProvider) ClientOption {
234+
return func(clientOptions *clientOptions) {
235+
clientOptions.runnerProvider = runnerProvider
236+
}
237+
}
238+
239+
// ClientWithLocalWasmPlugins returns a new ClientOption that specifies reading Wasm plugins.
240+
//
241+
// The readBucket is used to read the Wasm plugin data from the filesystem.
242+
func ClientWithLocalWasmPlugins(readFile func(string) ([]byte, error)) ClientOption {
243+
return func(clientOptions *clientOptions) {
244+
clientOptions.pluginReadFile = readFile
245+
}
246+
}
247+
248+
// ClientWithLocalWasmPluginsFromOS returns a new ClientOption that specifies reading Wasm plugins
249+
// from the OS.
250+
//
251+
// This is only used for local applications.
252+
func ClientWithLocalWasmPluginsFromOS() ClientOption {
253+
return func(clientOptions *clientOptions) {
254+
clientOptions.pluginReadFile = pluginrpcutil.ReadWasmFileFromOS
255+
}
256+
}
257+
258+
// ClientWithRemoteWasmPlugins returns a new ClientOption that specifies the remote plugin key
259+
// and data providers.
260+
func ClientWithRemoteWasmPlugins(
261+
pluginKeyProvider bufplugin.PluginKeyProvider,
262+
pluginDataProvider bufplugin.PluginDataProvider,
263+
) ClientOption {
264+
return func(clientOptions *clientOptions) {
265+
clientOptions.pluginKeyProvider = pluginKeyProvider
266+
clientOptions.pluginDataProvider = pluginDataProvider
267+
}
268+
}
269+
242270
// PrintRules prints the rules to the Writer.
243271
func PrintRules(writer io.Writer, rules []Rule, options ...PrintRulesOption) (retErr error) {
244272
return printRules(writer, rules, options...)

0 commit comments

Comments
 (0)