diff --git a/CHANGELOG.md b/CHANGELOG.md index 4305847027..0a0ded5ba5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/private/buf/bufprint/bufprint.go b/private/buf/bufprint/bufprint.go index 1647a41f93..24c35f835d 100644 --- a/private/buf/bufprint/bufprint.go +++ b/private/buf/bufprint/bufprint.go @@ -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 @@ -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 +} diff --git a/private/buf/cmd/buf/buf.go b/private/buf/cmd/buf/buf.go index de11357338..277c88d653 100644 --- a/private/buf/cmd/buf/buf.go +++ b/private/buf/cmd/buf/buf.go @@ -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" @@ -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" @@ -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", @@ -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", diff --git a/private/buf/cmd/buf/command/beta/registry/whoami/usage.gen.go b/private/buf/cmd/buf/command/registry/whoami/usage.gen.go similarity index 100% rename from private/buf/cmd/buf/command/beta/registry/whoami/usage.gen.go rename to private/buf/cmd/buf/command/registry/whoami/usage.gen.go diff --git a/private/buf/cmd/buf/command/beta/registry/whoami/whoami.go b/private/buf/cmd/buf/command/registry/whoami/whoami.go similarity index 75% rename from private/buf/cmd/buf/command/beta/registry/whoami/whoami.go rename to private/buf/cmd/buf/command/registry/whoami/whoami.go index 06e760110f..1639b6ac6b 100644 --- a/private/buf/cmd/buf/command/beta/registry/whoami/whoami.go +++ b/private/buf/cmd/buf/command/registry/whoami/whoami.go @@ -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" @@ -31,6 +32,10 @@ import ( "github.com/spf13/pflag" ) +const ( + formatFlagName = "format" +) + func NewCommand( name string, builder appext.SubCommandBuilder, @@ -51,13 +56,22 @@ The 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, @@ -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 }