Public/Invoke-Terrafun.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
Function Invoke-Terrafun {
    Param(
    )
        
    # load configuration
    $Config = [TerrafunConfig]::new()
    $Config.ReadConfigFile()
    
    # check for new releases
    $Timespan = New-TimeSpan -Start $Config.LastUpdateCheck -End (Get-Date)
    if ($Timespan.TotalHours -gt 1) {
        $Config.AvailableVersions = Get-TerraformVersions
        $Config.LastUpdateCheck = Get-Date
        $Config.WriteConfigFile()
    }

    # parse cmdline args for version selection
    $Opts = Resolve-Options -Arguments $args

    if ($Opts.versionIsPresent -and $Opts.ListIsPresent){
        Get-TerraformVersions | Write-Verbose -Verbose
        Return
    }

    if ($Opts.versionIsPresent -and $Opts.versionNumberIsValid){
        $ShouldExecuteTerraform = $false
        if ($ArgOptions.globalVersion){
            $Config.DesiredVersion = $Opts.versionNumber
            $Config.WriteConfigFile()
        }
        else{
            Set-Content -Path ".terraform-version" -Value $Opts.versionNumber -NoNewline
        }
    }

    # resolve which version of terraform to use
    $Version = Resolve-Version -Config $Config
    
    # install required terraform version
    $Environment = Install-TerraformVersion -Config $Config -Version $Version.DesiredVersion
    Write-Verbose -Verbose ("terrafun: platform = {0}, arch = {1}, version_source = {3}, version = {2}" -f $Environment.Platform, $Environment.Arch, $Version.DesiredVersion, $Version.Source)

    # if we are only managing versions then exit now
    if ($ShouldExecuteTerraform -eq $false){
        Return
    }

    # execute terraform
    & $Environment.BinaryPath $args
    
}