Skip to content

Commit

Permalink
Update buf beta registry repository commands to use v1 API (#3030)
Browse files Browse the repository at this point in the history
Update `buf beta registry repository [create, delete, deprecate, get,
list, undeprecate, update]` to use v1 APIs (`ModuleService`).

The only behavioral change is updating the `RepositoryPrinter`'s headers
from `Full name` and `Created` to `Full Name` and `Create Time`.

Keep most of the variable names consistent with the existing code, when
it comes to `module` vs `repository`. However, the oddity comes from the
fact that most of these variables have type `modulev1.Module`. I'm happy
to do a follow-up if that's desired.
  • Loading branch information
oliversun9 authored Jun 10, 2024
1 parent ec93e69 commit 35a9087
Show file tree
Hide file tree
Showing 11 changed files with 181 additions and 136 deletions.
6 changes: 6 additions & 0 deletions private/buf/bufcli/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ func NewOrganizationNotFoundError(name string) error {
return fmt.Errorf(`an organization named %q does not exist, use "buf beta registry organization create" to create one`, name)
}

// NewOrganizationOrUserNotFoundError informs the user that an organization or user with
// that name does not exist.
func NewOrganizationOrUserNotFoundError(name string) error {
return fmt.Errorf(`an organization or user named %q does not exist`, name)
}

// NewRepositoryNotFoundError informs the user that a repository with
// that name does not exist.
func NewRepositoryNotFoundError(name string) error {
Expand Down
19 changes: 9 additions & 10 deletions private/buf/bufcli/flags_args.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
modulev1 "buf.build/gen/go/bufbuild/registry/protocolbuffers/go/buf/registry/module/v1"
"github.com/bufbuild/buf/private/buf/buffetch"
"github.com/bufbuild/buf/private/bufpkg/bufcheck/buflint"
registryv1alpha1 "github.com/bufbuild/buf/private/gen/proto/go/buf/alpha/registry/v1alpha1"
"github.com/bufbuild/buf/private/pkg/app"
"github.com/bufbuild/buf/private/pkg/app/appcmd"
"github.com/bufbuild/buf/private/pkg/stringutil"
Expand Down Expand Up @@ -246,28 +245,28 @@ func GetInputValue(
return defaultValue, nil
}

// VisibilityFlagToVisibility parses the given string as a registryv1alpha1.Visibility.
func VisibilityFlagToVisibility(visibility string) (registryv1alpha1.Visibility, error) {
// VisibilityFlagToVisibility parses the given string as a modulev1.ModuleVisibility
func VisibilityFlagToVisibility(visibility string) (modulev1.ModuleVisibility, error) {
switch visibility {
case publicVisibility:
return registryv1alpha1.Visibility_VISIBILITY_PUBLIC, nil
return modulev1.ModuleVisibility_MODULE_VISIBILITY_PUBLIC, nil
case privateVisibility:
return registryv1alpha1.Visibility_VISIBILITY_PRIVATE, nil
return modulev1.ModuleVisibility_MODULE_VISIBILITY_PRIVATE, nil
default:
return 0, fmt.Errorf("invalid visibility: %s, expected one of %s", visibility, stringutil.SliceToString(allVisibiltyStrings))
}
}

// VisibilityFlagToVisibilityAllowUnspecified parses the given string as a registryv1alpha1.Visibility,
// VisibilityFlagToVisibilityAllowUnspecified parses the given string as a modulev1.ModuleVisibility
// where an empty string will be parsed as unspecified
func VisibilityFlagToVisibilityAllowUnspecified(visibility string) (registryv1alpha1.Visibility, error) {
func VisibilityFlagToVisibilityAllowUnspecified(visibility string) (modulev1.ModuleVisibility, error) {
switch visibility {
case publicVisibility:
return registryv1alpha1.Visibility_VISIBILITY_PUBLIC, nil
return modulev1.ModuleVisibility_MODULE_VISIBILITY_PUBLIC, nil
case privateVisibility:
return registryv1alpha1.Visibility_VISIBILITY_PRIVATE, nil
return modulev1.ModuleVisibility_MODULE_VISIBILITY_PRIVATE, nil
case "":
return registryv1alpha1.Visibility_VISIBILITY_UNSPECIFIED, nil
return modulev1.ModuleVisibility_MODULE_VISIBILITY_UNSPECIFIED, nil
default:
return 0, fmt.Errorf("invalid visibility: %s", visibility)
}
Expand Down
4 changes: 2 additions & 2 deletions private/buf/bufprint/bufprint.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ func NewOrganizationPrinter(address string, writer io.Writer) OrganizationPrinte

// RepositoryPrinter is a repository printer.
type RepositoryPrinter interface {
PrintRepository(ctx context.Context, format Format, repository *registryv1alpha1.Repository) error
PrintRepositories(ctx context.Context, format Format, nextPageToken string, repositories ...*registryv1alpha1.Repository) error
PrintRepository(ctx context.Context, format Format, repository *modulev1.Module) error
PrintRepositories(ctx context.Context, format Format, nextPageToken string, repositories ...*modulev1.Module) error
}

// NewRepositoryPrinter returns a new RepositoryPrinter.
Expand Down
2 changes: 1 addition & 1 deletion private/buf/bufprint/organization_printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (p *organizationPrinter) printOrganizationsText(outputOrganizations []outpu
return WithTabWriter(
p.writer,
[]string{
"Full name",
"Full Name",
"Create Time",
},
func(tabWriter TabWriter) error {
Expand Down
86 changes: 46 additions & 40 deletions private/buf/bufprint/repository_printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@ import (
"io"
"time"

modulev1 "buf.build/gen/go/bufbuild/registry/protocolbuffers/go/buf/registry/module/v1"
ownerv1 "buf.build/gen/go/bufbuild/registry/protocolbuffers/go/buf/registry/owner/v1"
"connectrpc.com/connect"
"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/bufpkg/bufapi"
"github.com/bufbuild/buf/private/pkg/connectclient"
"github.com/bufbuild/buf/private/pkg/slicesext"
"github.com/bufbuild/buf/private/pkg/syserror"
)

type repositoryPrinter struct {
Expand All @@ -45,7 +48,7 @@ func newRepositoryPrinter(
}
}

func (p *repositoryPrinter) PrintRepository(ctx context.Context, format Format, message *registryv1alpha1.Repository) error {
func (p *repositoryPrinter) PrintRepository(ctx context.Context, format Format, message *modulev1.Module) error {
outputRepositories, err := p.registryRepositoriesToOutRepositories(ctx, message)
if err != nil {
return err
Expand All @@ -63,7 +66,7 @@ func (p *repositoryPrinter) PrintRepository(ctx context.Context, format Format,
}
}

func (p *repositoryPrinter) PrintRepositories(ctx context.Context, format Format, nextPageToken string, messages ...*registryv1alpha1.Repository) error {
func (p *repositoryPrinter) PrintRepositories(ctx context.Context, format Format, nextPageToken string, messages ...*modulev1.Module) error {
if len(messages) == 0 {
return nil
}
Expand All @@ -84,46 +87,49 @@ func (p *repositoryPrinter) PrintRepositories(ctx context.Context, format Format
}
}

func (p *repositoryPrinter) registryRepositoriesToOutRepositories(ctx context.Context, messages ...*registryv1alpha1.Repository) ([]outputRepository, error) {
var outputRepositories []outputRepository
for _, repository := range messages {
func (p *repositoryPrinter) registryRepositoriesToOutRepositories(ctx context.Context, messages ...*modulev1.Module) ([]outputRepository, error) {
ownerRefs := slicesext.Map(messages, func(module *modulev1.Module) *ownerv1.OwnerRef {
return &ownerv1.OwnerRef{
Value: &ownerv1.OwnerRef_Id{
Id: module.OwnerId,
},
}
})
ownerServiceClient := bufapi.NewClientProvider(p.clientConfig).V1OwnerServiceClient(p.address)
resp, err := ownerServiceClient.GetOwners(
ctx,
&connect.Request[ownerv1.GetOwnersRequest]{
Msg: &ownerv1.GetOwnersRequest{
OwnerRefs: ownerRefs,
},
},
)
if err != nil {
return nil, err
}
owners := resp.Msg.GetOwners()
if len(owners) != len(messages) {
return nil, syserror.Newf("expected %d owners from response, got %d", len(messages), len(owners))
}
outputRepositories := make([]outputRepository, len(messages))
for i, module := range messages {
var ownerName string
switch owner := repository.Owner.(type) {
case *registryv1alpha1.Repository_OrganizationId:
organizationService := connectclient.Make(p.clientConfig, p.address, registryv1alpha1connect.NewOrganizationServiceClient)
resp, err := organizationService.GetOrganization(
ctx,
connect.NewRequest(&registryv1alpha1.GetOrganizationRequest{
Id: owner.OrganizationId,
}),
)
if err != nil {
return nil, err
}
ownerName = resp.Msg.Organization.Name
case *registryv1alpha1.Repository_UserId:
userService := connectclient.Make(p.clientConfig, p.address, registryv1alpha1connect.NewUserServiceClient)
resp, err := userService.GetUser(
ctx,
connect.NewRequest(&registryv1alpha1.GetUserRequest{
Id: owner.UserId,
}),
)
if err != nil {
return nil, err
}
ownerName = resp.Msg.User.Username
owner := owners[i]
switch {
case owner.GetUser() != nil:
ownerName = owner.GetUser().Name
case owner.GetOrganization() != nil:
ownerName = owner.GetOrganization().Name
default:
return nil, fmt.Errorf("unknown owner: %T", owner)
return nil, syserror.Newf("owner with id %s is neither a user nor an organization", messages[i].GetOwnerId())
}
outputRepository := outputRepository{
ID: repository.Id,
outputRepositories[i] = outputRepository{
ID: module.GetId(),
Remote: p.address,
Owner: ownerName,
Name: repository.Name,
CreateTime: repository.CreateTime.AsTime(),
Name: module.GetName(),
CreateTime: module.GetCreateTime().AsTime(),
}
outputRepositories = append(outputRepositories, outputRepository)
}
return outputRepositories, nil
}
Expand All @@ -132,8 +138,8 @@ func (p *repositoryPrinter) printRepositoriesText(outputRepositories []outputRep
return WithTabWriter(
p.writer,
[]string{
"Full name",
"Created",
"Full Name",
"Create Time",
},
func(tabWriter TabWriter) error {
for _, outputRepository := range outputRepositories {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,16 @@ import (
"context"
"fmt"

modulev1 "buf.build/gen/go/bufbuild/registry/protocolbuffers/go/buf/registry/module/v1"
ownerv1 "buf.build/gen/go/bufbuild/registry/protocolbuffers/go/buf/registry/owner/v1"
"connectrpc.com/connect"
"github.com/bufbuild/buf/private/buf/bufcli"
"github.com/bufbuild/buf/private/buf/bufprint"
"github.com/bufbuild/buf/private/bufpkg/bufapi"
"github.com/bufbuild/buf/private/bufpkg/bufmodule"
"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/appext"
"github.com/bufbuild/buf/private/pkg/connectclient"
"github.com/bufbuild/buf/private/pkg/syserror"
"github.com/spf13/pflag"
)

Expand Down Expand Up @@ -97,30 +98,38 @@ func run(
if err != nil {
return err
}
service := connectclient.Make(
clientConfig,
moduleFullName.Registry(),
registryv1alpha1connect.NewRepositoryServiceClient,
)
resp, err := service.CreateRepositoryByFullName(
moduleServiceClient := bufapi.NewClientProvider(clientConfig).V1ModuleServiceClient(moduleFullName.Registry())
resp, err := moduleServiceClient.CreateModules(
ctx,
connect.NewRequest(
&registryv1alpha1.CreateRepositoryByFullNameRequest{
FullName: moduleFullName.Owner() + "/" + moduleFullName.Name(),
Visibility: visibility,
&modulev1.CreateModulesRequest{
Values: []*modulev1.CreateModulesRequest_Value{
{
OwnerRef: &ownerv1.OwnerRef{
Value: &ownerv1.OwnerRef_Name{
Name: moduleFullName.Owner(),
},
},
Name: moduleFullName.Name(),
Visibility: visibility,
},
},
},
),
)
if err != nil {
if connect.CodeOf(err) == connect.CodeAlreadyExists {
return bufcli.NewRepositoryNameAlreadyExistsError(container.Arg(0))
return bufcli.NewRepositoryNameAlreadyExistsError(moduleFullName.String())
}
return err
}
repository := resp.Msg.Repository
repositories := resp.Msg.Modules
if len(repositories) != 1 {
return syserror.Newf("unexpected nubmer of repositories returned from server: %d", len(repositories))
}
return bufprint.NewRepositoryPrinter(
clientConfig,
moduleFullName.Registry(),
container.Stdout(),
).PrintRepository(ctx, format, repository)
).PrintRepository(ctx, format, repositories[0])
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@ import (
"context"
"fmt"

modulev1 "buf.build/gen/go/bufbuild/registry/protocolbuffers/go/buf/registry/module/v1"
"connectrpc.com/connect"
"github.com/bufbuild/buf/private/buf/bufcli"
"github.com/bufbuild/buf/private/bufpkg/bufapi"
"github.com/bufbuild/buf/private/bufpkg/bufmodule"
"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/appext"
"github.com/bufbuild/buf/private/pkg/connectclient"
"github.com/bufbuild/buf/private/pkg/syserror"
"github.com/spf13/pflag"
)
Expand Down Expand Up @@ -82,21 +81,26 @@ func run(
if err != nil {
return err
}
service := connectclient.Make(
clientConfig,
moduleFullName.Registry(),
registryv1alpha1connect.NewRepositoryServiceClient,
)
if !flags.Force {
if err := bufcli.PromptUserForDelete(container, "repository", moduleFullName.Name()); err != nil {
return err
}
}
if _, err := service.DeleteRepositoryByFullName(
moduleServiceClient := bufapi.NewClientProvider(clientConfig).V1ModuleServiceClient(moduleFullName.Registry())
if _, err := moduleServiceClient.DeleteModules(
ctx,
connect.NewRequest(
&registryv1alpha1.DeleteRepositoryByFullNameRequest{
FullName: moduleFullName.Owner() + "/" + moduleFullName.Name(),
&modulev1.DeleteModulesRequest{
ModuleRefs: []*modulev1.ModuleRef{
{
Value: &modulev1.ModuleRef_Name_{
Name: &modulev1.ModuleRef_Name{
Owner: moduleFullName.Owner(),
Module: moduleFullName.Name(),
},
},
},
},
},
),
); err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ import (
"context"
"fmt"

modulev1 "buf.build/gen/go/bufbuild/registry/protocolbuffers/go/buf/registry/module/v1"
"connectrpc.com/connect"
"github.com/bufbuild/buf/private/buf/bufcli"
"github.com/bufbuild/buf/private/buf/bufprint"
"github.com/bufbuild/buf/private/bufpkg/bufapi"
"github.com/bufbuild/buf/private/bufpkg/bufmodule"
"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/appext"
"github.com/bufbuild/buf/private/pkg/connectclient"
"github.com/bufbuild/buf/private/pkg/syserror"
"github.com/spf13/pflag"
)

Expand Down Expand Up @@ -87,16 +87,21 @@ func run(
if err != nil {
return err
}
service := connectclient.Make(
clientConfig,
moduleFullName.Registry(),
registryv1alpha1connect.NewRepositoryServiceClient,
)
resp, err := service.GetRepositoryByFullName(
moduleServiceClient := bufapi.NewClientProvider(clientConfig).V1ModuleServiceClient(moduleFullName.Registry())
resp, err := moduleServiceClient.GetModules(
ctx,
connect.NewRequest(
&registryv1alpha1.GetRepositoryByFullNameRequest{
FullName: moduleFullName.Owner() + "/" + moduleFullName.Name(),
&modulev1.GetModulesRequest{
ModuleRefs: []*modulev1.ModuleRef{
{
Value: &modulev1.ModuleRef_Name_{
Name: &modulev1.ModuleRef_Name{
Owner: moduleFullName.Owner(),
Module: moduleFullName.Name(),
},
},
},
},
},
),
)
Expand All @@ -106,10 +111,13 @@ func run(
}
return err
}
repository := resp.Msg.Repository
repositories := resp.Msg.Modules
if len(repositories) != 1 {
return syserror.Newf("unexpected nubmer of repositories returned from server: %d", len(repositories))
}
return bufprint.NewRepositoryPrinter(
clientConfig,
moduleFullName.Registry(),
container.Stdout(),
).PrintRepository(ctx, format, repository)
).PrintRepository(ctx, format, repositories[0])
}
Loading

0 comments on commit 35a9087

Please sign in to comment.