psvagrant.psm1

function Get-VagEnvironmentHome {
    <#
    .SYNOPSIS
    Gets the Vagrant home path.
    .DESCRIPTION
    Gets the Vagrant home path using the VAGRANT_HOME environment variable or
    the default Vagrant home path.
 
    Note the function will return the path without checking it exists.
    .EXAMPLE
    Get-VagEnvironmentHome
 
    Returns the current Vagrant home path.
    .NOTES
    Author: Paul Broadwith (https://github.com/pauby)
    History: 1.0 - Initial release
    .LINK
    https://github.com/pauby/psvagrant/tree/master/docs/Get-VagEnvironmentHome.md
    #>

    [OutputType([String])]
    [CmdletBinding()]
    Param ()

    # the Vagrant home can be set in the VAGRANT_HOME environment variable and
    # if not set uses the default ~/.vagrant.d
    if ([string]::IsNullOrEmpty($env:VAGRANT_HOME)) {
        $home = Join-Path -Path $env:USERPROFILE -ChildPath '.vagrant.d'
    }
    else {
        $home = $env:VAGRANT_HOME
    }

    $home
}

function Get-VagGlobalStatus {
    [CmdletBinding()]
    Param ()
    # this is the file which stores the global index - the path is relative to
    # the Vagrant home.
    $vagIndexPath = Get-VagEnvironmentHome
    Write-Verbose "Using Vagrant home path '$vagIndexPath'"

    "data", "machine-index", "index" | ForEach-Object {
        $vagIndexPath = Join-Path -Path $vagIndexPath -ChildPath $_
    }
    Write-Verbose "Using Vagrant global index status path '$vagIndexPath'"

    if (Test-Path -Path $vagIndexPath -PathType Leaf) {
        # the index file is simply JSON so lets convert it
        $vagMachine = Get-Content -Path $vagIndexPath | ConvertFrom-Json
        # - get a list of the machine names (contained in
        # machines.psobject.properties.name)
        # - use that machine name list to access the underlying machine objects
        # and add the machine name as the id to that object
        # - return the machine object
        $status = $vagmachine.machines.PSObject.Properties | ForEach-Object {
            $obj = New-Object -TypeName PSObject
            $vagmachine.machines.($_.name).psobject.properties | ForEach-Object {
                $obj | Add-Member -MemberType NoteProperty -Name $_.name -value $_.value
            }
            $obj | Add-Member -MemberType NoteProperty -Name 'id' -Value $_.name -PassThru
        }

        Write-Verbose "Found $(@($status).count) running Vagrant machines."
    }
    else {
        Write-Verbose 'Found no running Vagrant machines.'
        $status = $null
    }

    $status
}

function Get-VagLocalStatus {
    [CmdletBinding()]
    Param ()

    Get-VagGlobalStatus | Where-Object { $_.vagrantfile_path -eq (Get-Location).Path.Replace('\', '/')}
}

function Write-VagStatus {
    $globalStatus = Get-VagGlobalStatus
    if ($globalStatus) {
        Write-Host '[' -NoNewline
        Write-Host "^$(@($globalStatus | Where-Object state -eq 'running').count)" -ForegroundColor Green -NoNewline
        $saved = @($globalStatus | Where-Object state -eq 'saved').count
        if ($saved -gt 0) {
            Write-Host " ~$saved" -ForegroundColor Magenta -NoNewline
        }

        $localStatus = Get-VagLocalStatus
        if ($localStatus) {
            Write-Host " >$(@($localStatus).count)" -ForegroundColor Cyan -NoNewline
        }
        Write-Host ']' -NoNewline
    }
}