| 1 |
trap { |
| 2 |
write-error $_ |
| 3 |
exit 1 |
| 4 |
} |
| 5 |
|
| 6 |
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 |
| 7 |
Invoke-WebRequest 'https://github.com/stedolan/jq/releases/download/jq-1.5/jq-win64.exe' -OutFile jq.exe |
| 8 |
Invoke-WebRequest 'https://golang.org/dl/?mode=json' -OutFile golang.json |
| 9 |
|
| 10 |
$GO_VERSION = $(./jq.exe -r 'map(select(.stable and (.version | split(""".""")[0] == """go1"""))) | .[0].files[] | select(.os == """windows""" and .arch == """amd64""" and .kind == """installer""").version' ./golang.json) |
| 11 |
|
| 12 |
Write-Host "Checking if Go needs to be installed or updated..." |
| 13 |
if ((Get-Command 'go.exe' -ErrorAction SilentlyContinue) -ne $null) { |
| 14 |
$version = "$(go.exe version)" |
| 15 |
if ($version -match "go version $GO_VERSION windows\/amd64") { |
| 16 |
Write-Host "Golang $GO_VERSION already installed, skipping download." |
| 17 |
exit 0 |
| 18 |
} |
| 19 |
} |
| 20 |
|
| 21 |
Write-Host "Installing $GO_VERSION" |
| 22 |
|
| 23 |
Invoke-WebRequest "https://storage.googleapis.com/golang/$GO_VERSION.windows-amd64.msi" ` -UseBasicParsing -OutFile go.msi |
| 24 |
|
| 25 |
$p = Start-Process -FilePath "msiexec" ` -ArgumentList "/passive /norestart /i go.msi" ` -Wait -PassThru |
| 26 |
|
| 27 |
if ($p.ExitCode -ne 0) { |
| 28 |
throw "Golang MSI installation process returned error code: $($p.ExitCode)" |
| 29 |
} |
| 30 |
|
| 31 |
Write-Host "Successfully installed go version: $(go version)" |