Skip to content

Commit

Permalink
Address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
doriable committed Oct 23, 2024
1 parent 137df84 commit d13cfcb
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 7 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## [Unreleased]

- Add `buf beta registry whoami` command, which checks if you are logged in to the Buf Schema
- Add `buf registry whoami` command, which checks if you are logged in to the Buf Schema
Registry at a given domain.

## [v1.45.0] - 2024-10-08
Expand Down
17 changes: 17 additions & 0 deletions private/buf/bufprint/bufprint.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,14 @@ func NewOrganizationEntity(organization *ownerv1.Organization, remote string) En
}
}

func NewUserEntity(user *registryv1alpha1.User) Entity {
return outputUser{
Username: user.Username,
// We use the Username as the full name for the user when printing.
FullName: user.Username,
}
}

// CuratedPluginPrinter is a printer for curated plugins.
type CuratedPluginPrinter interface {
PrintCuratedPlugin(ctx context.Context, format Format, plugin *registryv1alpha1.CuratedPlugin) error
Expand Down Expand Up @@ -456,3 +464,12 @@ type outputOrganization struct {
func (o outputOrganization) fullName() string {
return o.FullName
}

type outputUser struct {
Username string `json:"username,omitempty"`
FullName string `json:"-" bufprint:"Name"`
}

func (o outputUser) fullName() string {
return o.FullName
}
4 changes: 2 additions & 2 deletions private/buf/cmd/buf/buf.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ import (
"github.com/bufbuild/buf/private/buf/cmd/buf/command/beta/registry/webhook/webhookcreate"
"github.com/bufbuild/buf/private/buf/cmd/buf/command/beta/registry/webhook/webhookdelete"
"github.com/bufbuild/buf/private/buf/cmd/buf/command/beta/registry/webhook/webhooklist"
"github.com/bufbuild/buf/private/buf/cmd/buf/command/beta/registry/whoami"
"github.com/bufbuild/buf/private/buf/cmd/buf/command/beta/stats"
"github.com/bufbuild/buf/private/buf/cmd/buf/command/beta/studioagent"
"github.com/bufbuild/buf/private/buf/cmd/buf/command/breaking"
Expand Down Expand Up @@ -86,6 +85,7 @@ import (
"github.com/bufbuild/buf/private/buf/cmd/buf/command/registry/registrylogin"
"github.com/bufbuild/buf/private/buf/cmd/buf/command/registry/registrylogout"
"github.com/bufbuild/buf/private/buf/cmd/buf/command/registry/sdk/version"
"github.com/bufbuild/buf/private/buf/cmd/buf/command/registry/whoami"
"github.com/bufbuild/buf/private/bufpkg/bufcobra"
"github.com/bufbuild/buf/private/bufpkg/bufconnect"
"github.com/bufbuild/buf/private/bufpkg/bufmodule"
Expand Down Expand Up @@ -177,6 +177,7 @@ func NewRootCommand(name string) *appcmd.Command {
SubCommands: []*appcmd.Command{
registrylogin.NewCommand("login", builder),
registrylogout.NewCommand("logout", builder),
whoami.NewCommand("whoami", builder),
registrycc.NewCommand("cc", builder, ``, false),
{
Use: "commit",
Expand Down Expand Up @@ -244,7 +245,6 @@ func NewRootCommand(name string) *appcmd.Command {
Use: "registry",
Short: "Manage assets on the Buf Schema Registry",
SubCommands: []*appcmd.Command{
whoami.NewCommand("whoami", builder),
{
Use: "webhook",
Short: "Manage webhooks for a repository on the Buf Schema Registry",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

"connectrpc.com/connect"
"github.com/bufbuild/buf/private/buf/bufcli"
"github.com/bufbuild/buf/private/buf/bufprint"
"github.com/bufbuild/buf/private/bufpkg/bufconnect"
"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"
Expand All @@ -31,6 +32,10 @@ import (
"github.com/spf13/pflag"
)

const (
formatFlagName = "format"
)

func NewCommand(
name string,
builder appext.SubCommandBuilder,
Expand All @@ -51,13 +56,22 @@ The <domain> argument will default to buf.build if not specified.`,
}
}

type flags struct{}
type flags struct {
Format string
}

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

func (f *flags) Bind(flagSEt *pflag.FlagSet) {}
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,
Expand Down Expand Up @@ -87,6 +101,22 @@ func run(
if user == nil {
return errors.New("No user found for provided token")
}
_, err = fmt.Fprintf(container.Stdout(), "Logged in as %s.\n", user.Username)
return err
format, err := bufprint.ParseFormat(flags.Format)
if err != nil {
return appcmd.WrapInvalidArgumentError(err)
}
// ParseFormat always expects a format that is either text or json, otherwise it returns
// an error, so do not need a default case for this switch.
switch format {
case bufprint.FormatText:
_, err = fmt.Fprintf(container.Stdout(), "Logged in as %s.\n", user.Username)
return err
case bufprint.FormatJSON:
return bufprint.PrintEntity(
container.Stdout(),
format,
bufprint.NewUserEntity(user),
)
}
return nil
}

0 comments on commit d13cfcb

Please sign in to comment.