Skip to content

Commit 5163bdd

Browse files
committed
fall back to known install locations when PATH isn't set
1 parent 0bd2916 commit 5163bdd

1 file changed

Lines changed: 26 additions & 4 deletions

File tree

  • sdk/identity/azure-identity/azure/identity/_credentials

sdk/identity/azure-identity/azure/identity/_credentials/azure_cli.py

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,34 @@ def parse_token(output):
8282

8383

8484
def get_safe_working_dir():
85-
"""Invoke 'az' from a directory on $PATH to get 'az' from the path, not the executing program's directory"""
85+
"""Invoke 'az' from a directory on $PATH or a well-known install location, not the executing program's directory"""
86+
87+
path = os.environ.get("PATH")
8688

87-
path = os.environ["PATH"]
8889
if sys.platform.startswith("win"):
89-
return path.split(";")[0]
90-
return path.split(":")[0]
90+
if path:
91+
return path.split(";")[0]
92+
93+
# no system path; check well-known install locations
94+
cli_path = "Microsoft SDKs\\Azure\\CLI2\\wbin"
95+
for directory in (os.environ.get("PROGRAMFILES(X86)"), os.environ.get("PROGRAMFILES")):
96+
if directory:
97+
path = os.path.join(directory, cli_path)
98+
if os.path.exists(os.path.join(path, "az.cmd")):
99+
return path
100+
101+
raise CredentialUnavailableError(message=CLI_NOT_FOUND)
102+
103+
# linux or mac
104+
if path:
105+
return path.split(":")[0]
106+
107+
# no system path; check well-known install locations
108+
for path in ("/usr/bin", "/usr/local/bin"):
109+
if os.path.exists(path + "/az"):
110+
return path
111+
112+
raise CredentialUnavailableError(message=CLI_NOT_FOUND)
91113

92114

93115
def sanitize_output(output):

0 commit comments

Comments
 (0)