A lot of new stuff

Docker and Docker-Compose commands and aliases:
- dco: docker-compose
- dcc: docker-compose config
- dcedit/vidc/Edit-DockerCompose:
    edit docker-compose.yml and satellite files
- dcdown.ps1
- dl/dll/dcl/dcll:
    view docker/docker-compose logs
    l : follow
    ll : just list
- dx/dcx: docker / docker-compose exec
- dcdown: docker-compose down
- dcup: docker-compose up
- dcr: docker-compose run
- dcre: docker-compose restart
- dcreup: docker-compose up with force recreate
- di: docker inspect

A bunch of other aliases and tools:
- l/ll - shortcuts for ls -la
- Edit-MyConfig with MyConfig.class: Edit various configuration files
- Update-ArchOSz: my common update params for Arch all ready to run
- Update-UbuntuOSz: my common update params for Ubuntu all ready to run
- sz-df - a ps stylized df
- sz-du - what's the size of a dir
- sys.Linux/ls - a Linux specific ls (with all required configuration)

Works in progress (not completely tested yet):
- Stop-ProcessTree
- Update-OSz.ps1
- src/smartsudo.inc.ps1
This commit is contained in:
lksz 2020-10-31 17:10:32 -04:00
parent 306e03874d
commit 310103c2f7
29 changed files with 226 additions and 8 deletions

1
Aliases/dcedit.ps1 Normal file
View File

@ -0,0 +1 @@
Edit-DockerCompose

1
Aliases/dco.ps1 Normal file
View File

@ -0,0 +1 @@
docker-compose

1
Aliases/l.ps1 Normal file
View File

@ -0,0 +1 @@
ls

1
Aliases/ll.ps1 Normal file
View File

@ -0,0 +1 @@
ls

1
Aliases/vidc.ps1 Normal file
View File

@ -0,0 +1 @@
Edit-DockerCompose

14
Edit-DockerCompose.ps1 Normal file
View File

@ -0,0 +1,14 @@
[CmdletBinding(SupportsShouldProcess)]param([string[]]$ProjectPath=@($PWD))
$local:editCandidates = @('docker-compose.yml','.base.docker-compose.yml')
$local:editFiles = @()
foreach( $local:fileName in $EditCandidates ) {
foreach( $local:path in $ProjectPath ) {
$local:testPath = Join-Path $path $fileName
if( Test-Path $testPath ) { $editFiles += $testPath }
}
}
Edit-TextFile $editFiles
docker-compose config -q

21
Edit-MyConfig.ps1 Normal file
View File

@ -0,0 +1,21 @@
[CmdletBinding(SupportsShouldProcess)]param(
[Parameter(Position = 0, ValueFromRemainingArguments)]
[ArgumentCompleter({ param (
$commandName,
$parameterName,
$wordToComplete,
$commandAst,
$fakeBoundParameters
)
[MyConfig]::_GetValidValues($wordToComplete,$true) | Sort-Object
})]
[string[]]$ConfigName,
[switch]$Force
)
if( -not $ConfigName ) {
([MyConfig]::_GetValidValues('',$true)) | Sort-Object
return
}
$local:ScriptPaths = [MyConfig]::GetConfigPaths($ConfigName,$Force)
Edit-TextFile $ScriptPaths

13
Stop-ProcessTree.ps1 Normal file
View File

@ -0,0 +1,13 @@
[cmdletbinding()]param([string]$ProcessName)
$local:new_KillPIDs = @() + (Get-Process -name $processName | Select-Object -ExpandProperty ID)
$local:KillPIDs = @()
Do {
$KillPIDs += $new_KillPIDs | Where-Object { $_ -notin $KillPIDs }
Write-Verbose "Again"
Write-Verbose $new_KillPIDs -join ", "
$new_KillPIDs = $new_KillPIDs | ForEach-Object { Get-CimInstance -ClassName Win32_Process -Filter "ParentProcessId = $_" } | Select-Object -ExpandProperty ProcessId
} Until ( -not $new_KillPIDs )
Write-Verbose "Done"
start-process powershell -ArgumentList "-command","Stop-Process -Force -Id @($($KillPIDs -join '', ''))" -WindowStyle Minimized

13
Update-ArchOSz.ps1 Normal file
View File

@ -0,0 +1,13 @@
param(
[ValidateSet('List','Update','Auto','All')]
[string]$Mode = 'List'
)
$script:yayCli = "cat /run/check.yay.updates/list"
switch( $Mode ){
'Update' { $yayCli = "yay -Syu --needed --ignore docker,linux,linux-api-headers,linux-firmware,linux-headers,zfs-linux" }
'Auto' { $yayCli = "yay -Syu --needed --noconfirm --ignore docker || yay -Syu --needed --ignore docker,linux,linux-api-headers,linux-firmware,linux-headers,zfs-linux --noconfirm" }
'All' { $yayCli = "yay -Syu --needed" }
}
& sh "-c" "$yayCli" -replace '#args',"$args"

10
Update-OSz.ps1 Normal file
View File

@ -0,0 +1,10 @@
param(
[ValidateSet('List','Update','Auto','All')]
[string]$Mode = 'List'
)
$local:detectedDisistro = cat /etc/os-release | Select-String -Pattern "^ID=" | ForEach-Object { $_ -split '=' | Select-Object -Skip 1 }
switch ($detectedDisistro) {
'arch' { Update-ArchOSz -Mode:$Mode $args }
'ubuntu' { Update-UbuntuOSz -Mode:$Mode $args }
}

14
Update-UbuntuOSz.ps1 Normal file
View File

@ -0,0 +1,14 @@
param(
[ValidateSet('List','Update','Auto','All')]
[string]$Mode = 'List'
)
$script:aptCli = "echo 'not defined'"
switch( $Mode ){
'List' { }
'Auto' { $aptCli = "sudo apt-get update && sudo apt-get upgrade --auto-remove --assume-yes" }
default { $aptCli = "sudo apt-get update && sudo apt-get upgrade --auto-remove " }
}
& sh "-c" "$aptCli" -replace '#args',"$args"

1
dcc.ps1 Normal file
View File

@ -0,0 +1 @@
docker-compose config | less

1
dcdown.ps1 Normal file
View File

@ -0,0 +1 @@
docker-compose down --timeout=3 --volumes --remove-orphans "$args"

1
dcl.ps1 Normal file
View File

@ -0,0 +1 @@
docker-compose logs --tail=40 --follow "$args"

1
dcll.ps1 Normal file
View File

@ -0,0 +1 @@
docker-compose logs "$args"

1
dcr.ps1 Normal file
View File

@ -0,0 +1 @@
docker-compose run --rm "$args"

1
dcre.ps1 Normal file
View File

@ -0,0 +1 @@
docker-compose restart "$args"

11
dcreup.ps1 Normal file
View File

@ -0,0 +1,11 @@
#[CmdletBinding(SupportsShouldProcess)]param()
$local:dcParams = [string]::Empty
if( $args ) {
$dcParams = $args | ForEach-Object {
if( $_ -match "(?:^'[^']+'$)|(?:^`"[^`"]+`"$)|(?:^[^\s]+$)" ) { $_ }
else { "'$($_.Replace( "'", "''" ))'" }
}
}
docker-compose up -d --force-recreate --remove-orphans $dcParams && docker-compose logs --follow --tail 10 $dcParams

10
dcup.ps1 Normal file
View File

@ -0,0 +1,10 @@
#[CmdletBinding(SupportsShouldProcess)]param()
$local:dcParams = [string]::Empty
if( $args ) {
$dcParams = $args | ForEach-Object {
if( $_ -match "(?:^'[^']+'$)|(?:^`"[^`"]+`"$)|(?:^[^\s]+$)" ) { $_ }
else { "'$($_.Replace( "'", "''" ))'" }
}
}
docker-compose up -d --remove-orphans $dcParams && docker-compose logs --follow --tail 10 $dcParams

1
dcx.ps1 Normal file
View File

@ -0,0 +1 @@
docker-compose exec "$args"

1
di.ps1 Normal file
View File

@ -0,0 +1 @@
docker inspect "$args"

1
dl.ps1 Normal file
View File

@ -0,0 +1 @@
docker logs --tail=40 --follow "$args"

1
dll.ps1 Normal file
View File

@ -0,0 +1 @@
docker logs "$args"

1
dx.ps1 Normal file
View File

@ -0,0 +1 @@
docker exec "$args"

View File

@ -1,13 +1,54 @@
class MyConfig { #: System.Management.Automation.IValidateSetValuesGenerator { class MyConfig { #: System.Management.Automation.IValidateSetValuesGenerator {
static [hashtable]$configDirectory = [ordered]@{ static [hashtable]$configDirectory = @{};
'vi' = '~/.virc' static ReloadConfigPaths() {
'vim' = '~/.vimrc','~/.vim/vimrc' [MyConfig]::ConfigDirectory = [ordered]@{
'neovim' = '~/.config/nvim/init.vim','~/.config/nvim/vim-plug/plugins.vim','#vim','#vi' 'vi' = '~/.virc'
'vim' = '~/.vimrc','~/.vim/vimrc'
'neovim' = '~/.config/nvim/init.vim','~/.config/nvim/vim-plug/plugins.vim','#vim','#vi'
'zshrc' = '~/.zshrc'
'sz-zshrc' = '~/.sz.zshrc.sh', '#zshrc'
'sz-local-rc' = '~/.sz.local.sh'
'sz-aliases-sh' = '~/.sz.aliases.sh'
'sz-shrc' = '~/.sz.shrc.sh'
'sz-rc-all' = '#sz-aliases-sh', '#sz-local-rc', '#sz-shrc', '#sz-zshrc', '#zshrc'
'tmux' = '~/.tmux.conf', '~/.byobu/.tmux.conf'
}
} }
static [string[]] GetConfigPaths([string[]]$ConfigNames) { static [string[]] GetConfigPaths([string[]]$ConfigNames,[switch]$Force) {
return $null [MyConfig]::ReloadConfigPaths()
$local:result = [string[]]@()
$local:flat = $false
while(-not $flat) {
$flat = $true
$ConfigNames += $ConfigNames | ForEach-Object {
[MyConfig]::configDirectory[$_]
} | Where-Object { $_ -match '^#[\w-\.]+$' } | ForEach-Object {
$_.Substring(1)
} | Where-Object { $_ -notin $ConfigNames } | ForEach-Object {
$flat = false; $_;
}
}
$local:flatConfigList = $ConfigNames | ForEach-Object {
[MyConfig]::configDirectory[$_]
} | Where-Object { $_ -notmatch '^#[\w-\.]+$' }
$local:exists = $false;
$local:first = $null
foreach( $local:configPath in $flatConfigList ) {
if( -not $first ) { $first = $configPath }
if( $Force -or (Test-Path $configPath) ) {
$result += Resolve-Path $configPath
$exists = $true
}
}
if( -not $exists -and -not $Force ) {
$result += $first
}
return $result
} }
static [string[]] _GetValidValues([string]$wordToComplete,[bool]$Strict) { static [string[]] _GetValidValues([string]$wordToComplete,[bool]$Strict) {
[MyConfig]::ReloadConfigPaths()
$local:possibleValues = [MyConfig]::configDirectory.Keys $local:possibleValues = [MyConfig]::configDirectory.Keys
return $(Get-PossibleArguments -WordToComplete $wordToComplete -FullValueSet $possibleValues -Strict:$Strict ); return $(Get-PossibleArguments -WordToComplete $wordToComplete -FullValueSet $possibleValues -Strict:$Strict );
} }

18
src/smartsudo.inc.ps1 Normal file
View File

@ -0,0 +1,18 @@
function smartsudo() {
[CmdletBinding(SupportsShouldProcess)]param(
[switch]$sudo,
[switch]$Force,
[Parameter(Position = 0, ValueFromRemainingArguments = $true)]
[string[]]$expr
)
if( $sudo -and -not $(Test-IsAdmin) ) {
Write-Verbose "Perofrming the following command line via SUDO:`n$($expr -join ' ')"
$local:base64command = ConvertTo-Base64 "$expr"
/usr/bin/env sudo pwsh -EncodedCommand $base64command
return
}
Write-Verbose "Perofrming the following expression in-line:`n$($expr -join ' ')"
Invoke-Expression "$expr"
}

View File

@ -1,4 +1,5 @@
$local:params = $args -join ' ' $local:params = $args -join ' '
if( -not $params ) { $params = '-la' } if( -not $params ) { $params = '-lah' }
/usr/bin/env ls $params
/usr/bin/env ls --color=auto $params

35
sz-df.ps1 Normal file
View File

@ -0,0 +1,35 @@
param([ValidateSet('KB','GB','TB')]$SizeUnitName = 'GB')
$local:zpool_cmd = $null
$local:excludeType = ''
$script:SizeUnit = Invoke-Expression "1$SizeUnitName"
$script:SizeRound = 2
$SizeUnit /= 1KB
function NewDataRow{param($Size,$Used,$Available,$Percent,$Source,$FSType,$Target)
[PSCustomObject]@{
"Size$SizeUnitName" = [Math]::Round($Size /$SizeUnit, $SizeRound)
"Used$SizeUnitName" = [Math]::Round($Used /$SizeUnit, $SizeRound)
"Available$SizeUnitName" = [Math]::Round($Available/$SizeUnit, $SizeRound)
'Used%' = [int]($Percent -replace '%$','')
Source = $Source
FSType = $FSType
Target = $Target
}
}
$local:df = @()
& df '--output=size,used,avail,pcent,source,fstype,target' |
Select-Object -Skip 1 | ForEach-Object {
$local:df = $_.Split(' ', [StringSplitOptions]::RemoveEmptyEntries)
NewDataRow $df[0] $df[1] $df[2] $df[3] $df[4] $df[5] $df[6]
} | Where-Object {
-not ($_.FSType -eq 'zfs' -and $_.Source -match '/')
} | Sort-Object -Property Target
#$zpool_cmd = get-command zpool | Where-Object CommandType -eq 'Application' | Select-Object -ExpandProperty Source
#if( $zpool_cmd ) {
# $excludeType = '--exclude-type=zfs'
#}

1
sz-du.ps1 Normal file
View File

@ -0,0 +1 @@
sudo /bin/du --human-readable --max-depth=1 --one-file-system $args | sort --human-numeric-sort --reverse