diff --git a/auth/pdcp/auth.go b/auth/pdcp/auth.go index 3e7bb11..05fb7c9 100644 --- a/auth/pdcp/auth.go +++ b/auth/pdcp/auth.go @@ -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.") @@ -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) } @@ -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 +}