forked from barnuri/powershell-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprofileTools.psm1
323 lines (280 loc) · 12.4 KB
/
profileTools.psm1
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
################# Profile By BarNuri #################
# author : ✡ BarNuri ✡
# git: https://github.com/barnuri/powershell-utils
# symbols - https://coolsymbol.com/
# PSReadLine - https://docs.microsoft.com/en-us/powershell/module/psreadline/set-psreadlinekeyhandler?view=powershell-7.2
$parentDir = $(Split-Path $profile)
$profileTools = Join-Path -Path $parentDir -ChildPath "profileTools.psm1"
function profileTools() {
echo $profileTools
}
function syncPowershellUtils() {
mkdir -p (Split-Path -Path $profile -Parent) -errorAction SilentlyContinue
$date = $(Get-Date).ToString()
$newProfileContent = $(curl https://raw.githubusercontent.com/barnuri/powershell-utils/master/profileTools.psm1?noCache=$date -H "Cache-Control: no-cache, no-store, must-revalidate"-H "Pragma: no-cache")
echo $newProfileContent > $profileTools
$installString = "### load profileTools.psm1"
$importModuleExists = Select-String -Quiet -Pattern $installString -Path $profile
if (-not $importModuleExists)
{
echo $installString >> $profile
echo "`$SaveVerbosePreference = `$global:VerbosePreference;" >> $profile
echo "`$global:VerbosePreference = 'SilentlyContinue';" >> $profile
echo "Import-Module $profileTools -Force -DisableNameChecking" >> $profile
echo "`$global:VerbosePreference = `$SaveVerbosePreference;" >> $profile
}
$SaveVerbosePreference = $global:VerbosePreference;
$global:VerbosePreference = 'SilentlyContinue';
Import-Module $profileTools -Force -DisableNameChecking
$global:VerbosePreference = $SaveVerbosePreference;
}
function reloadProfile() {
. $profile
$SaveVerbosePreference = $global:VerbosePreference;
$global:VerbosePreference = 'SilentlyContinue';
Import-Module $profileTools -Force -DisableNameChecking
$global:VerbosePreference = $SaveVerbosePreference;
}
function prompt {
$host.ui.RawUI.WindowTitle = "Current Folder: $pwd"
$CmdPromptUser = [Security.Principal.WindowsIdentity]::GetCurrent();
$IsAdmin = (New-Object Security.Principal.WindowsPrincipal ([Security.Principal.WindowsIdentity]::GetCurrent())).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
Write-host ($(if ($IsAdmin) { '[Admin]' } else { '' })) -BackgroundColor DarkBlue -ForegroundColor White -NoNewline
Write-Host " $($CmdPromptUser.Name.split("\")[1]) " -BackgroundColor DarkBlue -ForegroundColor White -NoNewline
Write-Host " $pwd" -NoNewline
## Git Status
if (Test-Path -Path ".git") {
$longStatus=$(git --no-optional-locks status)
$status=$(git --no-optional-locks status --short --ahead-behind --branch)
$lines=$status.Split([Environment]::NewLine)
$firstLine, $statusLines = $lines
$statusLines += ""
$firstLine -match '## (.+)' | out-null
$isRemoteBranch = $Matches[1].Split(".").Count -gt 1
$branch=$Matches[1].Split(".")[0]
'' -match '' | out-null # reset regex result
$firstLine -match '\[(?:.*)?(?:behind (\d+))\]' | out-null
$behind=$($Matches[1] ?? 0)
'' -match '' | out-null # reset regex result
$firstLine -match '\[(?:ahead (\d+)).*\]' | out-null
$ahead=$($Matches[1] ?? 0)
'' -match '' | out-null # reset regex result
Write-Host " [" -NoNewLine -ForeGroundColor Yellow
Write-Host "$branch" -NoNewLine -ForeGroundColor Cyan
$dontHaveCommitedFiles = ($($longStatus.ToLower().Split([Environment]::NewLine) | where { $_.StartsWith("no changes added to commit") }).Count + $($longStatus.ToLower().Split([Environment]::NewLine) | where { $_.StartsWith("nothing added to commit") }).Count) -gt 0
$statusLines = $statusLines | foreach { If ($dontHaveCommitedFiles) { $_.Trim() } Else { $_ } }
$deleted=$($statusLines | where { $_.StartsWith("D ") }).Count
$modify=$($statusLines | where { $_.StartsWith("M ") -OR $_.StartsWith("T ") -OR $_.StartsWith("R ") -OR $_.StartsWith("C ") }).Count
$new=$($statusLines | where { $_.StartsWith("A ") }).Count
if($dontHaveCommitedFiles) {
$new=$new+$($statusLines | where { $_.StartsWith("?? ") }).Count + $($statusLines | where { $_.StartsWith("? ") }).Count
}
$mergeConflicts=$($statusLines | where {
$_.StartsWith("UU ") -OR
$_.StartsWith("U ") -OR
$_.StartsWith("AA ") -OR
$_.StartsWith("DD ") -OR
$_.StartsWith("AU ") -OR
$_.StartsWith("UD ") -OR
$_.StartsWith("UA ") -OR
$_.StartsWith("DU ")
}).Count
if(!($isRemoteBranch)) {
Write-Host " ☁ ↑" -ForeGroundColor yellow -NoNewLine
}
if ($isRemoteBranch -and $behind -eq 0 -and $ahead -eq 0 -and $new -eq 0 -and $modify -eq 0 -and $deleted -eq 0 -and $mergeConflicts -eq 0) {
Write-Host " =" -ForeGroundColor Cyan -NoNewLine
}
else {
Write-Host " ↓$behind " -ForeGroundColor Red -NoNewLine
Write-Host "↑$ahead " -ForeGroundColor Cyan -NoNewLine
if ($new -ne 0 -or $modify -ne 0 -or $deleted -ne 0) {
Write-Host "+$new " -ForeGroundColor Green -NoNewLine
Write-Host "±$modify " -ForeGroundColor Cyan -NoNewLine
Write-Host "-$deleted" -ForeGroundColor Red -NoNewLine
}
if ($mergeConflicts -ne 0) {
Write-Host " !$mergeConflicts" -ForeGroundColor Magenta -NoNewLine
}
}
Write-Host "]" -NoNewLine -ForeGroundColor Yellow
}
return " > "
}
Import-Module PSReadLine
Set-PSReadLineOption -Colors @{ InlinePrediction = '#9CA3AF'}
Set-PSReadLineOption -EditMode Windows
Set-PSReadLineOption -PredictionSource HistoryAndPlugin
Set-PSReadlineOption -PredictionViewStyle InlineView
(Get-PSReadLineOption).ShowToolTips = $True
(Get-PSReadLineOption).HistoryNoDuplicates = $True
Set-PSReadlineKeyHandler -Key Ctrl+Spacebar -Function MenuComplete
Set-PSReadlineKeyHandler -Key Tab -Function AcceptNextSuggestionWord
Set-Alias ll dir
############# k8s
Set-Alias k kubectl
function kconf { kubectl config view --raw --flatten --minify }
function kall($appName='') { kubectl get deploy,svc,ingress,pod $appName }
function klog($search) { kubectl logs --tail=100000 -f -l $search }
function klogs($search) { klog $search }
############# ssh
function sshKeyFunc { cat $home\.ssh\id_rsa.pub }
Set-Alias sshkey sshKeyFunc
############# python
function p3venv() { python3 -m virtualenv venv }
function p2venv() { python2 -m virtualenv venv }
# pip install
function pipi() {
python -m pip install --upgrade pip;
pip install --upgrade -r REQUIREMENTS
}
# pip install package
function pipp() {
python -m pip install --upgrade pip;
pip install .
}
############# git
function gitRemoveMergedBranches { git branch --merged | ForEach-Object { $_.Trim() } | Where-Object {$_ -NotMatch "^\*"} | Where-Object {-not ( $_ -Like "*master" )} | ForEach-Object { git branch -d $_ } }
function getAllBranches() { git branch -a -l --format "%(refname:short)" | ForEach-Object { $_.Split("/")[-1] } | Where-Object { $_ -ne "HEAD" } }
function gitCleanLocalBranches() {
git fetch --all --prune ;
git branch -l --format "%(refname:short)" | ForEach-Object { git branch $_.Trim() -D }
}
function gitCleanIgnoreFiles() { git clean -dfx }
Class BranchesNames : System.Management.Automation.IValidateSetValuesGenerator {
[String[]] GetValidValues() {
$BranchesNames = $(getAllBranches)
return [String[]] $BranchesNames
}
}
function gitMergeTo([ValidateSet([BranchesNames])] $targetBranchName='integration') {
$currentBranch = $(git branch --show-current)
git checkout $targetBranchName;
git pull ;
git merge -X ignore-all-space --no-ff $currentBranch ;
git push ;
git checkout $currentBranch ;
}
Set-Alias gitmt gitMergeTo
function gitc([ValidateSet([BranchesNames])] $branchName='master') {
git checkout $branchName;
git pull
}
# git new branch
function gitnb($branchName) { git checkout -b $branchName; }
# git merge
function gitm([ValidateSet([BranchesNames])] $branchName='master') {
git fetch origin $branchName;
git pull ;
git merge -X ignore-all-space --no-ff origin/$branchName
}
function gitDiff([ValidateSet([BranchesNames])] $branchName='master') {
git fetch origin $branchName;
git diff origin/$branchName...$(git branch --show-current) --name-status
}
function gitCheckoutFile([ValidateSet([BranchesNames])] $branchName) {
git fetch origin $branchName;
git checkout origin/$branchName -- $args
}
function gitCheckoutFileFromMaster() {
git fetch origin master;
git checkout origin/master -- $args
}
# git commit & push
function gitCommitAndPush() {
$msg = "$args"
$currentBranchName = $(git --no-optional-locks name-rev --name-only HEAD)
$IsRemoteBranch=[bool]$(git --no-optional-locks config branch.$($currentBranchName).merge)
if ($msg -eq "") {
$msg = "$currentBranchName"
}
if(!$IsRemoteBranch) {
git push --set-upstream origin $currentBranchName
}
git add .;
git commit -am $msg;
git pull;
git push;
if(!$IsRemoteBranch) {
Write-Host "$(gitOriginUrl)/pull/new/$currentBranchName" -ForeGroundColor Cyan
}
}
Set-Alias gitp gitCommitAndPush
function gitOriginUrl() {
$repoUrl = $(git config --get remote.origin.url)
if($repoUrl.StartsWith("git@")) {
$repoUrl = $repoUrl.SubString(4)
}
$repoUrl = $repoUrl.Replace(":","/")
if($repoUrl.EndsWith(".git")) {
$repoUrl = $repoUrl.SubString(0, $repoUrl.Length - 4)
}
if(!$repoUrl.StartsWith("http")) {
$repoUrl = "https://$repoUrl"
}
$repoUrl = $repoUrl.Trim("/")
echo "$repoUrl"
}
function gitEmptyCommit() {
git commit --allow-empty -m "empty commit - trigger status checks";
git pull;
git push;
}
function gitSpeedUp() {
$env:GIT_ASK_YESNO="false" ;
git fsck ;
git repack -ad ;
git gc --aggressive --prune=now --force ;
git status ;
}
# general
function HistoryFile() { (Get-PSReadlineOption).HistorySavePath }
Set-Alias hfile HistoryFile
function filesByGlob($glob) {
Get-ChildItem -Filter $glob -Recurse -ErrorAction SilentlyContinue -Force | Select-Object -ExpandProperty FullName
}
function updatePowershell() {
iex "& { $(irm https://aka.ms/install-powershell.ps1) } -UseMSI"
}
function hostsFile() { echo "C:\Windows\System32\drivers\etc\hosts" }
function hostFile() { hostsFile }
function profile() { echo $profile }
function which($search) { $res=$(Get-Command $search -errorAction SilentlyContinue); if($res.Source) { echo $res.Source } else { echo $res } }
function screenClose() { (Add-Type '[DllImport(\"user32.dll\")]^public static extern int PostMessage(int hWnd, int hMsg, int wParam, int lParam);' -Name a -Pas)::PostMessage(-1,0x0112,0xF170,2) }
function watch($command, $secsToSleep = 5) {
while (1) {
clear ;
echo "$(Get-Date)" ;
. $command ;
sleep $secsToSleep ;
}
}
function wslIp($DOCKER_DISTRO = "Ubuntu-20.04") {
echo "$((wsl -d "$DOCKER_DISTRO" sh -c "hostname -I").Split(" ")[0] )"
}
function DockerService($DOCKER_DISTRO = "Ubuntu-20.04") {
$DOCKER_DIR = "/mnt/wsl/shared-docker"
$DOCKER_SOCK = "$DOCKER_DIR/docker.sock"
wsl -d "$DOCKER_DISTRO" sh -c "[ -S '$DOCKER_SOCK' ]"
if ($LASTEXITCODE) {
wsl -d "$DOCKER_DISTRO" sh -c "mkdir -pm o=,ug=rwx $DOCKER_DIR ; chgrp docker $DOCKER_DIR"
wsl -d "$DOCKER_DISTRO" sh -c "nohup sudo -b dockerd < /dev/null > $DOCKER_DIR/dockerd.log 2>&1"
}
}
function wslProxy($DOCKER_DISTRO = "Ubuntu-20.04") {
$env:WSL_HOST = (wsl -d "$DOCKER_DISTRO" sh -c "hostname -I").Split(" ")[0]
$env:DOCKER_HOST = "tcp://$($env:WSL_HOST):2375"
netsh interface portproxy add v4tov4 listenport=2375 connectport=2375 connectaddress=$env:WSL_HOST
}
function wslProxyPort($port, $DOCKER_DISTRO = "Ubuntu-20.04") {
$WSL_HOST = (wsl -d "$DOCKER_DISTRO" sh -c "hostname -I").Split(" ")[0]
netsh interface portproxy add v4tov4 listenport=$port connectport=$port connectaddress=$WSL_HOST
}
function wslProxyPortDelete($port) {
netsh interface portproxy delete v4tov4 listenport=$port
}
function minikubeProxy($DOCKER_DISTRO = "Ubuntu-20.04") {
$(wsl -d "$DOCKER_DISTRO" kubectl config view --raw --flatten --minify) > ~/.kube/config
}
Export-ModuleMember -Function * -Alias * -Variable * -Cmdlet *
################# END Profile By BarNuri #################