PowerShell_Scripts/docker/profile.d/Docker.class.ps1

163 lines
5.6 KiB
PowerShell

class DockerContainers { #: System.Management.Automation.IValidateSetValuesGenerator {
static [string[]] _GetValidValues([string]$wordToComplete,[bool]$Strict) {
$local:possibleValues = $(
docker container ls -a --no-trunc --format "{{.Names}}"
)
return $(Get-PossibleArguments -WordToComplete $wordToComplete -FullValueSet $possibleValues -Strict:$Strict );
}
[String[]] GetValidValues() {
return [DockerContainers]::_GetValidValues('',$true)
}
}
class DockerImages { #: System.Management.Automation.IValidateSetValuesGenerator {
static [string[]] _GetValidValues([string]$wordToComplete,[bool]$Strict) {
$local:possibleValues = $(
docker image ls --format "{{.Repository}}"
)
return $(Get-PossibleArguments -WordToComplete $wordToComplete -FullValueSet $possibleValues -Strict:$Strict );
}
[String[]] GetValidValues() {
return [DockerImages]::_GetValidValues('',$true)
}
}
class DockerNetworks {
static [string[]] _GetValidValues([string]$wordToComplete,[bool]$Strict) {
$local:possibleValues = $(
docker network ls --no-trunc --format "{{.Name}}"
)
return $(Get-PossibleArguments -WordToComplete $wordToComplete -FullValueSet $possibleValues -Strict:$Strict );
}
[String[]] GetValidValues() {
return [DockerNetworks]::_GetValidValues('',$true)
}
}
class DockerComposeCommands { #: System.Management.Automation.IValidateSetValuesGenerator {
static [string[]] $cachedCommands = @()
static [string[]] _GetValidValues([string]$wordToComplete,[string]$subcommand,[bool]$Strict) {
$local:gate = $false
$local:possibleValues = $(
docker-compose $subcommand --help | Out-String -Stream |
ForEach-Object {
if( $wordToComplete -match '^-' ) {
if( $_ -match "^ *(-.*) (\w.*)$" ) {
$matches[1] -split ', ' | ForEach-Object { $_ -split ' ' | Select-Object -First 1 }
}
} elseif( -not $gate ) {
if ( $_.trim() -eq 'Commands:' ) { $gate = $true }
} else {
$null = $_ -match '^\W+(\w+)\W+(.+)$';
$Matches[1];
}
}
)
[DockerComposeCommands]::cachedCommands = $(Get-PossibleArguments -WordToComplete $wordToComplete -FullValueSet $possibleValues -Strict:$Strict );
return [DockerComposeCommands]::cachedCommands;
}
[String[]] GetValidValues() {
return [DockerComposeCommands]::_GetValidValues('',$true)
}
}
$global:_DockerComposeCommandCompleter = [ScriptBlock]{
param(
$commandName,
$parameterName,
$wordToComplete,
$commandAst,
$fakeBoundParameters
)
[DockerComposeCommands]::_GetValidValues($wordToComplete,"",$true)
}
class DockerComposeServices { #: System.Management.Automation.IValidateSetValuesGenerator {
static [string[]] _GetValidValues([string]$wordToComplete,[string]$Path,[bool]$Strict) {
$local:possibleValues = $(
dco -ProjectPath $Path config --services
)
return $(Get-PossibleArguments -WordToComplete $wordToComplete -FullValueSet $possibleValues -Strict:$Strict );
}
static [string[]] _GetValidValues([string]$wordToComplete,[bool]$Strict) {
return [DockerComposeServices]::_GetValidValues($wordToComplete,'.',$true)
}
[String[]] GetValidValues() {
return [DockerComposeServices]::_GetValidValues('',$true)
}
}
$null = [DockerComposeCommands]::_GetValidValues("","",$true)
$global:_DockerComposeCompleter = [ScriptBlock]{
param(
$commandName,
$parameterName,
$wordToComplete,
$commandAst,
$fakeBoundParameters,
$subCommand
)
$local:cmd = $subCommand
if( -not $cmd ) {
$cmd = $commandAst.CommandElements |
Where-Object Value -in ([DockerComposeCommands]::cachedCommands) |
Select-Object -First 1
}
$local:ProjectPath = $fakeBoundParameters.ProjectPath
if( -not $ProjectPath ) { $ProjectPath=@($PWD) }
if( -not $cmd -or $wordToComplete -match '^-' ) {
[DockerComposeCommands]::_GetValidValues($wordToComplete,$cmd,$true)
} else {
switch($cmd){
'down' {}
'images' {}
'config' {}
default {
[DockerComposeServices]::_GetValidValues($wordToComplete,$ProjectPath,$true)
}
}
}
}
class DockerComposeDirs { #: System.Management.Automation.IValidateSetValuesGenerator {
static [string[]] _GetValidValues([string]$wordToComplete,[string]$Path,[bool]$Strict) {
$local:possibleValues = $(
Get-ChildItem -LiteralPath $Path -Directory -Depth 3 |
Where-Object { $_ |
Get-ChildItem -Include 'docker-compose.y*l' |
Where-Object Extension -in '.yml','.yaml'
} | ForEach-Object {
$_.FullName.Replace($(Join-Path $PWD ""),'')
}
)
return $(Get-PossibleArguments -WordToComplete $wordToComplete -FullValueSet $possibleValues -Strict:$Strict );
}
static [string[]] _GetValidValues([string]$wordToComplete,[bool]$Strict) {
return [DockerComposeDirs]::_GetValidValues($wordToComplete,'.',$true)
}
[String[]] GetValidValues() {
return [DockerComposeDirs]::_GetValidValues('',$true)
}
}
$global:_DockerComposeDirsCompleter = [ScriptBlock]{
param(
$commandName,
$parameterName,
$wordToComplete,
$commandAst,
$fakeBoundParameters
)
[DockerComposeDirs]::_GetValidValues($wordToComplete,$true)
}