cloudflare/cloudflared
Publicmirrored from https://github.com/cloudflare/cloudflaredAvailable
.ci/scripts/windows/go-wrapper.ps1
69lines · modecode
| 1 | Param( |
| 2 | [string]$GoVersion, |
| 3 | [string]$ScriptToExecute |
| 4 | ) |
| 5 | |
| 6 | # The script is a wrapper that downloads a specific version |
| 7 | # of go, adds it to the PATH and executes a script with that go |
| 8 | # version in the path. |
| 9 | |
| 10 | Set-StrictMode -Version Latest |
| 11 | $ErrorActionPreference = "Stop" |
| 12 | $ProgressPreference = "SilentlyContinue" |
| 13 | |
| 14 | # Get the path to the system's temporary directory. |
| 15 | $tempPath = [System.IO.Path]::GetTempPath() |
| 16 | |
| 17 | # Create a unique name for the new temporary folder. |
| 18 | $folderName = "go_" + (Get-Random) |
| 19 | |
| 20 | # Join the temp path and the new folder name to create the full path. |
| 21 | $fullPath = Join-Path -Path $tempPath -ChildPath $folderName |
| 22 | |
| 23 | # Store the current value of PATH environment variable. |
| 24 | $oldPath = $env:Path |
| 25 | |
| 26 | # Use a try...finally block to ensure the temporrary folder and PATH are cleaned up. |
| 27 | try { |
| 28 | # Create the temporary folder. |
| 29 | Write-Host "Creating temporary folder at: $fullPath" |
| 30 | $newTempFolder = New-Item -ItemType Directory -Path $fullPath -Force |
| 31 | |
| 32 | # Download go |
| 33 | $url = "https://go.dev/dl/$GoVersion.windows-amd64.zip" |
| 34 | $destinationFile = Join-Path -Path $newTempFolder.FullName -ChildPath "go$GoVersion.windows-amd64.zip" |
| 35 | Write-Host "Downloading go from: $url" |
| 36 | Invoke-WebRequest -Uri $url -OutFile $destinationFile |
| 37 | Write-Host "File downloaded to: $destinationFile" |
| 38 | |
| 39 | # Unzip the downloaded file. |
| 40 | Write-Host "Unzipping the file..." |
| 41 | Expand-Archive -Path $destinationFile -DestinationPath $newTempFolder.FullName -Force |
| 42 | Write-Host "File unzipped successfully." |
| 43 | |
| 44 | # Define the go/bin path wich is inside the temporary folder |
| 45 | $goBinPath = Join-Path -Path $fullPath -ChildPath "go\bin" |
| 46 | |
| 47 | # Add the go/bin path to the PATH environment variable. |
| 48 | $env:Path = "$goBinPath;$($env:Path)" |
| 49 | Write-Host "Added $goBinPath to the environment PATH." |
| 50 | |
| 51 | go env |
| 52 | go version |
| 53 | |
| 54 | & $ScriptToExecute |
| 55 | } finally { |
| 56 | # Cleanup: Remove the path from the environment variable and then the temporary folder. |
| 57 | Write-Host "Starting cleanup..." |
| 58 | |
| 59 | $env:Path = $oldPath |
| 60 | Write-Host "Reverted changes in the environment PATH." |
| 61 | |
| 62 | # Remove the temporary folder and its contents. |
| 63 | if (Test-Path -Path $fullPath) { |
| 64 | Remove-Item -Path $fullPath -Recurse -Force |
| 65 | Write-Host "Temporary folder and its contents have been removed." |
| 66 | } else { |
| 67 | Write-Host "Temporary folder does not exist, no cleanup needed." |
| 68 | } |
| 69 | } |
| 70 | |