Images.psm1

#region Functions
#region Get-DockerImage
function Get-DockerImage
{
    [CmdletBinding()]
    param (
        [Parameter()]
        [String]
        $ID
    )

    Begin
    {
        # Check if docker is installed and running
        try {
            Check-Docker
        }
        catch {
            throw "Could not find docker."
        }
    }

    Process
    {
        #region Get the running containers

        # Form the arguments
        $arguments = @("image", "ls", "--format", '"{{json .}}"')

        $pinfo = New-Object System.Diagnostics.ProcessStartInfo
        $pinfo.FileName = "docker"
        $pinfo.RedirectStandardError = $true
        $pinfo.RedirectStandardOutput = $true
        $pinfo.CreateNoWindow = $true
        $pinfo.UseShellExecute = $false
        $pinfo.Arguments = $arguments
        $p = New-Object System.Diagnostics.Process
        $p.StartInfo = $pinfo
        $p.Start() | Out-Null

        # Get the output
        [string]$stdout = "";
        [string]$stderr = "";
        while ( !$p.HasExited ) {
            $stdout += $p.StandardOutput.ReadToEnd();
            $stderr += $p.StandardError.ReadToEnd();
        }

        #Write-Verbose $stdout
        #endregion

        # Check if there were any errors
        if($p.ExitCode -ne 0)
        {
            Write-Error $stderr
            return
        }

        # Convert the output to objects
        $data = "[" + $stdout.Replace("}`n{", "}, {") + "]" | ConvertFrom-Json

        #region Filter out any unwanted images
        if($ID)
        {
            foreach($d in $data)
            {
                if($ID -eq $d.ID)
                {
                    Write-Output $d
                }
            }
        }
        else
        {
            Write-Output $data    
        }
        #endregion
    }

    End {}
}
#endregion

#region Remove-DockerImage
function Remove-DockerImage
{
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
        [Alias("Names")]
        [String]
        $ID,

        [Parameter()]
        [Switch]
        $Force
    )

    Begin
    {
        # Check if docker is installed and running
        try {
            Check-Docker
        }
        catch {
            throw "Could not find docker."
        }
    }

    Process
    {
        foreach($i in $ID)
        {
            #region Stop the container

            # Form the arguments
            Write-Verbose "Removing image $i"
            $arguments = @("image", "rm", $i)
            if($Force)
            {
                $arguments += "--force"
            }
            Write-Verbose ("Executing: " + ($arguments -join " "))

            # Execute the command
            $pinfo = New-Object System.Diagnostics.ProcessStartInfo
            $pinfo.FileName = "docker"
            $pinfo.RedirectStandardError = $true
            $pinfo.RedirectStandardOutput = $true
            $pinfo.CreateNoWindow = $true
            $pinfo.UseShellExecute = $false
            $pinfo.Arguments = $arguments
            $p = New-Object System.Diagnostics.Process
            $p.StartInfo = $pinfo
            $p.Start() | Out-Null

            # Get the output
            [string]$stdout = "";
            [string]$stderr = "";
            while ( !$p.HasExited ) {
                $stdout += $p.StandardOutput.ReadToEnd();
                $stderr += $p.StandardError.ReadToEnd();
            }

            #Write-Verbose $stdout
            #endregion

            # Check if there were any errors
            if($p.ExitCode -ne 0)
            {
                Write-Error $stderr
                return
            }
            else
            {
                Write-Verbose "Image $i removed."    
            }
        }
    }

    End {}
}
#endregion

#region Helper Functions
function Check-Docker
{
    try {
        $pinfo = New-Object System.Diagnostics.ProcessStartInfo
        $pinfo.FileName = "docker.exe"
        $pinfo.RedirectStandardError = $true
        $pinfo.RedirectStandardOutput = $true
        $pinfo.CreateNoWindow = $true
        $pinfo.UseShellExecute = $false
        $pinfo.Arguments = @("--version")
        $p = New-Object System.Diagnostics.Process
        $p.StartInfo = $pinfo
        $p.Start() | Out-Null

        <#
        $p.WaitForExit()
        $stdout = $p.StandardOutput.ReadToEnd()
        $stderr = $p.StandardError.ReadToEnd()
        #>

        [string]$stdout = "";
        while ( !$p.HasExited ) {
            $stdout += $p.StandardOutput.ReadToEnd();
        }
        [string]$stderr = "";
        while ( !$p.HasExited ) {
            $stderr += $p.StandardError.ReadToEnd();
        }

    }
    catch {
        throw "Could not find docker."
    }
}
#endregion
#endregion

#region Exports
Export-ModuleMember -Function "Get-DockerImage"
Export-ModuleMember -Function "Remove-DockerImage"
#endregion