Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add buf plugin prune command #3523

Merged
merged 6 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
determinisitic.
- Add `buf plugin push` command to push a plugin to the Buf Schema Registry.
Only WebAssembly check plugins are supported at this time.
- Add `buf plugin update` and `buf plugin prune` command to manage plugins in the `buf.lock`
file. Only WebAssembly check plugins are supported at this time.
- Add `buf registry plugin commit {add-label,info,list,resolve}` to manage BSR plugin commits.
- Add `buf registry plugin label {archive,info,list,unarchive}` to manage BSR plugin commits.

Expand Down
2 changes: 2 additions & 0 deletions private/buf/cmd/buf/buf.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ import (
"github.com/bufbuild/buf/private/buf/cmd/buf/command/mod/modlsbreakingrules"
"github.com/bufbuild/buf/private/buf/cmd/buf/command/mod/modlslintrules"
"github.com/bufbuild/buf/private/buf/cmd/buf/command/mod/modopen"
"github.com/bufbuild/buf/private/buf/cmd/buf/command/plugin/pluginprune"
"github.com/bufbuild/buf/private/buf/cmd/buf/command/plugin/pluginpush"
"github.com/bufbuild/buf/private/buf/cmd/buf/command/plugin/pluginupdate"
"github.com/bufbuild/buf/private/buf/cmd/buf/command/push"
Expand Down Expand Up @@ -191,6 +192,7 @@ func NewRootCommand(name string) *appcmd.Command {
SubCommands: []*appcmd.Command{
pluginpush.NewCommand("push", builder),
pluginupdate.NewCommand("update", builder),
pluginprune.NewCommand("prune", builder),
},
},
{
Expand Down
117 changes: 117 additions & 0 deletions private/buf/cmd/buf/command/plugin/pluginprune/pluginprune.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// Copyright 2020-2024 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 pluginprune

import (
"context"

"github.com/bufbuild/buf/private/buf/bufcli"
"github.com/bufbuild/buf/private/buf/bufworkspace"
"github.com/bufbuild/buf/private/bufpkg/bufplugin"
"github.com/bufbuild/buf/private/pkg/app/appcmd"
"github.com/bufbuild/buf/private/pkg/app/appext"
"github.com/bufbuild/buf/private/pkg/slicesext"
)

// NewCommand returns a new Command.
func NewCommand(
name string,
builder appext.SubCommandBuilder,
) *appcmd.Command {
return &appcmd.Command{
Use: name + " <directory>",
Short: "Prune unused plugins from buf.lock",
Long: `Plugins that are no longer configured in buf.yaml are removed from the buf.lock file.

The first argument is the directory of your buf.yaml configuration file.
Defaults to "." if no argument is specified.`,
Args: appcmd.MaximumNArgs(1),
Run: builder.NewRunFunc(
func(ctx context.Context, container appext.Container) error {
return run(ctx, container)
},
),
}
}

func run(
ctx context.Context,
container appext.Container,
) error {
dirPath := "."
if container.NumArgs() > 0 {
dirPath = container.Arg(0)
}
controller, err := bufcli.NewController(container)
if err != nil {
return err
}
workspaceDepManager, err := controller.GetWorkspaceDepManager(ctx, dirPath)
if err != nil {
return err
}
configuredRemotePluginRefs, err := workspaceDepManager.ConfiguredRemotePluginRefs(ctx)
if err != nil {
return err
}
pluginKeyProvider, err := bufcli.NewPluginKeyProvider(container)
if err != nil {
return err
}
configuredRemotePluginKeys, err := pluginKeyProvider.GetPluginKeysForPluginRefs(
ctx,
configuredRemotePluginRefs,
bufplugin.DigestTypeP1,
)
if err != nil {
return err
}
emcfarlane marked this conversation as resolved.
Show resolved Hide resolved
return prune(
ctx,
slicesext.Map(
configuredRemotePluginKeys,
func(pluginKey bufplugin.PluginKey) string {
return pluginKey.String()
},
),
workspaceDepManager,
)
}

func prune(
ctx context.Context,
bufYAMLBasedRemotePluginKeys []string,
workspaceDepManager bufworkspace.WorkspaceDepManager,
) error {
bufYAMLPluginKeys := slicesext.ToStructMap(bufYAMLBasedRemotePluginKeys)
existingRemotePluginKeys, err := workspaceDepManager.ExistingBufLockFileRemotePluginKeys(ctx)
if err != nil {
return err
}
var updatedBufLockPluginKeys []bufplugin.PluginKey
for _, existingRemotePluginKey := range existingRemotePluginKeys {
// Check if an existing plugin key from the buf.lock is confiugred in the buf.yaml.
if _, ok := bufYAMLPluginKeys[existingRemotePluginKey.String()]; ok {
// If yes, then we keep it for the updated buf.lock.
updatedBufLockPluginKeys = append(updatedBufLockPluginKeys, existingRemotePluginKey)
}
}
// We keep the existing dep module keys as-is.
existingDepModuleKeys, err := workspaceDepManager.ExistingBufLockFileDepModuleKeys(ctx)
if err != nil {
return err
}
return workspaceDepManager.UpdateBufLockFile(ctx, existingDepModuleKeys, updatedBufLockPluginKeys)
}
19 changes: 19 additions & 0 deletions private/buf/cmd/buf/command/plugin/pluginprune/usage.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 28 additions & 1 deletion private/buf/cmd/buf/command/plugin/pluginupdate/pluginupdate.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ import (
"context"
"errors"
"fmt"
"sort"

"github.com/bufbuild/buf/private/buf/bufcli"
"github.com/bufbuild/buf/private/bufpkg/bufplugin"
"github.com/bufbuild/buf/private/pkg/app/appcmd"
"github.com/bufbuild/buf/private/pkg/app/appext"
"github.com/bufbuild/buf/private/pkg/slicesext"
"github.com/bufbuild/buf/private/pkg/syserror"
"github.com/spf13/pflag"
)
Expand Down Expand Up @@ -131,6 +133,31 @@ func run(
if err != nil {
return err
}
// We do not prune the buf.lock during update, we only add update existing plugin keys
// or add new plugin keys.
emcfarlane marked this conversation as resolved.
Show resolved Hide resolved
configuredRemotePluginKeyNameToPluginKey := map[string]bufplugin.PluginKey{}
for _, configuredRemotePluginKey := range configuredRemotePluginKeys {
configuredRemotePluginKeyNameToPluginKey[configuredRemotePluginKey.FullName().String()] = configuredRemotePluginKey
}
var updatedRemotePluginKeys []bufplugin.PluginKey
for _, existingRemotePluginKey := range existingRemotePluginKeys {
if updatedRemotePluginKey, ok := configuredRemotePluginKeyNameToPluginKey[existingRemotePluginKey.FullName().String()]; ok {
updatedRemotePluginKeys = append(updatedRemotePluginKeys, updatedRemotePluginKey)
// This was found and updated, so we can delete from the map
delete(configuredRemotePluginKeyNameToPluginKey, existingRemotePluginKey.FullName().String())
} else {
// Otherwise, keep the existing remote plugin key from buf.lock
updatedRemotePluginKeys = append(updatedRemotePluginKeys, existingRemotePluginKey)
}
}
// Add any new configured remote plugin keys
updatedRemotePluginKeys = append(updatedRemotePluginKeys, slicesext.MapValuesToSlice(configuredRemotePluginKeyNameToPluginKey)...)
sort.Slice(
updatedRemotePluginKeys,
func(i int, j int) bool {
return updatedRemotePluginKeys[i].FullName().String() < updatedRemotePluginKeys[j].FullName().String()
},
)

// We're about to edit the buf.lock file on disk. If we have a subsequent error,
// attempt to revert the buf.lock file.
Expand All @@ -144,7 +171,7 @@ func run(
}
}()
// Edit the buf.lock file with the updated remote plugins.
if err := workspaceDepManager.UpdateBufLockFile(ctx, existingDepModuleKeys, configuredRemotePluginKeys); err != nil {
if err := workspaceDepManager.UpdateBufLockFile(ctx, existingDepModuleKeys, updatedRemotePluginKeys); err != nil {
return err
}
return nil
Expand Down
Loading