Skip to content

Commit 819b459

Browse files
[Account] Use IArgumentCompleter to implement the Environment parameter auto completer. (Azure#16474)
* [Account] Implementations CompleteArgument function of the IArgumentCompleter. * [Account] updated environment argument completer tests. * Update ArgumentCompleterTests.ps1 Co-authored-by: Yunchi Wang <54880216+wyunchi-ms@users.noreply.github.com>
1 parent cd03567 commit 819b459

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

src/Accounts/Accounts.Test/ArgumentCompleterTests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,5 +107,5 @@ function Get-EnvironmentCompleterResult
107107
$command = Get-Command -Name $CmdletName
108108
$environmentCompleterAttribute = $command.Parameters.$ParameterName.Attributes | Where-Object { $_.GetType() -eq [Microsoft.Azure.Commands.Profile.Common.EnvironmentCompleterAttribute]}
109109

110-
return $environmentCompleterAttribute.ScriptBlock.Invoke().CompletionText
110+
return $environmentCompleterAttribute.CompleteArgument($CmdletName, $ParameterName, "", $null, $null).CompletionText
111111
}
Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
22
using Microsoft.Azure.Commands.Common.Authentication.Models;
33
using Microsoft.Azure.Commands.ResourceManager.Common;
4+
using System.Collections;
5+
using System.Collections.Generic;
46
using System.Linq;
57
using System.Management.Automation;
8+
using System.Management.Automation.Language;
69

710
namespace Microsoft.Azure.Commands.Profile.Common
811
{
912
/// <summary>
1013
/// This attribute will allow the user to autocomplete the values for valid Azure Environment names when applied to Environment related cmdlet parameters.
1114
/// </summary>
12-
public class EnvironmentCompleterAttribute : ArgumentCompleterAttribute
15+
public class EnvironmentCompleterAttribute : ArgumentCompleterAttribute, IArgumentCompleter
1316
{
1417
/// <summary>
1518
/// Initializes a new instance of <see cref="EnvironmentCompleterAttribute" /> .
1619
/// </summary>
17-
public EnvironmentCompleterAttribute() : base(CreateScriptBlock())
20+
public EnvironmentCompleterAttribute() : base(typeof(EnvironmentCompleterAttribute))
1821
{
1922
}
2023

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

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

0 commit comments

Comments
 (0)