Skip to content

Commit

Permalink
Merge pull request goharbor#148 from nox1134/issue-145
Browse files Browse the repository at this point in the history
Autogenerate the Name of Credential in Login Flow
  • Loading branch information
Vad1mo authored Sep 24, 2024
2 parents 37a6ab4 + 5968399 commit bdf0233
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
18 changes: 18 additions & 0 deletions cmd/harbor/root/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package root
import (
"context"
"fmt"
"strings"

"github.com/goharbor/go-client/pkg/harbor"
"github.com/goharbor/go-client/pkg/sdk/v2.0/client/user"
Expand Down Expand Up @@ -61,6 +62,20 @@ func LoginCommand() *cobra.Command {
return cmd
}

// generateCredentialName creates a default credential name based on server and username
func generateCredentialName(server, username string) string {
if strings.HasPrefix(server, "http://") {
server = strings.ReplaceAll(server, "http://", "")
}
if strings.HasPrefix(server, "https://") {
server = strings.ReplaceAll(server, "https://", "")
}
if username != "" {
return fmt.Sprintf("%s@%s", username, server)
}
return server
}

func createLoginView(loginView *login.LoginView) error {
if loginView == nil {
loginView = &login.LoginView{
Expand Down Expand Up @@ -89,6 +104,9 @@ func runLogin(opts login.LoginView) error {
if err != nil {
return fmt.Errorf("login failed, please check your credentials: %s", err)
}
if opts.Name == "" {
opts.Name = generateCredentialName(opts.Server, opts.Username)
}

cred := utils.Credential{
Name: opts.Name,
Expand Down
10 changes: 2 additions & 8 deletions pkg/views/login/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func CreateView(loginView *LoginView) {
}),
huh.NewInput().
Title("Password").
EchoMode(huh.EchoModePassword).
EchoMode(huh.EchoModePassword).
Value(&loginView.Password).
Validate(func(str string) error {
if str == "" {
Expand All @@ -48,13 +48,7 @@ func CreateView(loginView *LoginView) {
}),
huh.NewInput().
Title("Name of Credential").
Value(&loginView.Name).
Validate(func(str string) error {
if str == "" {
return errors.New("credential name cannot be empty")
}
return nil
}),
Value(&loginView.Name),
),
).WithTheme(theme).Run()

Expand Down
2 changes: 1 addition & 1 deletion pkg/views/user/create/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func CreateUserView(createView *CreateView) {
}),
huh.NewInput().
Title("Password").
EchoMode(huh.EchoModePassword).
EchoMode(huh.EchoModePassword).
Value(&createView.Password).
Validate(func(str string) error {
if str == "" {
Expand Down

0 comments on commit bdf0233

Please sign in to comment.