-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Account] Use the ArgumentCompleter attribute to replace the dynamic …
…parameters of Get-AzContext. (#19655) * [Account] removed dynamic parameters of the Get-AzContext. * [Account] update changelog. * Update ChangeLog.md Co-authored-by: Yunchi Wang <[email protected]>
- Loading branch information
1 parent
2cb394f
commit 0619ab7
Showing
3 changed files
with
55 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
src/Accounts/Accounts/Common/ContextNameCompleterAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using Microsoft.Azure.Commands.Common.Authentication.Abstractions; | ||
using Microsoft.Azure.Commands.Common.Authentication.Models; | ||
using Microsoft.Azure.Commands.ResourceManager.Common; | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Management.Automation; | ||
using System.Management.Automation.Language; | ||
using System.Text; | ||
|
||
namespace Microsoft.Azure.Commands.Profile.Common | ||
{ | ||
/// <summary> | ||
/// This attribute will allow the user to autocomplete the values for valid Azure Context names when applied to context name related cmdlet parameters. | ||
/// </summary> | ||
public class ContextNameCompleterAttribute : ArgumentCompleterAttribute, IArgumentCompleter | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of <see cref="ContextNameCompleterAttribute" /> . | ||
/// </summary> | ||
public ContextNameCompleterAttribute():base(typeof(ContextNameCompleterAttribute)) | ||
{ | ||
|
||
} | ||
|
||
/// <summary> | ||
/// Implementations CompleteArgument function of the <see cref="IArgumentCompleter"/>. | ||
/// </summary> | ||
public IEnumerable<CompletionResult> CompleteArgument(string commandName, string parameterName, string wordToComplete, CommandAst commandAst, IDictionary fakeBoundParameters) | ||
{ | ||
var profile = AzureRmProfileProvider.Instance.Profile; // Object profile with DefaultContextKey. | ||
AzureRmProfile localProfile = profile as AzureRmProfile; | ||
if (localProfile.Contexts != null && localProfile.Contexts.Count > 0) | ||
{ | ||
IEnumerable<string> names = localProfile.Contexts.Keys.ToArray(); | ||
foreach (string name in names) | ||
{ | ||
yield return new CompletionResult($"'{name}'", $"'{name}'", CompletionResultType.ParameterValue, $"'{name}'"); | ||
} | ||
} | ||
else | ||
{ | ||
yield return new CompletionResult($"{localProfile.DefaultContextKey}", $"{localProfile.DefaultContextKey}", CompletionResultType.ParameterValue, $"{localProfile.DefaultContextKey}"); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters