Skip to content

Commit

Permalink
autogenerate the 'name of credential'
Browse files Browse the repository at this point in the history
Signed-off-by: Priyanshi Gaur <noxdot1134@gmail.com>
  • Loading branch information
nox1134 committed Aug 2, 2024
1 parent 8197acb commit 58b251f
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 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 @@ -30,6 +31,11 @@ func LoginCommand() *cobra.Command {
serverAddress = args[0]
}

if Name == "" {
// Auto-generate the credential name if not provided
Name = generateCredentialName(serverAddress, Username)
}

loginView := login.LoginView{
Server: serverAddress,
Username: Username,
Expand Down Expand Up @@ -61,6 +67,16 @@ func LoginCommand() *cobra.Command {
return cmd
}

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

func createLoginView(loginView *login.LoginView) error {
if loginView == nil {
loginView = &login.LoginView{
Expand Down

0 comments on commit 58b251f

Please sign in to comment.