Skip to content

[Account] Use IArgumentCompleter to implement the Environment parameter auto completer. #16474

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Accounts/Accounts.Test/ArgumentCompleterTests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -107,5 +107,5 @@ function Get-EnvironmentCompleterResult
$command = Get-Command -Name $CmdletName
$environmentCompleterAttribute = $command.Parameters.$ParameterName.Attributes | Where-Object { $_.GetType() -eq [Microsoft.Azure.Commands.Profile.Common.EnvironmentCompleterAttribute]}

return $environmentCompleterAttribute.ScriptBlock.Invoke().CompletionText
return $environmentCompleterAttribute.CompleteArgument($CmdletName, $ParameterName, "", $null, $null).CompletionText
}
22 changes: 14 additions & 8 deletions src/Accounts/Accounts/Common/EnvironmentCompleterAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
using Microsoft.Azure.Commands.Common.Authentication.Models;
using Microsoft.Azure.Commands.ResourceManager.Common;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;
using System.Management.Automation.Language;

namespace Microsoft.Azure.Commands.Profile.Common
{
/// <summary>
/// This attribute will allow the user to autocomplete the values for valid Azure Environment names when applied to Environment related cmdlet parameters.
/// </summary>
public class EnvironmentCompleterAttribute : ArgumentCompleterAttribute
public class EnvironmentCompleterAttribute : ArgumentCompleterAttribute, IArgumentCompleter
{
/// <summary>
/// Initializes a new instance of <see cref="EnvironmentCompleterAttribute" /> .
/// </summary>
public EnvironmentCompleterAttribute() : base(CreateScriptBlock())
public EnvironmentCompleterAttribute() : base(typeof(EnvironmentCompleterAttribute))
{
}

Expand All @@ -28,13 +31,16 @@ public static string[] GetEnvironments()
return profileClient.ListEnvironments(null).Select(x => x.Name).ToArray();
}

private static ScriptBlock CreateScriptBlock()
/// <summary>
/// Implementations CompleteArgument function of the <see cref="IArgumentCompleter"/>.
/// </summary>
public IEnumerable<CompletionResult> CompleteArgument(string commandName, string parameterName, string wordToComplete, CommandAst commandAst, IDictionary fakeBoundParameters)
{
string script = "param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)\n" +
"$environments = [Microsoft.Azure.Commands.Profile.Common.EnvironmentCompleterAttribute]::GetEnvironments()\n" +
"$environments | Where-Object { $_ -Like \"$wordToComplete*\" } | ForEach-Object { [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_) }";
ScriptBlock scriptBlock = ScriptBlock.Create(script);
return scriptBlock;
IEnumerable<string> names = GetEnvironments().Where(env => env.ToLower().StartsWith(wordToComplete.ToLower())) ;
foreach (string name in names)
{
yield return new CompletionResult(name, name, CompletionResultType.ParameterValue, name);
}
}
}
}