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 registry plugin label commands #3499

Merged
merged 4 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- 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 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.

## [v1.47.2] - 2024-11-14

Expand Down
18 changes: 16 additions & 2 deletions private/buf/bufcli/flags_args.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,8 +318,8 @@ func VisibilityFlagToPluginVisibilityAllowUnspecified(visibility string) (plugin
}
}

// ArchiveStatusFlagToArchiveStatusFilter parses the given string as a modulev1.ListLabelsRequest_ArchiveFilter.
func ArchiveStatusFlagToArchiveStatusFilter(archiveStatus string) (modulev1.ListLabelsRequest_ArchiveFilter, error) {
// ArchiveStatusFlagToModuleArchiveStatusFilter parses the given string as a modulev1.ListLabelsRequest_ArchiveFilter.
func ArchiveStatusFlagToModuleArchiveStatusFilter(archiveStatus string) (modulev1.ListLabelsRequest_ArchiveFilter, error) {
switch archiveStatus {
case archivedArchiveStatus:
return modulev1.ListLabelsRequest_ARCHIVE_FILTER_ARCHIVED_ONLY, nil
Expand All @@ -332,6 +332,20 @@ func ArchiveStatusFlagToArchiveStatusFilter(archiveStatus string) (modulev1.List
}
}

// ArchiveStatusFlagToPluginArchiveStatusFilter parses the given string as a pluginv1beta1.ListLabelsRequest_ArchiveFilter.
func ArchiveStatusFlagToPluginArchiveStatusFilter(archiveStatus string) (pluginv1beta1.ListLabelsRequest_ArchiveFilter, error) {
switch archiveStatus {
case archivedArchiveStatus:
return pluginv1beta1.ListLabelsRequest_ARCHIVE_FILTER_ARCHIVED_ONLY, nil
case unarchivedArchiveStatus:
return pluginv1beta1.ListLabelsRequest_ARCHIVE_FILTER_UNARCHIVED_ONLY, nil
case allArchiveStatus:
return pluginv1beta1.ListLabelsRequest_ARCHIVE_FILTER_ALL, nil
default:
return 0, fmt.Errorf("invalid archive status: %s", archiveStatus)
}
}

// ValidateRequiredFlag validates that the required flag is set.
func ValidateRequiredFlag[T comparable](flagName string, value T) error {
var zero T
Expand Down
14 changes: 14 additions & 0 deletions private/buf/cmd/buf/buf.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ import (
"github.com/bufbuild/buf/private/buf/cmd/buf/command/registry/plugin/plugincreate"
"github.com/bufbuild/buf/private/buf/cmd/buf/command/registry/plugin/plugindelete"
"github.com/bufbuild/buf/private/buf/cmd/buf/command/registry/plugin/plugininfo"
"github.com/bufbuild/buf/private/buf/cmd/buf/command/registry/plugin/pluginlabel/pluginlabelarchive"
"github.com/bufbuild/buf/private/buf/cmd/buf/command/registry/plugin/pluginlabel/pluginlabelinfo"
"github.com/bufbuild/buf/private/buf/cmd/buf/command/registry/plugin/pluginlabel/pluginlabellist"
"github.com/bufbuild/buf/private/buf/cmd/buf/command/registry/plugin/pluginlabel/pluginlabelunarchive"
"github.com/bufbuild/buf/private/buf/cmd/buf/command/registry/plugin/pluginupdate"
"github.com/bufbuild/buf/private/buf/cmd/buf/command/registry/registrycc"
"github.com/bufbuild/buf/private/buf/cmd/buf/command/registry/registrylogin"
Expand Down Expand Up @@ -282,6 +286,16 @@ func NewRootCommand(name string) *appcmd.Command {
plugincommitresolve.NewCommand("resolve", builder, ""),
},
},
{
Use: "label",
Short: "Manage a plugin's labels",
SubCommands: []*appcmd.Command{
pluginlabelarchive.NewCommand("archive", builder, ""),
pluginlabelinfo.NewCommand("info", builder, ""),
pluginlabellist.NewCommand("list", builder, ""),
pluginlabelunarchive.NewCommand("unarchive", builder, ""),
},
},
plugincreate.NewCommand("create", builder),
plugininfo.NewCommand("info", builder),
plugindelete.NewCommand("delete", builder),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func run(
if err != nil {
return appcmd.WrapInvalidArgumentError(err)
}
archiveStatusFitler, err := bufcli.ArchiveStatusFlagToArchiveStatusFilter(flags.ArchiveStatus)
archiveStatusFitler, err := bufcli.ArchiveStatusFlagToModuleArchiveStatusFilter(flags.ArchiveStatus)
if err != nil {
return appcmd.WrapInvalidArgumentError(err)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// 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 pluginlabelarchive

import (
"context"
"fmt"

pluginv1beta1 "buf.build/gen/go/bufbuild/registry/protocolbuffers/go/buf/registry/plugin/v1beta1"
"connectrpc.com/connect"
"github.com/bufbuild/buf/private/buf/bufcli"
"github.com/bufbuild/buf/private/bufpkg/bufparse"
"github.com/bufbuild/buf/private/bufpkg/bufregistryapi/bufregistryapiplugin"
"github.com/bufbuild/buf/private/pkg/app/appcmd"
"github.com/bufbuild/buf/private/pkg/app/appext"
"github.com/spf13/pflag"
)

// NewCommand returns a new Command.
func NewCommand(
name string,
builder appext.SubCommandBuilder,
deprecated string,
) *appcmd.Command {
flags := newFlags()
return &appcmd.Command{
Use: name + " <remote/owner/plugin:label>",
Short: "Archive a plugin label",
Args: appcmd.ExactArgs(1),
Deprecated: deprecated,
Run: builder.NewRunFunc(
func(ctx context.Context, container appext.Container) error {
return run(ctx, container, flags)
},
),
BindFlags: flags.Bind,
}
}

type flags struct {
}

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

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

func run(
ctx context.Context,
container appext.Container,
_ *flags,
) error {
pluginRef, err := bufparse.ParseRef(container.Arg(0))
if err != nil {
return appcmd.WrapInvalidArgumentError(err)
}
labelName := pluginRef.Ref()
if labelName == "" {
return appcmd.NewInvalidArgumentError("label is required")
}
clientConfig, err := bufcli.NewConnectClientConfig(container)
if err != nil {
return err
}
pluginFullName := pluginRef.FullName()
labelServiceClient := bufregistryapiplugin.NewClientProvider(clientConfig).V1Beta1LabelServiceClient(pluginFullName.Registry())
// ArchiveLabelsResponse is empty.
if _, err := labelServiceClient.ArchiveLabels(
ctx,
connect.NewRequest(
&pluginv1beta1.ArchiveLabelsRequest{
LabelRefs: []*pluginv1beta1.LabelRef{
{
Value: &pluginv1beta1.LabelRef_Name_{
Name: &pluginv1beta1.LabelRef_Name{
Owner: pluginFullName.Owner(),
Plugin: pluginFullName.Name(),
Label: labelName,
},
},
},
},
},
),
); err != nil {
if connect.CodeOf(err) == connect.CodeNotFound {
return bufcli.NewLabelNotFoundError(pluginRef)
}
return err
}
_, err = fmt.Fprintf(container.Stdout(), "Archived %s.\n", pluginRef)
return err
}

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
// 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 pluginlabelinfo

import (
"context"
"fmt"

pluginv1beta1 "buf.build/gen/go/bufbuild/registry/protocolbuffers/go/buf/registry/plugin/v1beta1"
"connectrpc.com/connect"
"github.com/bufbuild/buf/private/buf/bufcli"
"github.com/bufbuild/buf/private/buf/bufprint"
"github.com/bufbuild/buf/private/bufpkg/bufparse"
"github.com/bufbuild/buf/private/bufpkg/bufregistryapi/bufregistryapiplugin"
"github.com/bufbuild/buf/private/pkg/app/appcmd"
"github.com/bufbuild/buf/private/pkg/app/appext"
"github.com/bufbuild/buf/private/pkg/syserror"
"github.com/spf13/pflag"
)

const formatFlagName = "format"

// NewCommand returns a new Command
func NewCommand(
name string,
builder appext.SubCommandBuilder,
deprecated string,
) *appcmd.Command {
flags := newFlags()
return &appcmd.Command{
Use: name + " <remote/owner/plugin:label>",
Short: "Show label information",
Args: appcmd.ExactArgs(1),
Deprecated: deprecated,
Run: builder.NewRunFunc(
func(ctx context.Context, container appext.Container) error {
return run(ctx, container, flags)
},
),
BindFlags: flags.Bind,
}
}

type flags struct {
Format string
}

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

func (f *flags) Bind(flagSet *pflag.FlagSet) {
flagSet.StringVar(
&f.Format,
formatFlagName,
bufprint.FormatText.String(),
fmt.Sprintf(`The output format to use. Must be one of %s`, bufprint.AllFormatsString),
)
}

func run(
ctx context.Context,
container appext.Container,
flags *flags,
) error {
pluginRef, err := bufparse.ParseRef(container.Arg(0))
if err != nil {
return appcmd.WrapInvalidArgumentError(err)
}
labelName := pluginRef.Ref()
if labelName == "" {
return appcmd.NewInvalidArgumentError("label is required")
}
format, err := bufprint.ParseFormat(flags.Format)
if err != nil {
return appcmd.WrapInvalidArgumentError(err)
}
clientConfig, err := bufcli.NewConnectClientConfig(container)
if err != nil {
return err
}
pluginClientProvider := bufregistryapiplugin.NewClientProvider(clientConfig)
pluginFullName := pluginRef.FullName()
labelServiceClient := pluginClientProvider.V1Beta1LabelServiceClient(pluginFullName.Registry())
resp, err := labelServiceClient.GetLabels(
ctx,
connect.NewRequest(
&pluginv1beta1.GetLabelsRequest{
LabelRefs: []*pluginv1beta1.LabelRef{
{
Value: &pluginv1beta1.LabelRef_Name_{
Name: &pluginv1beta1.LabelRef_Name{
Owner: pluginFullName.Owner(),
Plugin: pluginFullName.Name(),
Label: labelName,
},
},
},
},
},
),
)
if err != nil {
if connect.CodeOf(err) == connect.CodeNotFound {
return bufcli.NewLabelNotFoundError(pluginRef)
}
return err
}
labels := resp.Msg.Labels
if len(labels) != 1 {
return syserror.Newf("expect 1 label from response, got %d", len(labels))
}
return bufprint.PrintEntity(
container.Stdout(),
format,
bufprint.NewLabelEntity(labels[0], pluginFullName),
)
}

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

Loading
Loading