Skip to content

Commit 1dd3a77

Browse files
authored
Add buf plugin update command (#3482)
1 parent 1c84307 commit 1dd3a77

File tree

26 files changed

+1246
-36
lines changed

26 files changed

+1246
-36
lines changed

private/buf/bufcli/cache.go

Lines changed: 60 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,32 +26,40 @@ import (
2626
"github.com/bufbuild/buf/private/bufpkg/bufmodule/bufmoduleapi"
2727
"github.com/bufbuild/buf/private/bufpkg/bufmodule/bufmodulecache"
2828
"github.com/bufbuild/buf/private/bufpkg/bufmodule/bufmodulestore"
29+
"github.com/bufbuild/buf/private/bufpkg/bufplugin"
30+
"github.com/bufbuild/buf/private/bufpkg/bufplugin/bufpluginapi"
31+
"github.com/bufbuild/buf/private/bufpkg/bufplugin/bufplugincache"
32+
"github.com/bufbuild/buf/private/bufpkg/bufplugin/bufpluginstore"
2933
"github.com/bufbuild/buf/private/bufpkg/bufregistryapi/bufregistryapimodule"
3034
"github.com/bufbuild/buf/private/bufpkg/bufregistryapi/bufregistryapiowner"
35+
"github.com/bufbuild/buf/private/bufpkg/bufregistryapi/bufregistryapiplugin"
3136
"github.com/bufbuild/buf/private/pkg/app/appext"
3237
"github.com/bufbuild/buf/private/pkg/filelock"
3338
"github.com/bufbuild/buf/private/pkg/normalpath"
3439
"github.com/bufbuild/buf/private/pkg/storage/storageos"
3540
)
3641

3742
var (
38-
// AllCacheModuleRelDirPaths are all directory paths for all time concerning the module cache.
43+
// AllCacheRelDirPaths are all directory paths for all time
44+
// concerning the module and plugin caches.
3945
//
4046
// These are normalized.
4147
// These are relative to container.CacheDirPath().
4248
//
4349
// This variable is used for clearing the cache.
44-
AllCacheModuleRelDirPaths = []string{
45-
v1beta1CacheModuleDataRelDirPath,
46-
v1beta1CacheModuleLockRelDirPath,
50+
AllCacheRelDirPaths = []string{
4751
v1CacheModuleDataRelDirPath,
4852
v1CacheModuleLockRelDirPath,
4953
v1CacheModuleSumRelDirPath,
54+
v1beta1CacheModuleDataRelDirPath,
55+
v1beta1CacheModuleLockRelDirPath,
5056
v2CacheModuleRelDirPath,
51-
v3CacheModuleRelDirPath,
5257
v3CacheCommitsRelDirPath,
53-
v3CacheWKTRelDirPath,
5458
v3CacheModuleLockRelDirPath,
59+
v3CacheModuleRelDirPath,
60+
v3CachePluginRelDirPath,
61+
v3CacheWKTRelDirPath,
62+
v3CacheWasmRuntimeRelDirPath,
5563
}
5664

5765
// v1CacheModuleDataRelDirPath is the relative path to the cache directory where module data
@@ -103,6 +111,10 @@ var (
103111
//
104112
// Normalized.
105113
v3CacheModuleLockRelDirPath = normalpath.Join("v3", "modulelocks")
114+
// v3CachePluginRelDirPath is the relative path to the files cache directory in its newest iteration.
115+
//
116+
// Normalized.
117+
v3CachePluginRelDirPath = normalpath.Join("v3", "plugins")
106118
// v3CacheWasmRuntimeRelDirPath is the relative path to the Wasm runtime cache directory in its newest iteration.
107119
// This directory is used to store the Wasm runtime cache. This is an implementation specific cache and opaque outside of the runtime.
108120
//
@@ -146,6 +158,21 @@ func NewCommitProvider(container appext.Container) (bufmodule.CommitProvider, er
146158
)
147159
}
148160

161+
// NewPluginDataProvider returns a new PluginDataProvider while creating the
162+
// required cache directories.
163+
func NewPluginDataProvider(container appext.Container) (bufplugin.PluginDataProvider, error) {
164+
clientConfig, err := NewConnectClientConfig(container)
165+
if err != nil {
166+
return nil, err
167+
}
168+
return newPluginDataProvider(
169+
container,
170+
bufregistryapiplugin.NewClientProvider(
171+
clientConfig,
172+
),
173+
)
174+
}
175+
149176
// CreateWasmRuntimeCacheDir creates the cache directory for the Wasm runtime.
150177
//
151178
// This is used by the Wasm runtime to cache compiled Wasm plugins. This is an
@@ -241,6 +268,33 @@ func newCommitProvider(
241268
), nil
242269
}
243270

271+
func newPluginDataProvider(
272+
container appext.Container,
273+
pluginClientProvider bufregistryapiplugin.ClientProvider,
274+
) (bufplugin.PluginDataProvider, error) {
275+
if err := createCacheDir(container.CacheDirPath(), v3CachePluginRelDirPath); err != nil {
276+
return nil, err
277+
}
278+
fullCacheDirPath := normalpath.Join(container.CacheDirPath(), v3CachePluginRelDirPath)
279+
storageosProvider := storageos.NewProvider() // No symlinks.
280+
cacheBucket, err := storageosProvider.NewReadWriteBucket(fullCacheDirPath)
281+
if err != nil {
282+
return nil, err
283+
}
284+
delegateModuleDataProvider := bufpluginapi.NewPluingDataProvider(
285+
container.Logger(),
286+
pluginClientProvider,
287+
)
288+
return bufplugincache.NewPluginDataProvider(
289+
container.Logger(),
290+
delegateModuleDataProvider,
291+
bufpluginstore.NewPluginDataStore(
292+
container.Logger(),
293+
cacheBucket,
294+
),
295+
), nil
296+
}
297+
244298
func createCacheDir(baseCacheDirPath string, relDirPath string) error {
245299
baseCacheDirPath = normalpath.Unnormalize(baseCacheDirPath)
246300
relDirPath = normalpath.Unnormalize(relDirPath)

private/buf/bufcli/controller.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ package bufcli
1717
import (
1818
"github.com/bufbuild/buf/private/buf/bufctl"
1919
"github.com/bufbuild/buf/private/bufpkg/bufmodule/bufmoduleapi"
20+
"github.com/bufbuild/buf/private/bufpkg/bufplugin/bufpluginapi"
2021
"github.com/bufbuild/buf/private/bufpkg/bufregistryapi/bufregistryapimodule"
2122
"github.com/bufbuild/buf/private/bufpkg/bufregistryapi/bufregistryapiowner"
23+
"github.com/bufbuild/buf/private/bufpkg/bufregistryapi/bufregistryapiplugin"
2224
"github.com/bufbuild/buf/private/pkg/app/appext"
2325
)
2426

@@ -39,6 +41,7 @@ func NewController(
3941
}
4042
moduleClientProvider := bufregistryapimodule.NewClientProvider(clientConfig)
4143
ownerClientProvider := bufregistryapiowner.NewClientProvider(clientConfig)
44+
pluginClientProvider := bufregistryapiplugin.NewClientProvider(clientConfig)
4245
moduleDataProvider, err := newModuleDataProvider(container, moduleClientProvider, ownerClientProvider)
4346
if err != nil {
4447
return nil, err
@@ -47,6 +50,10 @@ func NewController(
4750
if err != nil {
4851
return nil, err
4952
}
53+
pluginDataProvider, err := newPluginDataProvider(container, pluginClientProvider)
54+
if err != nil {
55+
return nil, err
56+
}
5057
wktStore, err := NewWKTStore(container)
5158
if err != nil {
5259
return nil, err
@@ -58,6 +65,8 @@ func NewController(
5865
bufmoduleapi.NewModuleKeyProvider(container.Logger(), moduleClientProvider),
5966
moduleDataProvider,
6067
commitProvider,
68+
bufpluginapi.NewPluginKeyProvider(container.Logger(), pluginClientProvider),
69+
pluginDataProvider,
6170
wktStore,
6271
// TODO FUTURE: Delete defaultHTTPClient and use the one from newConfig
6372
defaultHTTPClient,
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright 2020-2024 Buf Technologies, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package bufcli
16+
17+
import (
18+
"github.com/bufbuild/buf/private/bufpkg/bufplugin"
19+
"github.com/bufbuild/buf/private/bufpkg/bufplugin/bufpluginapi"
20+
"github.com/bufbuild/buf/private/bufpkg/bufregistryapi/bufregistryapiplugin"
21+
"github.com/bufbuild/buf/private/pkg/app/appext"
22+
)
23+
24+
// NewPluginKeyProvider returns a new PluginKeyProvider.
25+
func NewPluginKeyProvider(container appext.Container) (bufplugin.PluginKeyProvider, error) {
26+
clientConfig, err := NewConnectClientConfig(container)
27+
if err != nil {
28+
return nil, err
29+
}
30+
return bufpluginapi.NewPluginKeyProvider(
31+
container.Logger(),
32+
bufregistryapiplugin.NewClientProvider(
33+
clientConfig,
34+
),
35+
), nil
36+
}

private/buf/bufctl/controller.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
"github.com/bufbuild/buf/private/bufpkg/bufimage/bufimageutil"
3535
"github.com/bufbuild/buf/private/bufpkg/bufmodule"
3636
"github.com/bufbuild/buf/private/bufpkg/bufparse"
37+
"github.com/bufbuild/buf/private/bufpkg/bufplugin"
3738
"github.com/bufbuild/buf/private/bufpkg/bufreflect"
3839
"github.com/bufbuild/buf/private/gen/data/datawkt"
3940
imagev1 "github.com/bufbuild/buf/private/gen/proto/go/buf/alpha/image/v1"
@@ -136,6 +137,8 @@ func NewController(
136137
moduleKeyProvider bufmodule.ModuleKeyProvider,
137138
moduleDataProvider bufmodule.ModuleDataProvider,
138139
commitProvider bufmodule.CommitProvider,
140+
pluginKeyProvider bufplugin.PluginKeyProvider,
141+
pluginDataProvider bufplugin.PluginDataProvider,
139142
wktStore bufwktstore.Store,
140143
httpClient *http.Client,
141144
httpauthAuthenticator httpauth.Authenticator,
@@ -149,6 +152,8 @@ func NewController(
149152
moduleKeyProvider,
150153
moduleDataProvider,
151154
commitProvider,
155+
pluginKeyProvider,
156+
pluginDataProvider,
152157
wktStore,
153158
httpClient,
154159
httpauthAuthenticator,
@@ -170,6 +175,8 @@ type controller struct {
170175
moduleDataProvider bufmodule.ModuleDataProvider
171176
graphProvider bufmodule.GraphProvider
172177
commitProvider bufmodule.CommitProvider
178+
pluginKeyProvider bufplugin.PluginKeyProvider
179+
pluginDataProvider bufplugin.PluginDataProvider
173180
wktStore bufwktstore.Store
174181

175182
disableSymlinks bool
@@ -192,6 +199,8 @@ func newController(
192199
moduleKeyProvider bufmodule.ModuleKeyProvider,
193200
moduleDataProvider bufmodule.ModuleDataProvider,
194201
commitProvider bufmodule.CommitProvider,
202+
pluginKeyProvider bufplugin.PluginKeyProvider,
203+
pluginDataProvider bufplugin.PluginDataProvider,
195204
wktStore bufwktstore.Store,
196205
httpClient *http.Client,
197206
httpauthAuthenticator httpauth.Authenticator,
@@ -204,6 +213,8 @@ func newController(
204213
graphProvider: graphProvider,
205214
moduleDataProvider: moduleDataProvider,
206215
commitProvider: commitProvider,
216+
pluginKeyProvider: pluginKeyProvider,
217+
pluginDataProvider: pluginDataProvider,
207218
wktStore: wktStore,
208219
}
209220
for _, option := range options {

private/buf/bufmigrate/migrator.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,7 @@ func (m *migrator) buildBufYAMLAndBufLockFiles(
400400
bufLock, err = bufconfig.NewBufLockFile(
401401
bufconfig.FileVersionV2,
402402
resolvedLockEntries,
403+
nil,
403404
)
404405
if err != nil {
405406
return nil, nil, err
@@ -444,6 +445,7 @@ func (m *migrator) buildBufYAMLAndBufLockFiles(
444445
bufLock, err = bufconfig.NewBufLockFile(
445446
bufconfig.FileVersionV2,
446447
resolvedDepModuleKeys,
448+
nil, // Plugins are not supported in v1.
447449
)
448450
if err != nil {
449451
return nil, nil, err

0 commit comments

Comments
 (0)