Skip to content

Commit 6f5f181

Browse files
authored
Skip retrieving dynamic argument values on non-Windows platforms (#264)
Also added `GetAzCLIPythonPath()` to calculate the right AzCLI python path for different platforms and installations.
1 parent 75bdeff commit 6f5f181

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

shell/agents/Microsoft.Azure.Agent/DataRetriever.cs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ internal class DataRetriever : IDisposable
1515
private const string MetadataQueryTemplate = "{{\"command\":\"{0}\"}}";
1616
private const string MetadataEndpoint = "https://cli-validation-tool-meta-qry.azurewebsites.net/api/command_metadata";
1717

18+
private static string s_azPythonPath;
1819
private static readonly Dictionary<string, NamingRule> s_azNamingRules;
1920
private static readonly ConcurrentDictionary<string, AzCLICommand> s_azStaticDataCache;
2021

@@ -305,9 +306,28 @@ static DataRetriever()
305306
s_azNamingRules.Add(rule.AzPSCommand, rule);
306307
}
307308

309+
s_azPythonPath = GetAzCLIPythonPath();
308310
s_azStaticDataCache = new(StringComparer.OrdinalIgnoreCase);
309311
}
310312

313+
/// <summary>
314+
/// TODO: Need to support Linux and macOS.
315+
/// </summary>
316+
private static string GetAzCLIPythonPath()
317+
{
318+
if (OperatingSystem.IsWindows())
319+
{
320+
const string AzWindowsPath = @"Microsoft SDKs\Azure\CLI2\python.exe";
321+
string x64Path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), AzWindowsPath);
322+
string x86Path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86), AzWindowsPath);
323+
324+
if (File.Exists(x64Path)) { return x64Path; }
325+
if (File.Exists(x86Path)) { return x86Path; }
326+
}
327+
328+
return null;
329+
}
330+
311331
internal DataRetriever(ResponseData data, HttpClient httpClient)
312332
{
313333
_stop = false;
@@ -476,7 +496,7 @@ private List<string> GetArgValues(ArgumentPair pair)
476496
hasCompleter = param.HasCompleter;
477497
}
478498

479-
if (_stop || !hasCompleter) { return null; }
499+
if (_stop || !hasCompleter || s_azPythonPath is null) { return null; }
480500

481501
// Then, try to get dynamic argument values using AzCLI tab completion.
482502
string commandLine = $"{pair.Command} {pair.Parameter} ";
@@ -490,7 +510,7 @@ private List<string> GetArgValues(ArgumentPair pair)
490510
{
491511
StartInfo = new ProcessStartInfo()
492512
{
493-
FileName = @"C:\Program Files\Microsoft SDKs\Azure\CLI2\python.exe",
513+
FileName = s_azPythonPath,
494514
Arguments = "-Im azure.cli",
495515
UseShellExecute = false,
496516
RedirectStandardOutput = true,

0 commit comments

Comments
 (0)