-
Notifications
You must be signed in to change notification settings - Fork 223
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
Refactor GetCommandHandler to not use dynamic #1268
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,29 +48,53 @@ public async Task<List<PSCommandMessage>> Handle(GetCommandParams request, Cance | |
PSCommand psCommand = new PSCommand(); | ||
|
||
// Executes the following: | ||
// Get-Command -CommandType Function,Cmdlet,ExternalScript | Select-Object -Property Name,ModuleName | Sort-Object -Property Name | ||
// Get-Command -CommandType Function,Cmdlet,ExternalScript | Sort-Object -Property Name | ||
psCommand | ||
.AddCommand("Microsoft.PowerShell.Core\\Get-Command") | ||
.AddParameter("CommandType", new[] { "Function", "Cmdlet", "ExternalScript" }) | ||
.AddCommand("Microsoft.PowerShell.Utility\\Select-Object") | ||
.AddParameter("Property", new[] { "Name", "ModuleName" }) | ||
.AddCommand("Microsoft.PowerShell.Utility\\Sort-Object") | ||
.AddParameter("Property", "Name"); | ||
|
||
IEnumerable<PSObject> result = await _powerShellContextService.ExecuteCommandAsync<PSObject>(psCommand).ConfigureAwait(false); | ||
IEnumerable<CommandInfo> result = await _powerShellContextService.ExecuteCommandAsync<CommandInfo>(psCommand).ConfigureAwait(false); | ||
|
||
var commandList = new List<PSCommandMessage>(); | ||
if (result != null) | ||
{ | ||
foreach (dynamic command in result) | ||
foreach (CommandInfo command in result) | ||
{ | ||
// Some info objects have a quicker way to get the DefaultParameterSet. These | ||
// are also the most likely to show up so win-win. | ||
string defaultParameterSet = null; | ||
switch (command) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Issue found: Add a 'default' clause to this 'switch' statement. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
{ | ||
case CmdletInfo info: | ||
defaultParameterSet = info.DefaultParameterSet; | ||
break; | ||
case FunctionInfo info: | ||
defaultParameterSet = info.DefaultParameterSet; | ||
break; | ||
} | ||
|
||
if (defaultParameterSet == null) | ||
{ | ||
// Try to get the default ParameterSet if it isn't streamlined (ExternalScriptInfo for example) | ||
foreach (CommandParameterSetInfo parameterSetInfo in command.ParameterSets) | ||
{ | ||
if (parameterSetInfo.IsDefault) | ||
{ | ||
defaultParameterSet = parameterSetInfo.Name; | ||
break; | ||
} | ||
} | ||
} | ||
|
||
commandList.Add(new PSCommandMessage | ||
{ | ||
Name = command.Name, | ||
ModuleName = command.ModuleName, | ||
Parameters = command.Parameters, | ||
ParameterSets = command.ParameterSets, | ||
DefaultParameterSet = command.DefaultParameterSet | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the story on needing the for-loop instead of this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. CommandInfo's only have: no There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Given that we're probably mostly going to deal with those types, I think that would make sense as an optimisation There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added a switch but still kept the loop for everything else (which atm is just ExternalScript). |
||
DefaultParameterSet = defaultParameterSet | ||
}); | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just wanna use the new pattern matching switch but it says it's still "preview" 😭