Skip to content

Instantly share code, notes, and snippets.

@ecnepsnai
Last active January 16, 2024 21:07
Show Gist options
  • Save ecnepsnai/656115a4c6631f4565e4337e61880c45 to your computer and use it in GitHub Desktop.
Save ecnepsnai/656115a4c6631f4565e4337e61880c45 to your computer and use it in GitHub Desktop.
Install or Update Go on Windows
function Get-LatestRelease() {
$response = Invoke-WebRequest "https://go.dev/dl/?mode=json"
$results = ConvertFrom-JSON $response
$latest = $results[0]
$arch = "amd64"
if ($env:PROCESSOR_ARCHITECTURE -eq "ARM64") {
$arch = "arm64"
}
foreach ($file in $latest.files) {
if ($file.os -eq "windows" -and $file.arch -eq $arch -and $file.kind -eq "archive") {
return $file
}
}
return $null
}
function Get-CurrentVersion() {
try {
$versionOut = go.exe version
$currentVersion = ($versionOut -split " ")[2]
return $currentVersion
} catch {
return "go0.0.0"
}
}
$latestRelease = Get-LatestRelease
if ($null -eq $latestRelease) {
Write-Error "No release found!"
return
}
$currentVersion = Get-CurrentVersion
if ($currentVersion -eq $latestRelease.version) {
Write-Host "Go $($latestRelease.version) installed is latest"
return
}
Write-Host "Updating go to version $($latestRelease.version)"
New-Item -Path "C:\go\$($latestRelease.version)_temp" -ItemType Directory | Out-Null
$downloadFilePath = "C:\go\$($latestRelease.version)_temp\go.zip"
$downloaderURL = "https://dl.google.com/go/$($latestRelease.filename)"
Write-Host "Downloading $downloaderURL to $downloadFilePath..."
Invoke-WebRequest -Uri $downloaderURL -OutFile $downloadFilePath
$fileHash = $(Get-FileHash -Path $downloadFilePath -Algorithm SHA256).Hash.ToLower()
if ($fileHash -ne $latestRelease.sha256) {
Write-Error "Checksum validation failed!"
return
}
Expand-Archive -Path $downloadFilePath -DestinationPath "C:\go\$($latestRelease.version)_temp"
Move-Item -Path "C:\go\$($latestRelease.version)_temp\go" -Destination "C:\go\$($latestRelease.version)"
Remove-Item -Path "C:\go\$($latestRelease.version)_temp" -Recurse
Remove-Item -Path "C:\go\current" -ErrorAction SilentlyContinue
New-Item -ItemType SymbolicLink -Path "C:\go\current" -Value "C:\go\$($latestRelease.version)" | Out-Null
go.exe version
function Get-LatestRelease() {
$response = Invoke-WebRequest "https://api.github.com/repos/nodejs/node/releases"
$releases = ConvertFrom-JSON $response
$latest_release = $null
foreach ($release in $releases) {
if ($release.name.Contains('(Current)')) {
$latest_release = $release
break
}
}
return $latest_release
}
$release = Get-LatestRelease
if ($null -eq $release) {
Write-Error "No release found!"
return
}
$current_version = node.exe --version
if ($release.tag_name -eq $current_version) {
Write-Host "No update available"
return
}
$downloaderURL = "https://nodejs.org/dist/$($release.tag_name)/node-$($release.tag_name)-x64.msi"
$downloadPath = "$($env:TEMP)\node-$($release.tag_name)-x64.msi"
Write-Host "Downloading $($downloaderURL) to $($downloadPath)..."
Invoke-WebRequest -Uri $downloaderURL -OutFile $downloadPath
$msiexecArguments = "/i",$downloadPath,"/passive"
Write-Host "Running: msiexec.exe $($msiexecArguments)"
Start-Process msiexec.exe -Wait -Verb RunAs -ArgumentList $msiexecArguments
Remove-Item $downloadPath
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment