Skip to content

Commit

Permalink
use global private function instead of anon
Browse files Browse the repository at this point in the history
  • Loading branch information
tarunKoyalwar committed Feb 20, 2024
1 parent d8c2b53 commit 7ad29d3
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions auth/pdcp/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,13 @@ var (
// when validate is true any existing credentials are validated
// Note: this is meant to be used in cli only (interactive mode)
func CheckNValidateCredentials(toolName string) {
userStrFn := func(creds *PDCPCredentials) string {
user := fmt.Sprintf("@%v", creds.Username)
if creds.Username == "" {
user = creds.Email
}
return user
}
h := &PDCPCredHandler{}
creds, err := h.GetCreds()
if err == nil {
// validate by fetching user profile
gotCreds, err := h.ValidateAPIKey(creds.APIKey, creds.Server, toolName)
if err == nil {
gologger.Info().Msgf("You are logged in as (%v)", userStrFn(gotCreds))
gologger.Info().Msgf("You are logged in as (%v)", userIdentifier(gotCreds))
os.Exit(0)
}
gologger.Error().Msgf("Invalid API key found in file, please recheck or recreate your API key and retry.")
Expand All @@ -63,7 +56,7 @@ func CheckNValidateCredentials(toolName string) {
// validate by fetching user profile
validatedCreds, err := h.ValidateAPIKey(apiKey, apiServer, toolName)
if err == nil {
gologger.Info().Msgf("Successfully logged in as (%v)", userStrFn(validatedCreds))
gologger.Info().Msgf("Successfully logged in as (%v)", userIdentifier(validatedCreds))
if saveErr := h.SaveCreds(validatedCreds); saveErr != nil {
gologger.Warning().Msgf("Could not save credentials to file: %s\n", saveErr)
}
Expand All @@ -80,3 +73,13 @@ func maskKey(key string) string {
}
return fmt.Sprintf("%v%v", key[:3], strings.Repeat("*", len(key)-3))
}

// userIdentifier returns user identifier in format @username
// if username is empty, it returns email
func userIdentifier(creds *PDCPCredentials) string {
user := fmt.Sprintf("@%v", creds.Username)
if creds.Username == "" {
user = creds.Email
}
return user
}

0 comments on commit 7ad29d3

Please sign in to comment.