|
| 1 | +// Copyright 2020-2025 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 policyprune |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + |
| 20 | + "buf.build/go/app/appcmd" |
| 21 | + "buf.build/go/app/appext" |
| 22 | + "buf.build/go/standard/xslices" |
| 23 | + "github.com/bufbuild/buf/private/buf/bufcli" |
| 24 | + "github.com/bufbuild/buf/private/buf/bufworkspace" |
| 25 | + "github.com/bufbuild/buf/private/bufpkg/bufparse" |
| 26 | + "github.com/bufbuild/buf/private/bufpkg/bufplugin" |
| 27 | + "github.com/bufbuild/buf/private/bufpkg/bufpolicy" |
| 28 | +) |
| 29 | + |
| 30 | +// NewCommand returns a new Command. |
| 31 | +func NewCommand( |
| 32 | + name string, |
| 33 | + builder appext.SubCommandBuilder, |
| 34 | +) *appcmd.Command { |
| 35 | + return &appcmd.Command{ |
| 36 | + Use: name + " <directory>", |
| 37 | + Short: "Prune unused policies from buf.lock", |
| 38 | + Long: `Policies that are no longer configured in buf.yaml are removed from the buf.lock file. |
| 39 | +
|
| 40 | +The first argument is the directory of your buf.yaml configuration file. |
| 41 | +Defaults to "." if no argument is specified.`, |
| 42 | + Args: appcmd.MaximumNArgs(1), |
| 43 | + Run: builder.NewRunFunc( |
| 44 | + func(ctx context.Context, container appext.Container) error { |
| 45 | + return run(ctx, container) |
| 46 | + }, |
| 47 | + ), |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +func run( |
| 52 | + ctx context.Context, |
| 53 | + container appext.Container, |
| 54 | +) error { |
| 55 | + dirPath := "." |
| 56 | + if container.NumArgs() > 0 { |
| 57 | + dirPath = container.Arg(0) |
| 58 | + } |
| 59 | + controller, err := bufcli.NewController(container) |
| 60 | + if err != nil { |
| 61 | + return err |
| 62 | + } |
| 63 | + workspaceDepManager, err := controller.GetWorkspaceDepManager(ctx, dirPath) |
| 64 | + if err != nil { |
| 65 | + return err |
| 66 | + } |
| 67 | + configuredRemotePolicyRefs, err := workspaceDepManager.ConfiguredRemotePolicyRefs(ctx) |
| 68 | + if err != nil { |
| 69 | + return err |
| 70 | + } |
| 71 | + return prune( |
| 72 | + ctx, |
| 73 | + xslices.Map( |
| 74 | + configuredRemotePolicyRefs, |
| 75 | + func(policyRef bufparse.Ref) string { |
| 76 | + return policyRef.FullName().String() |
| 77 | + }, |
| 78 | + ), |
| 79 | + workspaceDepManager, |
| 80 | + ) |
| 81 | +} |
| 82 | + |
| 83 | +func prune( |
| 84 | + ctx context.Context, |
| 85 | + bufYAMLBasedRemotePolicyNames []string, |
| 86 | + workspaceDepManager bufworkspace.WorkspaceDepManager, |
| 87 | +) error { |
| 88 | + bufYAMLRemotePolicyNames := xslices.ToStructMap(bufYAMLBasedRemotePolicyNames) |
| 89 | + existingRemotePolicyKeys, err := workspaceDepManager.ExistingBufLockFileRemotePolicyKeys(ctx) |
| 90 | + if err != nil { |
| 91 | + return err |
| 92 | + } |
| 93 | + existingPolicyNameToRemotePluginKeys, err := workspaceDepManager.ExistingBufLockFilePolicyNameToRemotePluginKeys(ctx) |
| 94 | + if err != nil { |
| 95 | + return err |
| 96 | + } |
| 97 | + var ( |
| 98 | + prunedBufLockPolicyKeys []bufpolicy.PolicyKey |
| 99 | + prunedBufLockPolicyNameToRemotePluginKeys = make(map[string][]bufplugin.PluginKey) |
| 100 | + ) |
| 101 | + for _, existingRemotePolicyKey := range existingRemotePolicyKeys { |
| 102 | + // Check if an existing policy key from the buf.lock is configured in the buf.yaml. |
| 103 | + policyFullNameString := existingRemotePolicyKey.FullName().String() |
| 104 | + if _, ok := bufYAMLRemotePolicyNames[policyFullNameString]; ok { |
| 105 | + // If yes, then we keep it for the updated buf.lock. |
| 106 | + prunedBufLockPolicyKeys = append(prunedBufLockPolicyKeys, existingRemotePolicyKey) |
| 107 | + existingRemotePluginKeys := existingPolicyNameToRemotePluginKeys[policyFullNameString] |
| 108 | + prunedBufLockPolicyNameToRemotePluginKeys[policyFullNameString] = existingRemotePluginKeys |
| 109 | + } |
| 110 | + } |
| 111 | + // We keep the existing dep module keys as-is. |
| 112 | + existingDepModuleKeys, err := workspaceDepManager.ExistingBufLockFileDepModuleKeys(ctx) |
| 113 | + if err != nil { |
| 114 | + return err |
| 115 | + } |
| 116 | + existingRemotePluginKeys, err := workspaceDepManager.ExistingBufLockFileRemotePluginKeys(ctx) |
| 117 | + if err != nil { |
| 118 | + return err |
| 119 | + } |
| 120 | + return workspaceDepManager.UpdateBufLockFile(ctx, existingDepModuleKeys, existingRemotePluginKeys, prunedBufLockPolicyKeys, prunedBufLockPolicyNameToRemotePluginKeys) |
| 121 | +} |
0 commit comments