Skip to content

Commit

Permalink
Merge branch 'master' into DropScanningRegistration
Browse files Browse the repository at this point in the history
  • Loading branch information
lzybkr committed Jul 10, 2015
2 parents f4e1fef + 0a9aa82 commit 8ead49c
Show file tree
Hide file tree
Showing 3 changed files with 259 additions and 12 deletions.
220 changes: 220 additions & 0 deletions Microsoft.Azure.ArgumentCompleters.ps1
@@ -0,0 +1,220 @@
#
# .SYNOPSIS
#
# Auto-complete the -StorageAccountName parameter value for Azure PowerShell cmdlets.
#
# .NOTES
#
# Created by Trevor Sullivan <pcgeek86@gmail.com>
# http://trevorsullivan.net
#
function StorageAccount_StorageAccountNameCompleter
{
[ArgumentCompleter(
Parameter = 'StorageAccountName',
Command = { Get-CommandWithParameter -Module Azure -ParameterName StorageAccountName },
Description = 'Complete the -StorageAccountName parameter value for Azure cmdlets: Get-AzureStorageAccount -StorageAccountName <TAB>'
)]
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)

#Write-Verbose -Message ('Called Azure StorageAccountName completer at {0}' -f (Get-Date))

$CacheKey = 'StorageAccount_StorageAccountNameCache'
$StorageAccountNameCache = Get-CompletionPrivateData -Key $CacheKey

### Return the cached value if it has not expired
if ($StorageAccountNameCache) {
return $StorageAccountNameCache
}

$StorageAccountList = Get-AzureStorageAccount -WarningAction SilentlyContinue | Where-Object {$PSItem.StorageAccountName -match ${wordToComplete} } | ForEach-Object {
$CompletionResult = @{
CompletionText = $PSItem.StorageAccountName
ToolTip = 'Storage Account "{0}" in "{1}" region.' -f $PSItem.StorageAccountName, $PSItem.Location
ListItemText = '{0} ({1})' -f $PSItem.StorageAccountName, $PSItem.Location
CompletionResultType = [System.Management.Automation.CompletionResultType]::ParameterValue
}
New-CompletionResult @CompletionResult
}

Set-CompletionPrivateData -Key $CacheKey -Value $StorageAccountList
return $StorageAccountList
}

#
# .SYNOPSIS
#
# Auto-complete the -Name parameter value for Azure PowerShell storage container cmdlets.
#
# .NOTES
#
# Created by Trevor Sullivan <pcgeek86@gmail.com>
# http://trevorsullivan.net
#
function AzureStorage_StorageContainerNameCompleter
{
[ArgumentCompleter(
Parameter = 'Name',
Command = { Get-CommandWithParameter -Module Azure -ParameterName Name -Name *container* },
Description = 'Complete the -Name parameter value for Azure cmdlets: Get-AzureStorageContainer -Context $Context -Name <TAB>'
)]
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)

$CacheKey = 'AzureStorage_ContainerNameCache'
$ContainerNameCache = Get-CompletionPrivateData -Key $CacheKey

### Return the cached value if it has not expired
if ($ContainerNameCache) {
return $ContainerNameCache
}

$ContainerList = Get-AzureStorageContainer -Context $fakeBoundParameter['Context'] | Where-Object -FilterScript { $PSItem.Name -match ${wordToComplete} } | ForEach-Object {
$CompletionResult = @{
CompletionText = $PSItem.Name
ToolTip = 'Storage Container "{0}" in "{1}" Storage Account.' -f $PSItem.Name, $fakeBoundParameter['Context'].StorageAccountName
ListItemText = '{0} ({1})' -f $PSItem.Name, $fakeBoundParameter['Context'].StorageAccountName
CompletionResultType = [System.Management.Automation.CompletionResultType]::ParameterValue
}
New-CompletionResult @CompletionResult
}

Set-CompletionPrivateData -Key $CacheKey -Value $ContainerList
return $ContainerList
}

#
# .SYNOPSIS
#
# Auto-complete the -ServiceName parameter value for Azure PowerShell cmdlets.
#
# .NOTES
#
# Created by Trevor Sullivan <pcgeek86@gmail.com>
# http://trevorsullivan.net
#
function CloudService_ServiceNameCompleter
{
[ArgumentCompleter(
Parameter = 'ServiceName',
Command = { Get-CommandWithParameter -Module Azure -ParameterName ServiceName },
Description = 'Complete the -ServiceName parameter value for Azure cmdlets: Get-AzureService -ServiceName <TAB>'
)]
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)

#Write-Verbose -Message ('Called Azure ServiceName completer at {0}' -f (Get-Date))
$CacheKey = 'CloudService_ServiceNameCache'
$ServiceNameCache = Get-CompletionPrivateData -Key $CacheKey
if ($ServiceNameCache) {
return $ServiceNameCache
}

$ItemList = Get-AzureService | Where-Object { $PSItem.ServiceName -match ${wordToComplete} } | ForEach-Object {
$CompletionResult = @{
CompletionText = $PSItem.ServiceName
ToolTip = 'Cloud Service in "{0}" region.' -f $PSItem.ExtendedProperties.ResourceLocation
ListItemText = '{0} ({1})' -f $PSItem.ServiceName, $PSItem.ExtendedProperties.ResourceLocation
CompletionResultType = [System.Management.Automation.CompletionResultType]::ParameterValue
}
New-CompletionResult @CompletionResult
}
Set-CompletionPrivateData -Key $CacheKey -Value $ItemList

return $ItemList
}

#
# .SYNOPSIS
#
# Auto-complete the -SubscriptionName parameter value for Azure PowerShell cmdlets.
#
# .NOTES
#
# Created by Trevor Sullivan <pcgeek86@gmail.com>
# http://trevorsullivan.net
#
function Subscription_SubscriptionNameCompleter
{
[ArgumentCompleter(
Parameter = 'SubscriptionName',
Command = { Get-CommandWithParameter -Module Azure -ParameterName SubscriptionName },
Description = 'Complete the -SubscriptionName parameter value for Azure cmdlets: Select-AzureSubscription -SubscriptionName <TAB>'
)]
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)

#Write-Verbose -Message ('Called Azure SubscriptionName completer at {0}' -f (Get-Date))

### Attempt to read Azure subscription details from the cache
$CacheKey = 'AzureSubscription_SubscriptionNameCache'
$SubscriptionNameCache = Get-CompletionPrivateData -Key $CacheKey

### If there is a valid cache for the Azure subscription names, then go ahead and return them immediately
if ($SubscriptionNameCache) {
return $SubscriptionNameCache
}

### Create fresh completion results for Azure subscriptions
$ItemList = Get-AzureSubscription | Where-Object { $PSItem.SubscriptionName -match ${wordToComplete} } | ForEach-Object {
$CompletionResult = @{
CompletionText = $PSItem.SubscriptionName
ToolTip = 'Azure subscription "{0}" with ID {1}.' -f $PSItem.SubscriptionName, $PSItem.SubscriptionId
ListItemText = '{0} ({1})' -f $PSItem.SubscriptionName, $PSItem.SubscriptionId
CompletionResultType = [System.Management.Automation.CompletionResultType]::ParameterValue
}
New-CompletionResult @CompletionResult
}

### Update the cache for Azure subscription names
Set-CompletionPrivateData -Key $CacheKey -Value $ItemList

### Return the fresh completion results
return $ItemList
}

#
# .SYNOPSIS
#
# Auto-complete the -Name parameter value for Azure PowerShell virtual machine cmdlets.
#
# .NOTES
#
# Created by Trevor Sullivan <pcgeek86@gmail.com>
# http://trevorsullivan.net
# http://twitter.com/pcgeek86
#
function AzureVirtualMachine_NameCompleter
{
[ArgumentCompleter(
Parameter = 'Name',
Command = { Get-CommandWithParameter -Module Azure -ParameterName Name -Noun AzureVM },
Description = 'Complete the -Name parameter value for Azure virtual machine cmdlets: Stop-AzureVM -Name <TAB>'
)]
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)

#Write-Verbose -Message ('Called Azure Virtual Machine Name completer at {0}' -f (Get-Date))

### Attempt to read Azure virtual machine details from the cache
$CacheKey = 'AzureVirtualMachine_NameCache'
$VirtualMachineNameCache = Get-CompletionPrivateData -Key $CacheKey

### If there is a valid cache for the Azure virtual machine names, then go ahead and return them immediately
if ($VirtualMachineNameCache -and (Get-Date) -gt $VirtualMachineNameCache.ExpirationTime) {
return $VirtualMachineNameCache
}

### Create fresh completion results for Azure virtual machines
$ItemList = Get-AzureVM | Where-Object { $PSItem.Name -match ${wordToComplete} } | ForEach-Object {
$CompletionResult = @{
CompletionText = '{0} -ServiceName {1}' -f $PSItem.Name, $PSItem.ServiceName
ToolTip = 'Azure VM {0}/{1} in state {2}.' -f $PSItem.ServiceName, $PSItem.Name, $PSItem.Status
ListItemText = '{0}/{1}' -f $PSItem.ServiceName, $PSItem.Name
CompletionResultType = [System.Management.Automation.CompletionResultType]::ParameterValue
}
New-CompletionResult @CompletionResult
}

### Update the cache for Azure virtual machines
Set-CompletionPrivateData -Key $CacheKey -Value $ItemList

### Return the fresh completion results
return $ItemList
}
41 changes: 35 additions & 6 deletions TabExpansion++.psm1
Expand Up @@ -44,14 +44,22 @@ function New-CompletionResult
$ListItemText,

[System.Management.Automation.CompletionResultType]
$CompletionResultType = [System.Management.Automation.CompletionResultType]::ParameterValue)
$CompletionResultType = [System.Management.Automation.CompletionResultType]::ParameterValue,

[Parameter(Mandatory = $false)]
[switch] $NoQuotes = $false
)

process
{
$toolTipToUse = if ($ToolTip -eq '') { $CompletionText } else { $ToolTip }
$listItemToUse = if ($ListItemText -eq '') { $CompletionText } else { $ListItemText }

if ($CompletionResultType -eq [System.Management.Automation.CompletionResultType]::ParameterValue)
# If the caller explicitly requests that quotes
# not be included, via the -NoQuotes parameter,
# then skip adding quotes.

if ($CompletionResultType -eq [System.Management.Automation.CompletionResultType]::ParameterValue -and -not $NoQuotes)
{
# Add single quotes for the caller in case they are needed.
# We use the parser to robustly determine how it will treat
Expand Down Expand Up @@ -134,9 +142,18 @@ function Set-CompletionPrivateData
$Key,

[object]
$Value)
$Value,

[ValidateNotNullOrEmpty()]
[int]
$ExpirationSeconds = 604800
)

$completionPrivateData[$key] = $value
$Cache = [PSCustomObject]@{
Value = $Value
ExpirationTime = (Get-Date).AddSeconds($ExpirationSeconds)
}
$completionPrivateData[$key] = $Cache
}

#############################################################################
Expand All @@ -148,7 +165,11 @@ function Get-CompletionPrivateData
[string]
$Key)

return $completionPrivateData[$key]

$cacheValue = $completionPrivateData[$key]
if ((Get-Date) -lt $cacheValue.ExpirationTime) {
return $cacheValue.Value
}
}

#############################################################################
Expand Down Expand Up @@ -484,7 +505,15 @@ function Test-ArgumentCompleter
#############################################################################
#
# .SYNOPSIS
# Retrieves a list of argument completers that have been loaded into the
# PowerShell session.
#
# .PARAMETER Name
# The name of the argument complete to retrieve. This parameter supports
# wildcards (asterisk).
#
# .EXAMPLE
# Get-ArgumentCompleter -Name *Azure*;
function Get-ArgumentCompleter
{
[CmdletBinding()]
Expand Down Expand Up @@ -671,7 +700,7 @@ function TryNativeCommandOptionCompletion
param($ast)
return $offset -gt $ast.Extent.StartOffset -and
$offset -le $ast.Extent.EndOffset -and
$ast.Extent.Text -in '-','--'
$ast.Extent.Text.StartsWith('-')
}
$option = $ast.Find($offsetInOptionExtentPredicate, $true)
if ($option -ne $null)
Expand Down
10 changes: 4 additions & 6 deletions WindowsExe.ArgumentCompleters.ps1
Expand Up @@ -26,7 +26,7 @@ function PowerShellExeCompletion
{
$completions = "Text", "XML"
}
elseif ("WindowsStyle".StartsWith($parameterAst.ParameterName, "OrdinalIgnoreCase"))
elseif ("WindowStyle".StartsWith($parameterAst.ParameterName, "OrdinalIgnoreCase"))
{
$completions = "Normal", "Minimized", "Maximized", "Hidden"
}
Expand All @@ -35,11 +35,9 @@ function PowerShellExeCompletion
$completions = ([Microsoft.PowerShell.ExecutionPolicy] | Get-Member -Static -MemberType Property).Name
}

foreach ($completion in $completions)
{
$tryParameters = $false
New-CompletionResult $completion
}
$tryParameters = ($completions.Length -eq 0)

$completions | Where-Object { $_ -match "^$wordToComplete" } | ForEach-Object { New-CompletionResult $_ }
}

if ($tryParameters -and ($wordToComplete.StartsWith("-") -or "" -eq $wordToComplete))
Expand Down

0 comments on commit 8ead49c

Please sign in to comment.