Skip to content

Commit 1cef871

Browse files
refactor: use credential provider chain in whoami
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 577a913 commit 1cef871

File tree

1 file changed

+10
-22
lines changed

1 file changed

+10
-22
lines changed

cloudsmith_cli/cli/commands/whoami.py

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
"""CLI/Commands - Retrieve authentication status."""
22

3-
import os
4-
53
import click
64

75
from ...core import keyring
86
from ...core.api.exceptions import ApiException
97
from ...core.api.user import get_token_metadata, get_user_brief
108
from .. import decorators, utils
11-
from ..config import CredentialsReader
129
from ..exceptions import handle_api_exceptions
1310
from .main import main
1411

@@ -26,26 +23,17 @@ def _get_active_method(api_config):
2623
def _get_api_key_source(opts):
2724
"""Determine where the API key was loaded from.
2825
29-
Checks in priority order matching actual resolution:
30-
CLI --api-key flag > CLOUDSMITH_API_KEY env var > credentials.ini.
26+
Uses the credential provider chain result attached by initialise_api.
3127
"""
32-
if not opts.api_key:
33-
return {"configured": False, "source": None, "source_key": None}
34-
35-
env_key = os.environ.get("CLOUDSMITH_API_KEY")
36-
37-
# If env var is set but differs from the resolved key, CLI flag won
38-
if env_key and opts.api_key != env_key:
39-
source, key = "CLI --api-key flag", "cli_flag"
40-
elif env_key:
41-
suffix = env_key[-4:]
42-
source, key = f"CLOUDSMITH_API_KEY env var (ends with ...{suffix})", "env_var"
43-
elif creds := CredentialsReader.find_existing_files():
44-
source, key = f"credentials.ini ({creds[0]})", "credentials_file"
45-
else:
46-
source, key = "CLI --api-key flag", "cli_flag"
47-
48-
return {"configured": True, "source": source, "source_key": key}
28+
credential = getattr(opts, "credential", None)
29+
if credential:
30+
return {
31+
"configured": True,
32+
"source": credential.source_detail or credential.source_name,
33+
"source_key": credential.source_name,
34+
}
35+
36+
return {"configured": False, "source": None, "source_key": None}
4937

5038

5139
def _get_sso_status(api_host):

0 commit comments

Comments
 (0)