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

feat(cmd/registry/auth/basic): allow to specify credentials as cli flags #314

Closed
wants to merge 1 commit into from
Closed
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
26 changes: 22 additions & 4 deletions cmd/registry/auth/basic/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import (

type loginOptions struct {
*options.Common
username string
password string
}

// NewBasicCmd returns the basic command.
Expand All @@ -51,15 +53,20 @@ func NewBasicCmd(ctx context.Context, opt *options.Common) *cobra.Command {
},
}

cmd.Flags().StringVarP(&o.username, "username", "u", "", "username of the basic authentication to the OCI registry")
cmd.Flags().StringVarP(&o.password, "password", "p", "", "password of the basic authentication to the OCI registry")

return cmd
}

// RunBasic executes the business logic for the basic command.
func (o *loginOptions) RunBasic(ctx context.Context, args []string) error {
reg := args[0]
var reg string
if len(args) > 0 {
reg = args[0]
}

user, token, err := utils.GetCredentials(o.Printer)
if err != nil {
if err := o.ensureCredentials(); err != nil {
return err
}

Expand All @@ -74,11 +81,22 @@ func (o *loginOptions) RunBasic(ctx context.Context, args []string) error {
return fmt.Errorf("unable to create new store: %w", err)
}

if err := basic.Login(ctx, client, credentialStore, reg, user, token); err != nil {
if err := basic.Login(ctx, client, credentialStore, reg, o.username, o.password); err != nil {
return err
}
o.Printer.Verbosef("credentials added to credential store")
o.Printer.Success.Println("Login succeeded")

return nil
}

func (o *loginOptions) ensureCredentials() error {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we support the use case where the username is given on the command line flag and the password from stdin?

if o.username == "" || o.password == "" {
var err error
if o.username, o.password, err = utils.GetCredentials(o.Printer); err != nil {
return err
}
}

return nil
}