function Get-GostmanArch { $name = $null try { $name = [System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture.ToString() } catch { $name = $null } if (-not $name) { $name = if ($env:PROCESSOR_ARCHITEW6432) { $env:PROCESSOR_ARCHITEW6432 } else { $env:PROCESSOR_ARCHITECTURE } } switch -Regex ($name) { '^(X64|AMD64)$' { return 'amd64' } '^(Arm64|ARM64)$' { return 'arm64' } default { throw "No Gostman build for the processor '$name'. Downloads: https://github.com/kunalgoesbyken/gostman/releases" } } } function Get-GostmanExpectedHash { param([string]$SumsPath, [string]$Name) foreach ($line in Get-Content -LiteralPath $SumsPath) { $parts = $line.Trim() -split '\s+', 2 if ($parts.Count -eq 2 -and $parts[1].TrimStart('*') -eq $Name) { return $parts[0].ToLowerInvariant() } } return $null } function Add-GostmanToUserPath { param([string]$Directory, [string]$KeyPath = 'Environment') $root = [Microsoft.Win32.Registry]::CurrentUser $key = $root.OpenSubKey($KeyPath, $true) if ($null -eq $key) { $key = $root.CreateSubKey($KeyPath) } try { $current = [string]$key.GetValue('Path', '', [Microsoft.Win32.RegistryValueOptions]::DoNotExpandEnvironmentNames) $wanted = $Directory.TrimEnd('\') foreach ($entry in $current.Split(';')) { if ($entry -eq '') { continue } if ([Environment]::ExpandEnvironmentVariables($entry).TrimEnd('\') -ieq $wanted) { return $false } } if ($current -eq '') { $joined = $Directory } elseif ($current.EndsWith(';')) { $joined = $current + $Directory } else { $joined = $current + ';' + $Directory } $key.SetValue('Path', $joined, [Microsoft.Win32.RegistryValueKind]::ExpandString) return $true } finally { $key.Close() } } function Update-GostmanEnvironment { [Environment]::SetEnvironmentVariable('GostmanPathRefresh', '1', 'User') [Environment]::SetEnvironmentVariable('GostmanPathRefresh', $null, 'User') } function Install-Gostman { $ErrorActionPreference = 'Stop' $ProgressPreference = 'SilentlyContinue' [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 $repository = 'kunalgoesbyken/gostman' $version = if ($env:GOSTMAN_VERSION) { $env:GOSTMAN_VERSION } else { 'latest' } $arch = Get-GostmanArch $asset = "gostman-cli-windows-$arch.zip" if ($env:GOSTMAN_BASE_URL) { $base = $env:GOSTMAN_BASE_URL.TrimEnd('/') } elseif ($version -eq 'latest') { $base = "https://github.com/$repository/releases/latest/download" } else { $base = "https://github.com/$repository/releases/download/v$($version.TrimStart('v'))" } if ($env:GOSTMAN_INSTALL_DIR) { $target = $env:GOSTMAN_INSTALL_DIR } else { if (-not $env:LOCALAPPDATA) { throw 'LOCALAPPDATA is not set. Set GOSTMAN_INSTALL_DIR to the folder to install into.' } $target = Join-Path $env:LOCALAPPDATA 'Programs\Gostman\cli' } $work = Join-Path ([IO.Path]::GetTempPath()) ('gostman-install-' + [Guid]::NewGuid().ToString('N')) New-Item -ItemType Directory -Path $work | Out-Null try { Write-Host "Downloading $asset" try { Invoke-WebRequest -UseBasicParsing -Uri "$base/$asset" -OutFile (Join-Path $work $asset) } catch { throw "Could not download $base/$asset. There may be no Windows $arch build in this release. $($_.Exception.Message)" } try { Invoke-WebRequest -UseBasicParsing -Uri "$base/SHA256SUMS" -OutFile (Join-Path $work 'SHA256SUMS') } catch { throw "Could not download $base/SHA256SUMS. $($_.Exception.Message)" } $expected = Get-GostmanExpectedHash -SumsPath (Join-Path $work 'SHA256SUMS') -Name $asset if (-not $expected) { throw "SHA256SUMS has no entry for $asset." } $actual = (Get-FileHash -Algorithm SHA256 -LiteralPath (Join-Path $work $asset)).Hash.ToLowerInvariant() if ($actual -ne $expected) { throw "$asset does not match SHA256SUMS (expected $expected, got $actual). Nothing was installed." } $unpacked = Join-Path $work 'unpacked' Expand-Archive -LiteralPath (Join-Path $work $asset) -DestinationPath $unpacked $fresh = Join-Path $unpacked 'gostman.exe' if (-not (Test-Path -LiteralPath $fresh)) { throw "$asset has no gostman.exe." } New-Item -ItemType Directory -Force -Path $target | Out-Null $installed = Join-Path $target 'gostman.exe' $previous = Join-Path $target 'gostman.old' Remove-Item -LiteralPath $previous -Force -ErrorAction SilentlyContinue $movedAside = $false if (Test-Path -LiteralPath $installed) { try { Move-Item -LiteralPath $installed -Destination $previous -Force $movedAside = $true } catch { throw "Could not replace $installed. Close programs that use gostman and run the command again." } } try { Copy-Item -LiteralPath $fresh -Destination $installed -Force } catch { if ($movedAside) { Move-Item -LiteralPath $previous -Destination $installed -Force -ErrorAction SilentlyContinue } throw "Could not write $installed. $($_.Exception.Message)" } Remove-Item -LiteralPath $previous -Force -ErrorAction SilentlyContinue Write-Host "Installed gostman in $installed" if ($env:GOSTMAN_NO_PATH -eq '1') { Write-Host "Left the PATH as it is. Add $target to it to type gostman in a terminal." } elseif ($target.Contains(';') -or $target.Contains('%')) { Write-Host "Not adding $target to the PATH: the folder name has a ; or a % in it. Run $installed by its full path." } else { if (Add-GostmanToUserPath -Directory $target) { Update-GostmanEnvironment Write-Host "Added $target to your PATH. Open a new terminal to use gostman." } else { Write-Host "$target is already on your PATH." } if (($env:Path -split ';') -notcontains $target) { $env:Path = "$env:Path;$target" } } } finally { Remove-Item -LiteralPath $work -Recurse -Force -ErrorAction SilentlyContinue } } Install-Gostman