-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGlobalProfile.ps1
166 lines (158 loc) · 6.13 KB
/
GlobalProfile.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
$cred = Import-Clixml -Path $profile\..\Cred.xml
function New-Credential {
[OutputType([pscredential])]
param (
[Parameter(Mandatory)]
[String]$User,
[Parameter(Mandatory)]
[String]$Password
)
[pscredential]::new(
$User,
(ConvertTo-SecureString -AsPlainText -Force -String $Password)
)
}
$adUserSearch = {
param (
[String]$CommandName,
[String]$ParameterName,
[String]$WordToComplete,
[System.Management.Automation.Language.CommandAst]$CommandAst,
[System.Collections.IDictionary]$FakeBoundParameters
)
([ADSISearcher]"(&(objectClass=user)(samAccountName=$WordToComplete*))").FindAll() |
ForEach-Object {
$user = $_.GetDirectoryEntry()
[System.Management.Automation.CompletionResult]::new(
$user.sAMAccountName,
$user.sAMAccountName,
[System.Management.Automation.CompletionResultType]::ParameterValue,
"User repository with source $($user.Name)"
)
}
}
Register-ArgumentCompleter -CommandName New-OPGitPullRequest -ParameterName Reviewers -ScriptBlock $adUserSearch
Register-ArgumentCompleter -CommandName Find-Module, Install-Module -ParameterName Repository -ScriptBlock {
param (
[String]$CommandName,
[String]$ParameterName,
[String]$WordToComplete,
[System.Management.Automation.Language.CommandAst]$CommandAst,
[System.Collections.IDictionary]$FakeBoundParameters
)
Get-PSRepository -Name "$WordToComplete*" | Sort-Object -Property Name | ForEach-Object {
[System.Management.Automation.CompletionResult]::new(
$_.Name,
$_.Name,
[System.Management.Automation.CompletionResultType]::ParameterValue,
"$($_.InstallationPolicy) repository with source $($_.SourceLocation)"
)
}
}
Register-ArgumentCompleter -CommandName Find-Module, Install-Module -ParameterName Name -ScriptBlock {
param (
[String]$CommandName,
[String]$ParameterName,
[String]$WordToComplete,
[System.Management.Automation.Language.CommandAst]$CommandAst,
[System.Collections.IDictionary]$FakeBoundParameters
)
$params = @{}
if ($FakeBoundParameters['Repository']) {
$params.Repository = $FakeBoundParameters['Repository']
}
Find-Module @params -Name "$WordToComplete*" | Sort-Object -Property Name | ForEach-Object {
[System.Management.Automation.CompletionResult]::new(
$_.Name,
$_.Name,
[System.Management.Automation.CompletionResultType]::ParameterValue,
"$($_.Description)"
)
}
}
Register-ArgumentCompleter -CommandName Update-Module -ParameterName Name -ScriptBlock {
param (
[String]$CommandName,
[String]$ParameterName,
[String]$WordToComplete,
[System.Management.Automation.Language.CommandAst]$CommandAst,
[System.Collections.IDictionary]$FakeBoundParameters
)
Get-InstalledModule -Name "$WordToComplete*" | Sort-Object -Property Name | ForEach-Object {
[System.Management.Automation.CompletionResult]::new(
$_.Name,
$_.Name,
[System.Management.Automation.CompletionResultType]::ParameterValue,
"$($_.Description)"
)
}
}
Register-ArgumentCompleter -CommandName Get-OPGitPullRequest, New-OPGitPullRequest, Merge-OPGitPullRequest, Get-OPGitBranch -ParameterName Repository -ScriptBlock {
param (
[String]$CommandName,
[String]$ParameterName,
[String]$WordToComplete,
[System.Management.Automation.Language.CommandAst]$CommandAst,
[System.Collections.IDictionary]$FakeBoundParameters
)
$cred = $FakeBoundParameters['Credential']
if (-not $cred) {
foreach ($key in $PSDefaultParameterValues.Keys) {
$command, $param = $key.Split(':')
if ('Credential' -like $param -and $CommandName -like $command) {
$cred = $PSDefaultParameterValues.$key
}
}
}
if (-not $cred) {
return
}
$restSplat = New-OPBasicAuthRestObject -Credential $cred
$stashUri = 'http://bitbucket.expertslive.local:7990/rest/api/latest'
(Invoke-RestMethod @restSplat -Uri $stashUri/projects/EL/repos).values |
Where-Object { $_.slug -like "$WordToComplete*" } | ForEach-Object {
[System.Management.Automation.CompletionResult]::new(
$_.slug,
$_.slug,
[System.Management.Automation.CompletionResultType]::ParameterValue,
"$($_.name) in $($_.project.name)"
)
}
}
Register-ArgumentCompleter -CommandName New-OPGitPullRequest -ParameterName SourceBranch -ScriptBlock {
param (
[String]$CommandName,
[String]$ParameterName,
[String]$WordToComplete,
[System.Management.Automation.Language.CommandAst]$CommandAst,
[System.Collections.IDictionary]$FakeBoundParameters
)
$repo = $FakeBoundParameters['Repository']
$cred = $FakeBoundParameters['Credential']
if (-not $cred -or -not $repo) {
foreach ($key in $PSDefaultParameterValues.Keys) {
$command, $param = $key.Split(':')
if ($CommandName -like $command) {
if ('Credential' -like $param) {
$cred = $PSDefaultParameterValues.$key
} elseif ('Repository' -like $param) {
$repo = $PSDefaultParameterValues.$key
}
}
}
}
if (-not $cred -or -not $repo) {
return
}
$restSplat = New-OPBasicAuthRestObject -Credential $cred
$stashUri = 'http://bitbucket.expertslive.local:7990/rest/api/latest'
(Invoke-RestMethod @restSplat -Uri $stashUri/projects/EL/repos/$repo/branches).values |
Where-Object { $_.displayId -like "$WordToComplete*" } | ForEach-Object {
[System.Management.Automation.CompletionResult]::new(
$_.displayId,
$_.displayId,
[System.Management.Automation.CompletionResultType]::ParameterValue,
"$($_.displayId) in $repo"
)
}
}