Skip to content

Remove username prompt on buf registry login #3128

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

Merged
merged 6 commits into from
Jul 8, 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
- Add `buf generate --clean` flag that will delete the directories, jar files, or zip files that the
plugins will write to, prior to generation. Allows cleaning of existing assets without having
to call `rm -rf`.
- Deprecate `--username` flag on and username prompt on `buf registry login`. A username is no longer
required to log in.

## [v1.34.0] - 2024-06-21

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func NewCommand(
return &appcmd.Command{
Use: name + " <domain>",
Short: `Log in to the Buf Schema Registry`,
Long: fmt.Sprintf(`This prompts for your BSR username and a BSR token and updates your %s file with these credentials.
Long: fmt.Sprintf(`This prompts for your BSR token and updates your %s file with these credentials.
The <domain> argument will default to buf.build if not specified.`, netrc.Filename),
Args: appcmd.MaximumNArgs(1),
Run: builder.NewRunFunc(
Expand All @@ -73,8 +73,10 @@ func (f *flags) Bind(flagSet *pflag.FlagSet) {
&f.Username,
usernameFlagName,
"",
"The username to use. This command prompts for a username by default",
"The username to use.",
)
_ = flagSet.MarkDeprecated(usernameFlagName, "This flag is no longer needed as the username is automatically derived from the token")
_ = flagSet.MarkHidden(usernameFlagName)
flagSet.BoolVar(
&f.TokenStdin,
tokenStdinFlagName,
Expand Down Expand Up @@ -135,26 +137,15 @@ func inner(
remote = container.Arg(0)
}
// Do not print unless we are prompting
if flags.Username == "" && !flags.TokenStdin {
if !flags.TokenStdin {
if _, err := fmt.Fprintf(
container.Stdout(),
"Log in with your Buf Schema Registry username. If you don't have a username, create one at https://%s.\n\n",
"Enter the BSR token created at https://%s/settings/user.\n\n",
remote,
); err != nil {
return err
}
}
username := flags.Username
if username == "" {
var err error
username, err = bufcli.PromptUser(container, "Username: ")
if err != nil {
if errors.Is(err, bufcli.ErrNotATTY) {
return errors.New("cannot perform an interactive login from a non-TTY device")
}
return err
}
}
var token string
if flags.TokenStdin {
data, err := io.ReadAll(container.Stdin())
Expand Down Expand Up @@ -197,14 +188,11 @@ func inner(
if user == nil {
return errors.New("no user found for provided token")
}
if user.Username != username {
return errors.New("the username associated with the provided token does not match the provided username")
}
if err := netrc.PutMachines(
container,
netrc.NewMachine(
remote,
username,
user.Username,
token,
),
); err != nil {
Expand All @@ -217,9 +205,9 @@ func inner(
if err != nil {
return err
}
loggedInMessage := fmt.Sprintf("Credentials saved to %s.\n", netrcFilePath)
loggedInMessage := fmt.Sprintf("Logged in as %s. Credentials saved to %s.\n", user.Username, netrcFilePath)
// Unless we did not prompt at all, print a newline first
if flags.Username == "" || !flags.TokenStdin {
if !flags.TokenStdin {
loggedInMessage = "\n" + loggedInMessage
}
if _, err := container.Stdout().Write([]byte(loggedInMessage)); err != nil {
Expand Down