Skip to content

Instantly share code, notes, and snippets.

@stuartleeks
Last active October 31, 2019 18:52
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save stuartleeks/4661c3c05678eec822bc to your computer and use it in GitHub Desktop.
Save stuartleeks/4661c3c05678eec822bc to your computer and use it in GitHub Desktop.
ConvertFrom-Docker
function PascalName($name){
$parts = $name.Split(" ")
for($i = 0 ; $i -lt $parts.Length ; $i++){
$parts[$i] = [char]::ToUpper($parts[$i][0]) + $parts[$i].SubString(1).ToLower();
}
$parts -join ""
}
function GetHeaderBreak($headerRow, $startPoint=0){
$i = $startPoint
while( $i + 1 -lt $headerRow.Length)
{
if ($headerRow[$i] -eq ' ' -and $headerRow[$i+1] -eq ' '){
return $i
break
}
$i += 1
}
return -1
}
function GetHeaderNonBreak($headerRow, $startPoint=0){
$i = $startPoint
while( $i + 1 -lt $headerRow.Length)
{
if ($headerRow[$i] -ne ' '){
return $i
break
}
$i += 1
}
return -1
}
function GetColumnInfo($headerRow){
$lastIndex = 0
$i = 0
while ($i -lt $headerRow.Length){
$i = GetHeaderBreak $headerRow $lastIndex
if ($i -lt 0){
$name = $headerRow.Substring($lastIndex)
New-Object PSObject -Property @{ HeaderName = $name; Name = PascalName $name; Start=$lastIndex; End=-1}
break
} else {
$name = $headerRow.Substring($lastIndex, $i-$lastIndex)
$temp = $lastIndex
$lastIndex = GetHeaderNonBreak $headerRow $i
New-Object PSObject -Property @{ HeaderName = $name; Name = PascalName $name; Start=$temp; End=$lastIndex}
}
}
}
function ParseRow($row, $columnInfo) {
$values = @{}
$columnInfo | ForEach-Object {
if ($_.End -lt 0) {
$len = $row.Length - $_.Start
} else {
$len = $_.End - $_.Start
}
$values[$_.Name] = $row.SubString($_.Start, $len).Trim()
}
New-Object PSObject -Property $values
}
function ConvertFrom-Docker(){
begin{
$positions = $null;
}
process {
if($positions -eq $null) {
# header row => determine column positions
$positions = GetColumnInfo -headerRow $_ #-propertyNames $propertyNames
} else {
# data row => output!
ParseRow -row $_ -columnInfo $positions
}
}
end {
}
}
# e.g. :
# docker --tls ps -a --no-trunc | ConvertFrom-Docker | ft
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment