Skip to content

Commit

Permalink
Add plugin delete command (#1711)
Browse files Browse the repository at this point in the history
  • Loading branch information
mfridman committed Jan 4, 2023
1 parent 2f0804d commit 3343502
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 7 deletions.
2 changes: 2 additions & 0 deletions private/buf/cmd/buf/buf.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"time"

"github.com/bufbuild/buf/private/buf/bufcli"
curatedplugindelete "github.com/bufbuild/buf/private/buf/cmd/buf/command/alpha/plugin/plugindelete"
"github.com/bufbuild/buf/private/buf/cmd/buf/command/alpha/plugin/pluginpush"
"github.com/bufbuild/buf/private/buf/cmd/buf/command/alpha/protoc"
"github.com/bufbuild/buf/private/buf/cmd/buf/command/alpha/registry/token/tokencreate"
Expand Down Expand Up @@ -273,6 +274,7 @@ func NewRootCommand(name string) *appcmd.Command {
Short: "Manage plugins on the Buf Schema Registry.",
SubCommands: []*appcmd.Command{
pluginpush.NewCommand("push", builder),
curatedplugindelete.NewCommand("delete", builder),
},
},
},
Expand Down
101 changes: 101 additions & 0 deletions private/buf/cmd/buf/command/alpha/plugin/plugindelete/plugindelete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// Copyright 2020-2022 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 plugindelete

import (
"context"
"fmt"
"strings"

"github.com/bufbuild/buf/private/buf/bufcli"
"github.com/bufbuild/buf/private/bufpkg/bufplugin/bufpluginref"
"github.com/bufbuild/buf/private/gen/proto/connect/buf/alpha/registry/v1alpha1/registryv1alpha1connect"
registryv1alpha1 "github.com/bufbuild/buf/private/gen/proto/go/buf/alpha/registry/v1alpha1"
"github.com/bufbuild/buf/private/pkg/app/appcmd"
"github.com/bufbuild/buf/private/pkg/app/appflag"
"github.com/bufbuild/buf/private/pkg/connectclient"
"github.com/bufbuild/connect-go"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)

// NewCommand returns a new Command.
func NewCommand(
name string,
builder appflag.Builder,
) *appcmd.Command {
flags := newFlags()
return &appcmd.Command{
Use: name + " <buf.build/owner/plugin[:version]>",
Short: "Delete a plugin from the registry.",
Args: cobra.ExactArgs(1),
Run: builder.NewRunFunc(
func(ctx context.Context, container appflag.Container) error {
return run(ctx, container, flags)
},
bufcli.NewErrorInterceptor(),
),
BindFlags: flags.Bind,
}
}

type flags struct{}

func newFlags() *flags {
return &flags{}
}

func (f *flags) Bind(flagSet *pflag.FlagSet) {}

func run(
ctx context.Context,
container appflag.Container,
flags *flags,
) error {
bufcli.WarnAlphaCommand(ctx, container)
identity, version, _ := strings.Cut(container.Arg(0), ":")
pluginIdentity, err := bufpluginref.PluginIdentityForString(identity)
if err != nil {
return appcmd.NewInvalidArgumentError(err.Error())
}
if version != "" {
if err := bufpluginref.ValidatePluginVersion(version); err != nil {
return appcmd.NewInvalidArgumentError(err.Error())
}
}
clientConfig, err := bufcli.NewConnectClientConfig(container)
if err != nil {
return err
}
service := connectclient.Make(
clientConfig,
pluginIdentity.Remote(),
registryv1alpha1connect.NewPluginCurationServiceClient,
)
if _, err := service.DeleteCuratedPlugin(
ctx,
connect.NewRequest(&registryv1alpha1.DeleteCuratedPluginRequest{
Owner: pluginIdentity.Owner(),
Name: pluginIdentity.Plugin(),
Version: version,
}),
); err != nil {
if connect.CodeOf(err) == connect.CodeNotFound {
return fmt.Errorf("the plugin %s does not exist", container.Arg(0))
}
return err
}
return nil
}
19 changes: 19 additions & 0 deletions private/buf/cmd/buf/command/alpha/plugin/plugindelete/usage.gen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2020-2022 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.

// Generated. DO NOT EDIT.

package plugindelete

import _ "github.com/bufbuild/buf/private/usage"
8 changes: 4 additions & 4 deletions private/bufpkg/bufplugin/bufpluginref/plugin_identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func newPluginIdentity(
owner: owner,
plugin: plugin,
}
if err := validatePluginIdentity(pluginIdentity); err != nil {
if err := ValidatePluginIdentity(pluginIdentity); err != nil {
return nil, err
}
return pluginIdentity, nil
Expand All @@ -61,11 +61,11 @@ func (m *pluginIdentity) IdentityString() string {

func (*pluginIdentity) isPluginIdentity() {}

func validatePluginIdentity(pluginIdentity PluginIdentity) error {
func ValidatePluginIdentity(pluginIdentity PluginIdentity) error {
if pluginIdentity == nil {
return errors.New("plugin identity is required")
}
if err := validateRemote(pluginIdentity.Remote()); err != nil {
if err := ValidateRemote(pluginIdentity.Remote()); err != nil {
return err
}
if pluginIdentity.Owner() == "" {
Expand All @@ -77,7 +77,7 @@ func validatePluginIdentity(pluginIdentity PluginIdentity) error {
return nil
}

func validateRemote(remote string) error {
func ValidateRemote(remote string) error {
if remote == "" {
return errors.New("remote name is required")
}
Expand Down
6 changes: 3 additions & 3 deletions private/bufpkg/bufplugin/bufpluginref/plugin_reference.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ func (p *pluginReference) Revision() int {
func (p *pluginReference) isPluginReference() {}

func newPluginReference(identity PluginIdentity, version string, revision int) (*pluginReference, error) {
if err := validatePluginIdentity(identity); err != nil {
if err := ValidatePluginIdentity(identity); err != nil {
return nil, err
}
if err := validatePluginVersion(version); err != nil {
if err := ValidatePluginVersion(version); err != nil {
return nil, err
}
if err := validatePluginRevision(revision); err != nil {
Expand All @@ -77,7 +77,7 @@ func newPluginReference(identity PluginIdentity, version string, revision int) (
}, nil
}

func validatePluginVersion(version string) error {
func ValidatePluginVersion(version string) error {
if !semver.IsValid(version) {
return fmt.Errorf("plugin version %q is not a valid semantic version", version)
}
Expand Down

0 comments on commit 3343502

Please sign in to comment.