Skip to content

Commit

Permalink
Skip retrieving dynamic argument values on non-Windows platforms (#264)
Browse files Browse the repository at this point in the history
Also added `GetAzCLIPythonPath()` to calculate the right AzCLI python path for different platforms and installations.
  • Loading branch information
daxian-dbw authored Oct 31, 2024
1 parent 75bdeff commit 6f5f181
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions shell/agents/Microsoft.Azure.Agent/DataRetriever.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ internal class DataRetriever : IDisposable
private const string MetadataQueryTemplate = "{{\"command\":\"{0}\"}}";
private const string MetadataEndpoint = "https://cli-validation-tool-meta-qry.azurewebsites.net/api/command_metadata";

private static string s_azPythonPath;
private static readonly Dictionary<string, NamingRule> s_azNamingRules;
private static readonly ConcurrentDictionary<string, AzCLICommand> s_azStaticDataCache;

Expand Down Expand Up @@ -305,9 +306,28 @@ static DataRetriever()
s_azNamingRules.Add(rule.AzPSCommand, rule);
}

s_azPythonPath = GetAzCLIPythonPath();
s_azStaticDataCache = new(StringComparer.OrdinalIgnoreCase);
}

/// <summary>
/// TODO: Need to support Linux and macOS.
/// </summary>
private static string GetAzCLIPythonPath()
{
if (OperatingSystem.IsWindows())
{
const string AzWindowsPath = @"Microsoft SDKs\Azure\CLI2\python.exe";
string x64Path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), AzWindowsPath);
string x86Path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86), AzWindowsPath);

if (File.Exists(x64Path)) { return x64Path; }
if (File.Exists(x86Path)) { return x86Path; }
}

return null;
}

internal DataRetriever(ResponseData data, HttpClient httpClient)
{
_stop = false;
Expand Down Expand Up @@ -476,7 +496,7 @@ private List<string> GetArgValues(ArgumentPair pair)
hasCompleter = param.HasCompleter;
}

if (_stop || !hasCompleter) { return null; }
if (_stop || !hasCompleter || s_azPythonPath is null) { return null; }

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

0 comments on commit 6f5f181

Please sign in to comment.